未捕获的TypeError:无法读取registerNgModuleType-Angular 9处未定义的属性'id'

时间:2020-05-22 12:09:41

标签: angular build aot

我遇到了一个又一个的bug,一个又一个的修复,直到我似乎不理解为止。应用程序已编译,但主页为空,错误为:core.js:36236 Uncaught TypeError: Cannot read property 'id' of undefined at registerNgModuleType

数据服务

@Injectable({ -> some say it isnt needed but i ran into other issue...
  providedIn: 'root'
})
export class DataService {

  constructor(@Inject(String) private url: string, private http: HttpClient){}



  public getAll(): Observable<any> {
    return this.http.get<any>(this.url);
  }


}

home组件中的调用(自动forkjoin,暂时保持简单)

 categories: any[];
  products: any[]=[];
  filteredProducts: any[]=[];
  category: string;
  constructor(
    private route: ActivatedRoute,
    private dataService: DataService
  ) {
    this.dataService.getAll().subscribe(data=>{
      this.categories=data[0]['categories'];
      console.log(this.categories)
    })
    this.dataService.getAll().subscribe(data=>{
      this.products=data[0]['products'];
      console.log(this.products)
    })

类别服务(产品相同)

@Injectable({
  providedIn: 'root'
})
export class CategoriesService extends DataService {

  constructor(http: HttpClient) { 
    super('assets/categories.json', http)
  }
}

我在Angular.JSON中也有"aot": false,

EDIT2:应用程序。 module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HomeComponent } from './components/home/home.component';
import { HttpClient, HttpClientModule } from '@angular/common/http';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { DataService } from './services/data.service';
import { CategoriesService } from './services/categories.service';
import { ProductsService } from './services/products.service';

@NgModule({
  declarations: [
    AppComponent,
    HomeComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    AppRoutingModule,
    BrowserAnimationsModule,

  ],
  providers: [HttpClient, DataService, CategoriesService, ProductsService],
  bootstrap: [AppComponent]
})
export class AppModule { }

已更新的错误:)

Uncaught (in promise): NullInjectorError: R3InjectorError(AppModule)[DataService -> String -> String -> String]: 
  NullInjectorError: No provider for String!

编辑3:现在我可以使用stackblitz了,但是没有我想要的父服务或类继承。该代码也属于@Michael D

1 个答案:

答案 0 :(得分:0)

您以providers数组的形式放置了模块,而不是放置模块的地方(使用imports)。

imports数组内部的服务相同(仅将数组导入模块),这是提供错误的原因。

也:

@Inject(String) private url: string

此代码意味着,要创建DataService实例,您需要使用String令牌/类型从DI中获取值。没有此提供程序,您将无法创建服务实例。