angular - 找不到管道 'async' - Angular 11

标签 angular typescript tsconfig angular-pipe

我正在尝试使用 Angular 11 作为可观察对象获取数据,并且在延迟加载的组件/模块中使用异步或 json 管道时遇到问题。我在控制台中收到错误消息。
模块:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { TestRoutingModule } from './test-routing.module';
import { TestComponent } from './test.component';

@NgModule({
  declarations: [TestComponent],
  imports: [CommonModule, TestRoutingModule],
})
export class TestModule {}
组件:
import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';

@Component({
  selector: 'app-test',
  templateUrl: './test.component.html',
  styleUrls: ['./test.component.scss'],
})
export class TestComponent implements OnInit {
  user$: Observable<any>;

  constructor(private http: HttpClient) {}

  ngOnInit() {
    this.user$ = this.http.get('https://jsonplaceholder.typicode.com/users/1');
  }
}
测试组件 html:
<pre>{{ user$ | async }}</pre>
应用模块:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { CommonModule } from '@angular/common';

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';

@NgModule({
  declarations: [AppComponent],
  imports: [
    CommonModule,
    AppRoutingModule,
    BrowserModule,
    BrowserAnimationsModule,
    HttpClientModule,
  ],
  providers: [],
  bootstrap: [AppComponent],
})
export class AppModule {}
app-routing.module.ts
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { IndexComponent } from './features/index/index.component';

const routes: Routes = [
  { path: 'index', component: IndexComponent },
  {
    path: 'test',
    loadChildren: () =>
      (
        import(
          './features/test/test-routing.module'
        )
      ).then(p => p.TestRoutingModule),
  }
  { path: '', redirectTo: '/index', pathMatch: 'full' },
  { path: '**', redirectTo: '/index' },
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule],
})
export class AppRoutingModule {}
我的 package.json 是:
  "dependencies": {
    "@angular/animations": "~11.0.9",
    "@angular/cdk": "^11.1.2",
    "@angular/common": "~11.0.9",
    "@angular/compiler": "~11.0.9",
    "@angular/core": "~11.0.9",
    "@angular/forms": "~11.0.9",
    "@angular/material": "^11.1.2",
    "@angular/platform-browser": "~11.0.9",
    "@angular/platform-browser-dynamic": "~11.0.9",
    "@angular/router": "~11.0.9",
    "eslint": "^7.19.0",
    "rxjs": "~6.6.0",
    "tslib": "^2.0.0",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.1100.7",
    "@angular/cli": "~11.0.7",
    "@angular/compiler-cli": "~11.0.9",
    "@types/jasmine": "~3.6.0",
    "@types/node": "^12.11.1",
    "codelyzer": "^6.0.0",
    "jasmine-core": "~3.6.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~5.1.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.0.3",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "protractor": "~7.0.0",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~4.0.2"
  }
我的 tsconfig.json:
{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "src",
    "paths": {
      "@app/*": ["app/*"],
      "@src/*": ["*"]
    },
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "typeRoots": ["node_modules/@types"],
    "module": "es2020",
    "lib": ["es2018", "dom"]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}
现在我已经尝试过:
  • 将 CommonModule 添加到延迟加载模块和 AppModule(不工作)
  • 将 Angular 更新为 Angular 11.2.0 版本(不工作)

  • 我已经检查了 core.js 中的 getPipeDef$1 函数,并且注册表为空(请参见图片):
    enter image description here
    知道如何解决这个问题吗?谢谢

    最佳答案

    这是因为您的应用程序的路由模块延迟加载错误的模块。您应该延迟加载具有路由模块作为导入的功能模块:
    路由:

    const routes: Route[] = [
      { path: 'your-path-here', loadChildren: () => import('./path/to/feature.module')
        .then(m => m.FeatureModule) }, // Not FeatureRoutingModule!
      // ...
    ]
    
    功能模块:
    @NgModule({
      imports: [
        // ...
        // Your routing module for the feature module should be imported here:
        FeatureRoutingModule
      ]
    })
    export class FeatureModule {}
    
    有关如何延迟加载功能模块的更多信息,请考虑查看 documentation .

    关于angular - 找不到管道 'async' - Angular 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66275870/

    相关文章:

    javascript - 在 Angular 4 中悬停时显示特定表格单元格上的项目

    compiler-errors - TypeScript : Duplicate identifier in function type

    typescript - 如何在 vs-code 中使用多个 tsconfig 文件?

    javascript - JS 正则表达式允许小写字母和下划线(在 之间)

    Angular Testing : provide injected @Attribute in TestBed

    angular - 标识符 'required' 未定义。 '__type' 不包含这样的成员

    javascript - 为什么 Sentry 会忽略我的 React 应用程序中的一些错误?

    angularjs - 使用 typescript 缩小编写的 Angular 提供者安全

    javascript - tsconfig.json 不关心allowJs

    typescript - 如何包含排除文件夹中的文件/子文件夹?