gpt4 book ai didi

javascript - 有没有办法在 main.js 中访问 vue js Vue Session

转载 作者:行者123 更新时间:2023-12-04 03:35:19 25 4
gpt4 key购买 nike

我在 main.js 中有以下设置:

import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
import Home from './views/Home.vue'
import Login from './views/Login.vue'
import Profile from './views/Profile.vue'
import Simulations from './views/Simulations.vue'
import VueSimpleAlert from "vue-simple-alert"
import StatisticPage from './views/StatisticPage.vue'
import VueSession from 'vue-session'
import Users from './views/Users.vue'

Vue.use(VueSimpleAlert)
Vue.use(VueRouter)
Vue.use(VueSession)

Vue.config.productionTip = false

const routes = [
{path: '/', name:'Login', component: Login },
{path: '/users', name:'Users', component: Users},
{path :'/home', name:'Home', component: Home},
{path: '/subject/:Pid', name: 'Profile', component: Profile},
{path: '/subject/:Pid/simulations', name:'Simulations', component: Simulations},
{path: '/subject/:Pid/simulations/:Sid/statistics', name:'Statistics', component: StatisticPage}
]

const router = new VueRouter({
routes,
mode: 'history'
})

router.beforeEach((to, from, next) => {
if(to.name !== 'Login'){
if(this.$session.get('token')){
next()
}else{
next({ name: 'Login'})
next()
}
}else{
next()
}
})

new Vue({
router,
render: h => h(App),
}).$mount('#app')

export default router

如您所见,我正在尝试访问 session 信息,但我一直收到 $session 未定义的错误。如何在此处访问路由器中的 session 信息?

最佳答案

如果你想在路由器中访问 Vue 实例,请使用 router.app 而不是 this

改变这一行:

this.$session.get('token')

为此:

router.app.$session.get('token')

您可以在以下位置阅读有关路由器实例属性的更多信息:https://router.vuejs.org/api/#router-instance-properties

关于javascript - 有没有办法在 main.js 中访问 vue js Vue Session,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67019917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com