1.utils文件夹下创建Loginplugin.js
import store from '@/store/'
import router from '@/router/router.js'
import { Dialog } from 'vant';
let myPlugin = {}
myPlugin.install = (Vue) => {
Vue.prototype.$isLogin = function () {
if (store.state.userInfo) {
return true;
} else {
Dialog
.confirm({
title: "提示",
message: "该功能需要先登录,是否去登录?"
})
.then(() => {
router.push('/login')
})
.catch(() => {
});
return false;
}
}
}
export default myPlugin
2.在vue文件中使用
- 在需要登陆的地方对是否登录进行判断,如果登录了,就正常发送请求,如果没有登陆,打回到登录页
async doFollow(aut_id) {
if (this.$isLogin()) {
await follow(aut_id);
this.detail.is_followed = true;
}
},