Angular 2 - 多个模块中的管道重用 - 未找到错误或重复定义

kit*_*lku 27 angular2-pipe angular

我正在进行角度2最终版本.

我已经宣布了两个模块:主应用程序和一个用于设置页面.

主模块被宣布全球管道.该模块还包括设置模块.

app.module.ts

@NgModule({
    imports: [BrowserModule, HttpModule, routing, FormsModule, SettingsModule],
    declarations: [AppComponent, JsonStringifyPipe],
    bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

settings.module.ts

@NgModule({
    imports: [CommonModule, HttpModule, FormsModule, routing],
    declarations: [SettingsComponent],
    exports: [SettingsComponent],
    providers: []
})
export class SettingsModule { }
Run Code Online (Sandbox Code Playgroud)

当尝试在设置模块中使用管道时,收到一条错误,指出无法找到管道.

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 
Run Code Online (Sandbox Code Playgroud)

如果我将管道包含在设置模块中,则会抱怨两个模块具有相同的管道.

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.
Run Code Online (Sandbox Code Playgroud)

JSON-stringify.pipe.ts

@Pipe({name: 'jsonStringify'})
export class JsonStringifyPipe implements PipeTransform {
    transform(object) {
        // Return object as a string
        return JSON.stringify(object);
    }
}
Run Code Online (Sandbox Code Playgroud)

对此有何想法?

Gün*_*uer 57

如果要在其他模块中使用管道,则将声明管道的模块添加到imports: [...]要重新使用管道的模块,而不是将其添加到declarations: []多个模块中.

例如:

@NgModule({
    imports: [],
    declarations: [JsonStringifyPipe],
    exports: [JsonStringifyPipe]
})
export class JsonStringifyModule { }
Run Code Online (Sandbox Code Playgroud)
@NgModule({
    imports: [
      BrowserModule, HttpModule, routing, FormsModule, SettingsModule,
      JsonStringifyModule],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)
@NgModule({
    imports: [
       CommonModule, HttpModule, FormsModule, routing, 
       JsonStringifyModule],
    declarations: [SettingsComponent],
    exports: [SettingsComponent],
    providers: []
})
export class SettingsModule { }
Run Code Online (Sandbox Code Playgroud)

  • 为什么这是个好问题.因为Angular2是这样设计的.我想这没有多大帮助;-).引入了'NgModule`概念,使延迟加载与离线模板编译一起工作,我猜他们有充分的理由以这种方式设计它.Angular只需要知道组件,指令,管道和服务的使用位置,以便能够创建适用于延迟加载和脱机编译的小而有效的代码. (5认同)
  • 谢谢你的答案......为什么我需要为管道创建一个模块?为什么在全局模块中定义管道不足以使管道可用于其他模块? (4认同)
  • 您无需为每个管道创建模块.您可以在此类模块中放置尽可能多的可重用组件,指令,管道和服务.但是,您需要在使用其中一个部件的任何地方导入它. (2认同)
  • 谢啦!你的答案澄清了很多概念 (2认同)