alanning:role,angular2-meteor 中的导入问题

alanning:role, importing issue in angular2-meteor

我使用了 meteoralanning:role 包,Roles 工作正常,但问题是我收到警告"Cannot find name 'Roles"

请帮我解决这个问题。我到处搜索它。

有些人像这样导入它 "import { Roles } from 'alanning:roles';"

我也试过了,但还是报错... 请帮助我,请不要像 "asked issue" 那样提交它。 这两天我一直在搜索这个问题,但我没有得到任何答案,请给我任何帮助或请解决这个问题,在此先感谢。

您遇到的错误是输入错误。它是一个流星包,要通知打字稿您正在使用它,您必须通过导入 it.I 来告诉它遇到了同样的问题。实际上你忘了导入

 import { Roles } from 'meteor/alanning:roles';

在您的 collection 文件或发布文件中。将其导入您的 collection 并发布文件,您将不会遇到此拼写错误。

下一步,如果您在导入后遇到错误 Cannot find module 'meteor/alanning:roles',这意味着 meteor 在 typings.d.ts 中没有该包的定义。有些包还没有类型定义文件。你必须在 typings.d.ts 文件中创建你自己的打字稿定义,或者如果你有新的 angular 2 meteor 样板,其中有 @typings 文件夹。只需在该文件中添加此代码

declare module "meteor/alanning:roles" {
  export module Roles {
    function userIsInRole(id?: any,value?: any): boolean{  }
    function addUsersToRoles(id?: any,value?: any): boolean{ }
  }
}

对于其他出现类似错误的包,您可以创建自己的定义。

有一种更简单的方法可以在打字稿中导入流星包,而无需编写自己的声明。大多数类型都在这里定义

https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/meteor

安装已定义类型的 NPM 模块。

meteor npm install --save @types/meteor-roles

在您的模块中导入类型

import { } from '@types/meteor-roles';

现在,您可以将 Roles 类型用于 addUsersToRoles() 等方法。