<div class="collection"><el-button @click="collect">收藏</el-button></div>点击收藏按钮,上传KNOWLEDGEID参数到post接口; 这是封装的接口: export function AddKnowledgeCollectApi(data: AddKnowledgeCollect): AxiosPromise<KnowledgeCollectItem[]> { return http({ url: '/T_KMS_BU_KNOWLEDGECOLLECT/Add', method: 'post', data }); } 这是接口类型:export interface AddKnowledgeCollect { KNOWLEDGEID: string }在vue3中实现这个功能
时间: 2024-02-19 12:03:56 浏览: 101
您可以在Vue3中使用以下代码实现这个功能:
首先,您需要在组件中导入 `AddKnowledgeCollectApi` 接口:
```typescript
import { AddKnowledgeCollectApi } from '@/api/knowledge';
```
然后,在 `methods` 中定义 `collect` 方法来上传 `KNOWLEDGEID` 参数到 `post` 接口:
```typescript
import { defineComponent } from 'vue';
import { AddKnowledgeCollectApi } from '@/api/knowledge';
export default defineComponent({
methods: {
collect() {
const data = {
KNOWLEDGEID: 'your_knowledge_id_here',
};
AddKnowledgeCollectApi(data)
.then((response) => {
console.log('收藏成功!', response);
})
.catch((error) => {
console.error('收藏失败!', error);
});
},
},
});
```
请注意,您需要将 `"your_knowledge_id_here"` 替换为您要收藏的具体知识 ID。
最后,在模板中添加一个按钮来触发 `collect` 方法:
```html
<template>
<div class="collection">
<el-button @click="collect">收藏</el-button>
</div>
</template>
```
这样,当用户点击“收藏”按钮时,Vue3 应用程序将调用 `AddKnowledgeCollectApi` 接口,并将 `KNOWLEDGEID` 参数上传到后端。如果收藏成功,控制台将输出“收藏成功!”消息,否则将输出“收藏失败!”消息。
阅读全文
相关推荐
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
这是前端代码
{{ collect.bookName }} ¥{{ collect.bookPrice }} <button @click="delCollect(collect.bookId)"> × </button> new Vue({ el: "#app", data: { userId: '', collectBook: [] }, 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); }); }, 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("取消收藏"); // 从 collectBook 数组中移除被删除的收藏 _this.collectBook = _this.collectBook.filter(collect => collect.bookId !== bookId); 请分析逻辑是否有误
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
{{ collect.bookName }} ¥{{ collect.bookPrice }} <button @click="delCollect()"> × </button> new Vue({ el: "#app", data: { userId: '', collectBook: [] }, 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); }); }, methods: { delCollect() { var _this = this; axios({ method: "delete", url: "http://localhost:8080/BookShopSystem_war/deleteCollect", data: { userId: _this.userId, bookId: _this.collectBook.bookId } }).then(resp => { alert("取消收藏"); }) } }该取消收藏方法有错,给出原因并纠正
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)