角度-未捕获的错误:无法解析HomeComponent的所有参数:(?,?,?,?)

时间:2018-11-04 15:39:27

标签: angular typescript

在运行角度项目时会出现错误

  

未捕获的错误:无法解析HomeComponent的所有参数:(?,?,   ?,?)。

HomeComponent有四个调用的服务。所有服务都是Injectable。从构造函数中删除所有服务后,运行正常,但添加这些服务会导致问题。

HomeComponent.ts

import { Component, OnInit } from '@angular/core';
import { PashminaModel } from '../../model/pashmina.model';
import { HomeService } from '../../services/home-service/home-service';
import { Category } from "../../enum/Enum";
import { FooterService } from "../../services/footer/footer.service";
import { NavbarService } from "../../services/navbar/navbar.service";
import { DataService } from '../../services/data-service/data.service';

@Component({
    selector: 'app-home',
    templateUrl: './home.component.html',
    styleUrls: ['./home.component.scss']
})
constructor(
        private homeService: HomeService,
        private navService: NavbarService,
        private footerService: FooterService,
        private data: DataService
    ) {
}

------
-------

这就是我在app.module.ts中实现提供者部分的方式

providers: [
        AuthorizationComponent,
        NavbarService,
        FooterService,
        LoginServiceEndpoint,
        LoginService,
        PashminaServiceEndpoint,
        PashminaService,
        ImageServiceEndpoint,
        ImageService,
        DescriptionServiceEndpoint,
        DescriptionService,
        ColorServiceEndpoint,
        ColorService,
        HomeService,
        HomeServiceEndpoint,
        AccountService,
        AccountServiceEndpoint,
        OrderServiceEndpoint,
        OrderService,
        DataService
    ],

在运行正常之前,突然出现此错误。

2 个答案:

答案 0 :(得分:2)

pollyfills.ts中,您应该已经注释了这两行:

import 'core-js/es6/reflect';
import 'core-js/es7/reflect';

取消注释。它应该可以正常工作。

答案 1 :(得分:1)

您已在提供商内部添加了 AuthorizationComponent ,从提供商中删除了所有组件,并将其添加到了 declarations

providers: [
        AuthorizationComponent,  //remove from here and add under declarations
        NavbarService,
        FooterService,
        LoginServiceEndpoint,
        LoginService,
        PashminaServiceEndpoint,
        PashminaService,
        ImageServiceEndpoint,
        ImageService,
        DescriptionServiceEndpoint,
        DescriptionService,
        ColorServiceEndpoint,
        ColorService,
        HomeService,
        HomeServiceEndpoint,
        AccountService,
        AccountServiceEndpoint,
        OrderServiceEndpoint,
        OrderService,
        DataService
 ]