我正在努力角2最终版本。
我已经声明了两个模块:主应用程序和一个用于设置页面的模块。
该主模块被宣布全球管道。该模块还包括设置模块。
app.module.ts
@NgModule({
imports: [BrowserModule, HttpModule, routing, FormsModule, SettingsModule],
declarations: [AppComponent, JsonStringifyPipe],
bootstrap: [AppComponent]
})
export class AppModule { }
settings.module.ts
@NgModule({
imports: [CommonModule, HttpModule, FormsModule, routing],
declarations: [SettingsComponent],
exports: [SettingsComponent],
providers: []
})
export class SettingsModule { }
当试图使用管道的设置模块,我得到了管找不到一个错误。
zone.min.js?cb=bdf3d3f:1 Unhandled Promise rejection: Template parse errors:
The pipe 'jsonStringify' could not be found (" <td>{{user.name}}</td>
<td>{{user.email}}</td>
<td>[ERROR ->]{{user | jsonStringify}}</td>
<td>{{ user.registered }}</td>
</tr"): ManageSettingsComponent@44:24 ; Zone: <root> ; Task: Promise.then ; Value: Error: Template parse
如果我将管道包含在设置模块中,它将抱怨两个模块具有相同的管道。
zone.min.js?cb=bdf3d3f:1 Error: Error: Type JsonStringifyPipe is part of the declarations of 2 modules: SettingsModule and AppModule! Please consider moving JsonStringifyPipe to a higher module that imports SettingsModule and AppModule. You can also create a new NgModule that exports and includes JsonStringifyPipe then import that NgModule in SettingsModule and AppModule.
json-stringify.pipe.ts
@Pipe({name: 'jsonStringify'})
export class JsonStringifyPipe implements PipeTransform {
transform(object) {
// Return object as a string
return JSON.stringify(object);
}
}
有什么想法吗?
如果要在其他模块中使用管道,则将要声明管道的模块添加到imports: [...]
要重用管道的模块中,而不是将其添加到declarations: []
多个模块中。
例如:
@NgModule({
imports: [],
declarations: [JsonStringifyPipe],
exports: [JsonStringifyPipe]
})
export class JsonStringifyModule { }
@NgModule({
imports: [
BrowserModule, HttpModule, routing, FormsModule, SettingsModule,
JsonStringifyModule],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
@NgModule({
imports: [
CommonModule, HttpModule, FormsModule, routing,
JsonStringifyModule],
declarations: [SettingsComponent],
exports: [SettingsComponent],
providers: []
})
export class SettingsModule { }
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句