我正在尝试为我的应用程序实施CASL能力管理器angular 8
。我做了包使用指南中描述的所有事情,但can pipe
我的component html template
.
The pipe 'can' could not be found
所以这是我的应用程序模块:
@NgModule({
declarations: [],
imports: [
...
AbilityModule.forRoot()
],
exports: [],
providers: [
...
{ provide: Ability, useFactory: createAbility }
],
bootstrap: [AppComponent]
})
我也实现了这样的 casl 示例。
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;
}
});
}
我还会告诉它this.ability.can()
通过注入类在我的组件类中Ability
工作。但它不能与can pipe
我应该怎么做才能摆脱这个错误?
谢谢