简体   繁体   English

构建 Vue.js 项目后,CASL 库无法正常工作

[英]After build Vue.js project the CASL library not working

I try check ability by class instance:我尝试通过 class 实例检查能力:

<Can
 I="delete"
 :a="createHomeTask(item.teacher.id)"
>
</Can>

Where ability description:其中能力描述:

if (role === 'ROLE_teacher') {
  can('delete', 'HomeTask', { teacher: user.id });
}

Where createHomeTask:在哪里创建HomeTask:

class HomeTask {
  constructor(teacherId) {
    this.teacher = teacherId;
  }
}

export default function createHomeTask(teacherId) {
  return new HomeTask(teacherId);
}

Before building the project, everything works fine, but after the build, this functionality does not work.在构建项目之前,一切正常,但在构建之后,此功能不起作用。

What could be the problem?可能是什么问题呢?

I believe this is caused by Vue minifying code when building, so CASL doesn't work properly.我相信这是由于 Vue 在构建时缩小代码造成的,因此 CASL 无法正常工作。 This question might be also related to your problem: CASL is not working properly in Vue Production Mode .这个问题也可能与您的问题有关: CASL is not working proper in Vue Production Mode (However, the link to the docs in the answer is outdated for some reason, but I have this page saved) (但是,由于某种原因,答案中的文档链接已过时,但我保存了此页面)

In official docs (at least earlier) solving this problem was covered:在官方文档(至少更早)中解决了这个问题:

By default, CASL is looking for modelName attribute on constructor of the passed object (and fallbacks to constructor name if that is blank).默认情况下,CASL 在传递的 object 的构造函数上查找 modelName 属性(如果为空,则回退到构造函数名称)。

class Post {
  constructor({ title, published }) {
    this.title = title
    this.published = published
  }
}

Important : if you use minification for production builds, it will minify class names as well and this example won't work.重要提示:如果您将缩小用于生产构建,它也会缩小 class 名称,此示例将不起作用。 For such cases, you can define static modelName attribute on a class.对于这种情况,您可以在 class 上定义 static modelName 属性。

In this example, ability will check rules for Post and not for Article:在此示例中,功能将检查 Post 而不是 Article 的规则:

class Article {
  static get modelName() {
    return 'Post'
  }

  constructor({ title, published }) {
    this.title = title
    this.published = published
  }
}

I suggest you try doing the following:我建议您尝试执行以下操作:

class HomeTask {
  static get modelName() {
    return 'HomeTask'
  }
  constructor(teacherId) {
    this.teacher = teacherId;
  }
}

I hope this will help you.我希望这能帮到您。

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

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