如何修复 Angular Error: NG0302: The pipe 'async' could not be found in the '...' component
问题:
在浏览器控制台中,你看到如下错误消息
pipe_async_fix.ts
core.mjs:6677 ERROR Error: NG0302: The pipe 'async' could not be found in the '_MyComponent' component. Verify that it is included in the '@Component.imports' of this component. Find more at https://angular.dev/errors/NG0302
at getPipeDef (core.mjs:29500:15)
at Module.ɵɵpipe (core.mjs:29441:19)
at MyComponent_Template (my.component.html:1:1)
at executeTemplate (core.mjs:11621:9)
at renderView (core.mjs:12828:13)
at renderComponent (core.mjs:12774:5)
at renderChildComponents (core.mjs:12876:9)
at renderView (core.mjs:12856:13)
at renderComponent (core.mjs:12774:5)
at renderChildComponents (core.mjs:12876:9)解决方案
此问题通常与独立组件相关。
打开组件的 .ts 源代码并将 AsyncPipe 添加到其导入中:
async_pipe_import.ts
/* ... */
import { AsyncPipe } from '@angular/common';
@Component({
selector: 'app-my-component',
standalone: true,
imports: [
/* ...*/
AsyncPipe,
],
templateUrl: './my.component.html',
styleUrl: './my.component.scss'
})
export class MyComponent {如果你不使用独立组件,则将 AsyncPipe 添加到使用你组件的任何模块的 imports 中
Check out similar posts by category:
Angular, Typescript
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow