怎么实现在按下收藏按钮后出现你确定关注吗
时间: 2024-12-21 18:26:50 浏览: 4
要在按下收藏按钮后显示“你确定关注吗”的确认提示,可以使用 `ElMessageBox` 组件来实现。具体步骤如下:
1. **引入 `ElMessageBox`**:在 `<script setup>` 中引入 `ElMessageBox`。
2. **修改 `handleClick` 方法**:在 `handleClick` 方法中添加弹出确认框的逻辑。
以下是具体的代码示例:
```html
<template>
<div class="wrapper">
<!-- 其他代码 -->
<ul class="business">
<li v-for="(item,index) in businessList" :key="index" @click="toBusinessInfo(item.businessId)">
<img :src="item.businessImg" alt="">
<div class="cart-left-icon-quantity" v-if="item.quantity != 0 && item.quantity != undefined">{{ item.quantity }}</div>
<div class="business-info">
<div class="business-info-h">
<h3>{{ item.businessName }}</h3>
<div class="business-info-like" :style="getStyle(item)" @click.stop="handleClick(item)">
<i class="fa fa-star" aria-hidden="true" />
</div>
</div>
<!-- 其他代码 -->
</div>
</li>
</ul>
<!-- 其他代码 -->
</div>
</template>
<script setup>
import Footer from '../components/Footer.vue';
import { getSessionStorage } from '../common.js';
import { ref } from "vue";
import { get, post } from '../api';
import { useRouter } from 'vue-router';
import { ElDrawer, ElMessage, ElMessageBox } from 'element-plus';
const drawer = ref(false);
const router = useRouter();
const account = getSessionStorage("account");
const categoryList = ref([]);
const businessList = ref([]);
const keyword = ref('');
const money = ref('');
const getStyle = (item) => {
return { fontSize: '3.2vw', marginLeft: '1vw', color: item.clicked ? 'red' : 'grey' };
};
const handleClick = (item) => {
ElMessageBox.confirm(
'你确定关注吗?',
'确认操作',
{
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}
).then(() => {
if (!item.clicked) {
item.clicked = true;
IncrementReadCount(item);
} else {
item.clicked = false;
incrementReadCount(item);
}
}).catch(() => {
// 取消操作
});
};
const IncrementReadCount = (item) => {
const requestData = { businessId: item.businessId, accountId: account.accountId };
post('/AccountBusinessHandler/Statu', requestData, false)
.then((response) => { })
.catch(error => { console.error('更新浏览量失败', error); });
};
const incrementReadCount = (item) => {
const requestData = { businessId: item.businessId, accountId: account.accountId };
post('/AccountBusinessHandler/statu', requestData, false)
.then((response) => { })
.catch(error => { console.error('更新浏览量失败', error); });
};
const init = () => {
let url = "/CategoryHandler/get";
get(url).then(response => {
categoryList.value = response.data.resultdata;
}).catch(e => {
console.log(e);
});
let business_url = "/BusinessHandler/listByCategoryId?categoryId=" + 0;
get(business_url).then(response => {
businessList.value = response.data.resultdata;
}).catch(e => {
console.log(e);
});
};
init();
const search = () => {
const keyword = document.getElementById('searchInput').value;
let s_url = "/BusinessHandler/search?keyword=" + keyword;
get(s_url).then(response => {
businessList.value = response.data.resultdata;
}).catch(e => {
console.log(e);
});
};
const toBusinessList = (id) => {
router.push({ path: '/businessList', query: { categoryId: id } });
};
const toVip = () => {
let url = "/OrdersHandler/sum?accountId=" + account.accountId;
get(url).then(response => {
money.value = response.data.resultdata;
if (money.value > 100) {
ElMessage({ message: '开通会员成功', type: 'success' });
} else {
ElMessage({ message: '开通会员失败', type: 'success' });
}
}).catch(e => {
console.log(e);
});
};
const toSvip = () => {
let url = "/OrdersHandler/sum?accountId=" + account.accountId;
get(url).then(response => {
money.value = response.data.resultdata;
if (money.value > 500) {
ElMessage({ message: '开通超级会员成功', type: 'success' });
} else {
ElMessage({ message: '订单金额不足,无法开通', type: 'error' });
}
}).catch(e => {
console.log(e);
});
};
</script>
<style scoped>
/* 保持原有样式 */
</style>
```
### 解释
1. **引入 `ElMessageBox`**:在 `<script setup>` 中引入 `ElMessageBox`。
2. **修改 `handleClick` 方法**:在 `handleClick` 方法中调用 `ElMessageBox.confirm` 来显示确认对话框。如果用户点击“确定”,则执行收藏或取消收藏的操作;如果用户点击“取消”,则不进行任何操作。
这样,当用户点击收藏按钮时,会弹出一个确认对话框,询问用户是否确定关注。
阅读全文