我做了同样的事情: How to correctly import the Angular Material module through a shared module in Angular 4?
除了我想创建一个类似ui-kit的模板,然后在具有不同位置的不同应用程序中使用它(它们不在同一个文件夹中)。
目前,我正在这样做,一切正常,除非我尝试在我的sharedComponent中使用库,就像我想要使用的那样:
<button mat-raised-button color="primary">Click me!</button>
角度材质按钮,
angular-cli不会抛出任何错误,我只是在控制台上看到这个错误:
Error: StaticInjectorError[ElementRef]:
StaticInjectorError[ElementRef]:
NullInjectorError: No provider for ElementRef!
at _NullInjector.get (core.js:993)
at resolveToken (core.js:1281)
at tryResolveToken (core.js:1223)
at StaticInjector.get (core.js:1094)
at resolveToken (core.js:1281)
at tryResolveToken (core.js:1223)
at StaticInjector.get (core.js:1094)
at resolveNgModuleDep (core.js:10878)
at NgModuleRef_.get (core.js:12110)
at resolveDep (core.js:12608)
BtnComponent.html:3 ERROR TypeError: Cannot read prop
并且通过谷歌搜索找不到任何有用的东西。
任何提示都有帮助
更新
我创建了一个会重现错误的github存储库。
https://github.com/molikh/shared-module
我在角度材料库中也写了一个问题,并提供了更多解释: https://github.com/angular/material2/issues/9294#event-1416171140
答案 0 :(得分:3)
I found one solution for my problem.
I build some libraries out of my components and then installed them inside my other apps.
Here is a complete tutorial about it :
答案 1 :(得分:0)
在我的情况下,问题是我没有在我导出指令的包模块中导入BrowserModule所以我所做的就是导入它并将其添加到导入数组
喜欢这个
import { NgModule } from '@angular/core';
import { myDirective } from './my.directive';
import { BrowserModule } from '@angular/platform-browser';
@NgModule({
imports: [BrowserModule
],
declarations: [myDirective],
exports: [myDirective],
})
export class ImageHelperModule { }
答案 2 :(得分:0)
当我尝试从具有自己的node_modules目录的子项目中导入模块时,我也表达了同样的问题。将库构建到“主机”项目的./dist目录中并从那里导入可以解决此问题。