【问题标题】:The pipe 'can' could not be found找不到管道“can”
【发布时间】:2020-02-14 17:21:56
【问题描述】:

我正在尝试为我的angular 8 应用程序实施CASL 能力管理器。我做了包使用指南中描述的所有事情,但我的component html template 中有一个关于can pipe 的错误。

The pipe 'can' could not be found

这是我的应用模块:

@NgModule({
    declarations: [],
    imports: [
...
        AbilityModule.forRoot()
    ],
    exports: [],
    providers: [
...
        { provide: Ability, useFactory: createAbility }
    ],
    bootstrap: [AppComponent]
})

我也像this casl example 一样实现了。

createAbility函数如下图:

import { AbilityBuilder, Ability } from '@casl/ability'
import { User, Claim } from '@/_models';
import { ABILITIES } from './const-variables';

var user = localStorage.getItem('currentUser');
var currentUser = JSON.parse(user) as User;

export function defineAbilitiesFor(user: User) {
  const { can, rules } = AbilityBuilder.extract();

  if (user.roles.includes("Admin")) {
    can("manage", "all");
  } else {
    user.claims.forEach(claim => {
      ABILITIES.map(ability => {
        if (ability.name === (claim as Claim).type) {
          can(ability.name, ability.module);
        }
      })
    });
  }
  return rules;
}

export function createAbility() {
  return new Ability(defineAbilitiesFor(currentUser), {
    subjectName(subject) {
      if (!subject || typeof subject === "string") {
        return subject;
      }
      return subject.__typename;
    }
  });
}

我会告诉我它也可以通过注入Ability 类在我的组件类中使用this.ability.can()。但它不能与can pipe 一起使用

我应该怎么做才能摆脱这个错误?

谢谢

【问题讨论】:

  • 你能展示你的 createAbility 工厂吗?
  • 你使用什么版本的 casl/angular 和 casl/ability?
  • 我在 Angular 10 中也遇到了这个问题,对我来说,这与将 AbilityModule 导入自定义模块有关。现在工作正常。

标签: javascript angular casl


【解决方案1】:

你创造了你的能力吗?我看到您已经定义了 useFactory: createAbility 但从文档中的示例是:

import { AbilityBuilder } from '@casl/ability'

export const ability = AbilityBuilder.define(can => {
   can('read', 'all')
})

如果您将其更改为 useValue 并根据示例构建您的定义,会发生什么情况?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-29
    • 2017-03-13
    • 1970-01-01
    • 2023-04-09
    • 2016-12-24
    • 2017-09-04
    • 2017-11-29
    • 2017-11-10
    相关资源
    最近更新 更多