简体   繁体   English

@casl/vue:ability.js 文件应该是什么样的?

[英]@casl/vue: What should by ability.js file look like?

I'm trying to integrate @casl/vue with Vue 3, and I'm afraid I'm having problems.我正在尝试将@casl/vue与 Vue 3 集成,恐怕我遇到了问题。

Per the instructions, I've added the following to my /main.js:根据说明,我在 /main.js 中添加了以下内容:

import { abilitiesPlugin } from '@casl/vue';
import ability from './services/ability';

app.use(abilitiesPlugin, ability, {
  useGlobalProperties: true
})

So far, so good.到目前为止,一切都很好。 However, it's unclear what I should put in /services/ability.js.但是,目前还不清楚我应该在 /services/ability.js 中添加什么。

I tried the following (based on this ), and it works:我尝试了以下(基于this ),它有效:

import { defineAbility } from '@casl/ability';

export default defineAbility((can, cannot) => {
  can('manage', 'all');
  cannot('delete', 'User');
});

But, of course, this doesn't allow me to use different permissions for different users.但是,当然,这不允许我为不同的用户使用不同的权限。

So I tried the following instead (based on this ):所以我尝试了以下方法(基于):

import { defineAbility } from '@casl/ability';

export default (user) => defineAbility((can) => {
  can('read', 'Article');

  if (user.isLoggedIn) {
    can('update', 'Article', { authorId: user.id });
    can('create', 'Comment');
    can('update', 'Comment', { authorId: user.id });
  }
});

...and that doesn't work. ......那是行不通的。 In the console, I see:在控制台中,我看到:

plugin.ts:12 Uncaught Error: Please provide an Ability instance to abilitiesPlugin plugin
    at l (plugin.ts:12:11)
    at Object.use (runtime-core.esm-bundler.js:3812:21)
    at main.js?t=1651580949947:29:5

Got any hints?有什么提示吗? I'm finding the documentation to be pretty unclear.我发现文档很不清楚。 A practical example would really help me!一个实际的例子真的会帮助我!

Thanks!谢谢!

It means you must pass plugins as an instance of Ability这意味着您必须将插件作为 Ability 的实例传递

const ability = new Ability()常量能力 = 新能力()

.use(abilitiesPlugin, ability, {
  ....
  })

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

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