简体   繁体   English

全局使用角管

[英]Using angular pipe globally

I am working on Ionic project with angular, I need to use pipe for almost all my pages but I am getting errors: 我正在使用angular进行离子项目,我几乎需要对所有页面使用管道,但出现错误:

Logic 逻辑

  1. If I use declarations in one of my pages pipe works just fine 如果我在我的页面之一中使用declarations ,管道就可以正常工作
  2. If I add same declarations in another page it returns error that this pipe has been used twice and I might consider using upper module. 如果我在另一页中添加相同的declarations ,则会返回此管道已被使用两次的错误,我可能会考虑使用上层模块。
  3. If I add my pipe to app.module.ts file and try to access it in my pages it says pipe not found! 如果我将管道添加到app.module.ts文件并尝试在我的页面中访问它,则显示未找到管道!

Any idea how to get my pipe globally? 知道如何在全球范围内获取管道吗?

Code

app.module.ts

import { KeepHtmlPipe } from './pipes/keep-html.pipe';

@NgModule({
  declarations: [AppComponent, KeepHtmlPipe],
  entryComponents: [],
  imports: [
    HttpClientModule,
    FormsModule,
    CommonModule,
    ReactiveFormsModule,
    BrowserModule,
    IonicModule.forRoot(),
    AppRoutingModule,
    VariationsPageModule
  ],
  providers: [
    StatusBar,
    SplashScreen,
    LaunchReview,
    NativeStorage,
    ImagePicker,
    Camera,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

pipe

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';

@Pipe({
  name: 'keepHtml'
})
export class KeepHtmlPipe implements PipeTransform {

  constructor(private sanitizer: DomSanitizer) {
  }

  transform(content) {
    return this.sanitizer.bypassSecurityTrustHtml(content);
  }
}

usage in html files

<div [innerHTML]="product.stars | keepHtml"></div>

Update 更新资料

based on answers I've made new file pipes.module.ts 根据答案,我制作了新文件pipes.module.ts

import { NgModule } from '@angular/core';
import { KeepHtmlPipe } from './keep-html.pipe';

@NgModule({
    declarations: [
        KeepHtmlPipe
    ],
    imports: [],
    exports: [
        KeepHtmlPipe
    ]
})

Then add it to my app.module.ts 然后将其添加到我的app.module.ts

import { PipesModule } from './pipes/pipes.module';

@NgModule({
  declarations: [AppComponent],
  entryComponents: [],
  imports: [
    HttpClientModule,
    FormsModule,
    CommonModule,
    PipesModule, //here
    ReactiveFormsModule,
    BrowserModule,
    IonicModule.forRoot(),
    AppRoutingModule,
    VariationsPageModule
  ],
  providers: [
    StatusBar,
    SplashScreen,
    LaunchReview,
    NativeStorage,
    ImagePicker,
    Camera,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

And in my pages I add this: 在我的页面中添加以下内容:

import { KeepHtmlPipe } from 'src/app/pipes/keep-html.pipe';

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    ReactiveFormsModule,
    IonicModule,
    RouterModule.forChild(routes)
  ],
  declarations: [FavoritesPage, KeepHtmlPipe], //here
  exports: [
    KeepHtmlPipe //and here
  ]
})
export class FavoritesPageModule {}

Now I'm getting this error: 现在我得到这个错误:

ERROR Error: Uncaught (in promise): Error: Type KeepHtmlPipe is part of the declarations of 2 modules: PipesModule and FavoritesPageModule! Please consider moving KeepHtmlPipe to a higher module that imports PipesModule and FavoritesPageModule. You can also create a new NgModule that exports and includes KeepHtmlPipe then import that NgModule in PipesModule and FavoritesPageModule.

What you want to do is create a shared module, that your other modules can import. 您要做的是创建一个共享模块,其他模块可以导入该模块。 This was you can re-use components, pipes, module etc.. in any module! 您可以在任何模块中重复使用组件,管道,模块等。

You can do the following (you will have to remove KeepHtmlPipe from the current module) 您可以执行以下操作(您必须从当前模块中删除KeepHtmlPipe

import { CommonModule } ...
import { NgModule } ...

import { KeepHtmlPipe } ...

// ...

@NgModule({
  imports: [
    CommonModule,
  ],
  declarations: [
    KeepHtmlPipe
  ],
  exports: [
    KeepHtmlPipe
  ]
})
export class SharedModule {}  

!important - you must export the KeepHtmlPipe !important -您必须导出KeepHtmlPipe

then in your app.module 然后在你的app.module中

import { SharedModule } from ...

// ...

imports: [
   SharedModule
]

now you can use your pipe anywhere in your app, and in future if you want to add any more global pipes, directives or components they can go in this SharedModule 现在,您可以在应用程序中的任何位置使用管道,将来,如果您想添加更多可以在此SharedModule全局管道,指令或组件,

I recommend to create a module containing all your global pipes, then import the module in your app module. 我建议创建一个包含所有全局管道的模块,然后将该模块导入您的应用程序模块。 Here's how you do it: 这是您的操作方式:

1) Create pipes.module.ts inside src/pipes folder 1)在src / pipes文件夹中创建pipes.module.ts

import { NgModule } from '@angular/core';
import { KeepHtmlPipe } from './keep-html.pipe';

@NgModule({
    declarations: [
      KeepHtmlPipe
    ],
    imports: [],
    exports: [
      KeepHtmlPipe
    ]
})
export class PipesModule {}

2) In your app.module import the PipesModule and add it to the imports collection 2)在您的app.module中,导入PipesModule并将其添加到imports集合中

import { PipesModule } from '../pipes/pipes.module';

[...]

@NgModule({
  declarations: [
    MyApp
  ],
  imports: [
    [...]
    PipesModule
  ],
[...]

Afterwards you can use your pipe anywhere in the app. 之后,您可以在应用程序中的任何位置使用管道。 If you add more global pipes later just also add it to your PipesModule. 如果以后添加更多全局管道,也只需将其添加到PipesModule中即可。

Posting an answer to try and help explain some things. 发布答案以尝试帮助解释一些事情。

As answered by others, you need to create a "shared" module of some kind. 就像其他人回答的那样,您需要创建某种“共享”模块。 In your update, you've named it PipesModule , so I will use that from here on. 在您的更新中,您将其命名为PipesModule ,因此我将从这里开始使用它。

Pipes Module: 管道模块:

import { NgModule } from '@angular/core';
import { KeepHtmlPipe } from './keep-html.pipe';

@NgModule({
    declarations: [
        KeepHtmlPipe
    ],
    imports: [],
    exports: [
        KeepHtmlPipe
    ]
})

Now, when you import this into any other module , you will automattically have access to KeepHtmlPipe . 现在, 将其导入任何其他模块时 ,您将自动访问KeepHtmlPipe

FavoritesPageModule: 收藏夹页面模块:

import { PipesModule } from 'src/app/pipes/pipes.module';

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    ReactiveFormsModule,
    IonicModule,
    PipesModule,
    RouterModule.forChild(routes)
  ],
  declarations: [
    FavoritesPage
  ]
})
export class FavoritesPageModule {}

Adding PipesModule to the imports here gives you access to the KeepHtmlPipe that was exported in the PipesModule in any component declared in this module (IE: Part of the declarations list). 添加PipesModule的进口这里,您可以访问到KeepHtmlPipe已导出在PipesModule中(即:部分在此模块中声明的任何组件declarations列表)。

favorites-page.component.html favorite-page.component.html

<div [innerHTML]="product.stars | keepHtml"></div>

This should now work fine. 现在应该可以正常工作了。

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

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