Angular 6-无法解析AppComponent的所有参数 [英] Angular 6 - can't resolve all parameters for AppComponent

查看:403
本文介绍了Angular 6-无法解析AppComponent的所有参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Angular 6构建应用程序,但我仍在设置所有内容.但是似乎我的应用程序中的依赖项注入有问题.

I am trying to build an application with Angular 6 and I am still setting everything up. But it seems there is something wrong with the dependency injection in my app.

它无法解析任何构造函数参数.它们都导致Uncaught Error: Can't resolve all parameters for AppComponent: (?)..即使是自定义服务也会导致相同的错误.

It cannot resolve any constructor parameter. They all result in Uncaught Error: Can't resolve all parameters for AppComponent: (?).. Even a custom service results in the same error.

版本 (省略了对此没有任何影响的依赖项)

 "dependencies": {
    "@angular/common": "6.0.5",
    "@angular/compiler": "6.0.5",
    "@angular/core": "6.0.5",
    "@angular/forms": "6.0.5",
    "@angular/http": "6.0.5",
    "@angular/platform-browser": "6.0.5",
    "@angular/platform-browser-dynamic": "6.0.5",
    "@angular/router": "6.0.5",
    "core-js": "2.5.7",
    "reflect-metadata": "0.1.12",
    "rxjs": "6.2.1",
    "zone.js": "0.8.26"
  },
  "devDependencies": {
    "@angular/compiler-cli": "6.0.5",
    "@ngtools/webpack": "6.0.8",
    "angular2-template-loader": "0.6.2",
    "awesome-typescript-loader": "5.1.0",
    "typescript": "2.7.2",
    "webpack": "4.12.0",
    "webpack-cli": "3.0.8",
    "webpack-dev-server": "3.1.4",
  }


app.module.ts

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {
}


TestService.ts

import {Injectable} from "@angular/core";

@Injectable({
  providedIn: 'root'
})
export class TestService {

  constructor() {
    console.warn("It works!");
  }

  public sayHello(): string {
    return "hello world!";
  }
}


App.component.ts

import {Component} from '@angular/core';
import {TestService} from "./TestService";

@Component({
  selector: 'sh-home',
  styleUrls: ['./home.scss'],
  templateUrl: './home.html'
})
export class HomeComponent {

  constructor(testService: TestService) {
    testService.sayHello();
  }
}

在这种情况下,注入TestService会导致错误

The injection of the TestService gives the error in this case

Main.ts

import {enableProdMode} from '@angular/core';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';

import {AppModule} from './app/app.module';
import './assets/scss/styles.global.scss'; // Import the global scss files

// Polyfills
import './Polyfills';

if (process.env.NODE_ENV === 'production') {
    enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule);


Polyfills.ts

/** IE9, IE10 and IE11 requires all of the following polyfills. **/
import 'core-js/es6/symbol';
import 'core-js/es6/object';
import 'core-js/es6/function';
import 'core-js/es6/parse-int';
import 'core-js/es6/parse-float';
import 'core-js/es6/number';
import 'core-js/es6/math';
import 'core-js/es6/string';
import 'core-js/es6/date';
import 'core-js/es6/array';
import 'core-js/es6/regexp';
import 'core-js/es6/map';
import 'core-js/es6/weak-map';
import 'core-js/es6/set';

/** IE10 and IE11 requires the following for NgClass support on SVG elements */
import 'classlist.js';  // Run `npm install --save classlist.js`.

/** IE10 and IE11 requires the following for the Reflect API. */
import 'core-js/es6/reflect';


/** Evergreen browsers require these. **/
// Used for reflect-metadata in JIT. If you use AOT (and only Angular decorators), you can remove.
import 'core-js/es7/reflect';

/***************************************************************************************************
 * Zone JS is required by default for Angular itself.
 */
import 'zone.js/dist/zone';  // Included with Angular CLI.

我没有使用CLI,而是一个自定义的启动程序.我包括了与Angular-CLI相同的所有polyfill,因此我没有任何遗漏.

I am not using the CLI, but a custom starter. I included all the same polyfills as Angular-CLI does, so I am not missing any.

有人看到我在做什么错吗?

Does anyone see what I am doing wrong?

更新

我简化了测试用例,并注意到它不是翻译模块.即使创建简单的服务,也无法使用依赖项注入.将服务添加到providers列表也不起作用(也不必这样做,因为我使用的是Angular 6'provideIn: root').

I've simplified the test case and noticed that it is not the translation module. Even if I create a simple service I cannot use dependency injection. Adding the service to the providers list doesn't work either (and also shouldn't be necessary, since I am using the Angular 6 'provideIn: root').

推荐答案

请确保您在polyfills中进行了以下导入
导入'core-js/es7/reflect';

make sure you have following import in polyfills
import 'core-js/es7/reflect';

这篇关于Angular 6-无法解析AppComponent的所有参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆