简体   繁体   English

CASL Vue - 如何在路由保护中访问 $can

[英]CASL Vue - How to access $can in route guard

I am building a Nuxt app and trying to create a route guard.我正在构建一个 Nuxt 应用程序并尝试创建一个路由守卫。 How can I access $can in my Nuxt middleware?如何在我的 Nuxt 中间件中访问$can

export default (context) => {
    const { route } = context
    route.matched.some((routeRecord) => {
        const options = routeRecord.components.default.options
        console.log(options)
        // should use $can here
        return true
    })
}

I import the following in my nuxt.config.js :我在nuxt.config.js中导入以下内容:

plugins: [
    { src: '@plugins/vue-can.js' },
]

I add casl as plugins on the Nuxt $auth module:我在 Nuxt $auth 模块上添加casl作为插件:

auth: {
    plugins: [
         '@plugins/vue-casl.js',
    ]
}

This is because I need to access $auth.user in my plugins.这是因为我需要在我的插件中访问$auth.user

All of this works good, except I can't find $abilities or $can in my Nuxt middleware.所有这些都很好,除了我在 Nuxt 中间件中找不到$abilities$can


PS: I am talking about this package: @casl/vue PS:我说的是这个package: @casl/vue

$abilities and $can should both be available as properties of the context object in your middleware, so you should be able to access them using context.$foo . $abilities$can应该都可以作为中间件中context object 的属性使用,因此您应该能够使用context.$foo访问它们。

If you don't see it, it is probably because it was not injected properly.如果您没有看到它,可能是因为它没有正确注入。 The plugin you registered in your nuxt.config.js should use inject to add $can to the global vue instance and make it available as a property of this and context .您在nuxt.config.js中注册的插件应该使用inject$can添加到全局 vue 实例并使其作为thiscontext的属性可用。 See the documentation for further detail: https://nuxtjs.org/docs/2.x/directory-structure/plugins/#inject-in-root--context有关详细信息,请参阅文档: https://nuxtjs.org/docs/2.x/directory-structure/plugins/#inject-in-root--context

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM