methods: { delCollect(bookId) { var _this = this; axios({ method: "delete", url: "http://localhost:8080/BookShopSystem_war/deleteCollect", data: { userId: _this.userId, bookId: bookId } }).then(resp => { alert("取消收藏"); }); } }该方法还需要userId,请仿照以下接口的方式获取到usrIdmounted() { var _this = this; axios.get('/BookShopSystem_war/getUserId') .then((response) => { this.userId = response.data; var userId = this.userId; // 将userId赋值给一个变量 axios({ method: 'get', url: 'http://localhost:8080/BookShopSystem_war/getCollectBook/' + userId }).then(function (result) { var code = result.data.code; var data = result.data.data; if (code == 200) { _this.collectBook = data; } console.log(data) }) }) .catch((error) => { console.log(error); }); },
时间: 2024-02-10 22:33:18 浏览: 103
你的问题是如何获取 userId,对吧?
对于获取 userId,可以仿照以下接口的方式,在 mounted 钩子函数中使用 axios 发送 get 请求,获取到后将 userId 赋值给一个变量。具体代码如下:
```
mounted() {
var _this = this;
axios.get('/BookShopSystem_war/getUserId')
.then((response) => {
this.userId = response.data;
var userId = this.userId; // 将userId赋值给一个变量
axios({
method: 'get',
url: 'http://localhost:8080/BookShopSystem_war/getCollectBook/' + userId
}).then(function (result) {
var code = result.data.code;
var data = result.data.data;
if (code == 200) {
_this.collectBook = data;
}
console.log(data)
})
})
.catch((error) => {
console.log(error);
});
},
```
在 delCollect 方法中,可以直接使用 this.userId 获取到 userId,具体代码如下:
```
delCollect(bookId) {
var _this = this;
axios({
method: "delete",
url: "http://localhost:8080/BookShopSystem_war/deleteCollect",
data: {
userId: this.userId, // 直接使用 this.userId 获取到 userId
bookId: bookId
}
}).then(resp => {
alert("取消收藏");
});
}
```
阅读全文