in fact, I want to do login verification in the hook function beforeEach, and this login verification will call the background interface, but I can"t get the instance of vm in this hook function, so I can"t get the token, in store and I can"t request the backend. After messing around all morning, I really can"t find a solution, so I can only ask you for advice
.
related codes
Router.vue
<script>
import Vue from "vue"
import VueRouter from "vue-router"
Vue.use(VueRouter)
import Api from "@/components/config/Api"
import home from "@/pages/index"
import login from "@/pages/login"
import info from "@/pages/info"
const router = new VueRouter({
routes: [{
path: "/",
name: "index",
component: home
},{
path: "/login",
name: "login",
component: login
},{
path: "/info",
name: "info",
component: info
}],
});
router.beforeEach((from, to, next) => {
this.$http.get(Api.is_login_uri,{params: {"1":this.$store.state.token}}).then(function(res){
let code = res.data.code;
if(code != "0"){
next({path:"/info",params:{"msg":res.data.data}});
}
},function(error){
next({path:"/info",params:{"msg":""}});
});
})
export default router;
</script>
Code of main.js
import router from "./components/config/Router"
import store from "./components/config/Store"
// pages Home.vue
//import index from "./pages/index"
// Vue *
var vm = new Vue({
el: "-sharpapp",
router,
store,
data(){
return {
}
},
created(){
document.title=this.Global.appName;
}
})