// router文件夹下的 index.js
import Vue from "vue";
import Router from "vue-router";
import routes from "./routes"; // 配置的路径
Vue.use(Router);
const router = new Router({
routes,
scrollBehavior
// mode: 'history'
});
const router = new Router({
routes,
scrollBehavior
// mode: 'history'
});
router.beforeEach((to, from, next) => {
if (to.matched.some(route => route.meta && route.meta.requiresAuth)) {
if (!this.$storage.getStorage("userInfo")) { // 没有登录信息跳转到登录页
next({
path: "/login",
query: { redirect: to.fullPath } // 'to.fullPath'跳转到登录之前页面的路径
});
} else {
next();
}
} else {
next();
}
});
export default router;
下方是我微信公众号的二维码,可以扫码关注以下,后期博文推送主要在公众号上面,有什么问题也可以通过公众号跟我发消息哦~ ![在这里插入图片描述](https://img-blog.csdnimg.cn/20201126185542689.jpg#pic_center)
在登陆页面( login.vue里 )
提交
import { webLogin } from '@/apis/apis'
export default {
data() {
return {
username: '',
password: '',
}
},
created() { },
methods: {
onSubmit() {
if (!this.username) {
this.$notify({ type: "danger", message: "请填写用户名" });
return;
} else if (!this.password) {
this.$notify({ type: "danger", message: "请填写密码" });
return;
}
let params = {
username: this.username,
password: this.password
}
webLogin(params).then(res => {
if (res.code == 0) {
this.$notify({ type: 'success', message: '恭喜,登录成功!' });
// this.$router.push('/user');
this.$storage.setStorage('userInfo', res.data);
setTimeout(() => {
let path = '/user'; // '/user' 为个人中心页面
if (this.$route.query.redirect) {
path = this.$route.query.redirect // 跳到之前的页面
}
this.$router.push({
path: path
});
}, 1000)
} else {
this.$notify(res.message)
}
})
}
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200613102702534.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzUwMzA4MA==,size_16,color_FFFFFF,t_70)
|