您的位置:首页 > Web前端 > Vue.js

vue 之 路由元信息(登录访问)

2019-10-09 20:26 134 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/ws19900201/article/details/102469221

main.js :

// main.js
router.beforeEach((to, from, next) => {
//to and from are Route Object,next() must be called to resolve the hook

// some:一真为真  every 一假为假
const needLogin = to.matched.some(route => route.meta && route.meta.login);
// console.log(needLogin);
if (needLogin) {
const isLogin = document.cookie.includes('login-true');
if (isLogin) {
next();
return;
}
const toLoginFlag = window.confirm('该页面需要登录后才能访问,去登录吗?');
if (toLoginFlag) {
next('/login');
}
return;
}
next();
})

登录页:

<!-- 登录页 -->
<template>
<div class="login">
<button @click="handleClick">登录</button>
</div>
</template>
<script>
export default {
methods: {
handleClick() {
const expires = 900 * 24 * 60 * 60 * 1000;
const date = new Date(+new Date() + expires);
//写cookie
document.cookie = `login=true;expires=${date.toUTCString(expires)}`
}
},
}
</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐