如何在功能模块中使用独立组件、管道或指令

Abo*_*zlR 7 angular angular14 angular-standalone-components

我将我的 Angular 项目更新为 Angular 14。现在我想要一些standalone components,,pipes或者directives

我有一个名为ProductModule的特色模块,并且希望在此模块中使用名为uppercase的独立管道。

// structure
---Product
          ---product.component
          ---product.service
          ---product.module.ts

---StandabloneFolder
                    ---uppercase.pipe.ts
Run Code Online (Sandbox Code Playgroud)

我的大写管子

@Pipe({
    name: 'uppercase',
    standalone: true,
})
export class UppercasePipe implements PipeTransform {
    transform(value: string): string {
        return "UPPERCASE_INPUT"
    }
}
Run Code Online (Sandbox Code Playgroud)

在product.component.html中

{{'Javascript Addicted' |uppercase}}
Run Code Online (Sandbox Code Playgroud)

出现以下错误:

找不到名称为“大写”的管道product.component.ts(6, 39):组件 ProductComponent 的模板中发生错误。

笔记:

standalone: true如果我添加到Product.Component并从声明数组中删除ProductComponent,这个问题就会得到解决。

Mox*_*arm -1

您需要将UppercasePipe 添加到product.module.tsimports中。

产品模块.ts

@NgModule({
  imports: [/*some import*/, UppercasePipe],
  /* other stuff*/
})
export class ProductModule {}
Run Code Online (Sandbox Code Playgroud)

  • @AbolfazlRoshanzamir,如果您的 ProductComponent 是独立的,您不应该再有任何 ProductModule 并在 ProductComponent 的导入选项中声明您的独立管道。独立管道仍需在进口时申报。 (3认同)
  • 我说的是独立管道,更多信息https://angular.io/guide/standalone-components (2认同)