【问题标题】:Uncaught Error: Can't resolve all parameters for EventsComponent (Angular)未捕获的错误:无法解析 EventsComponent (Angular) 的所有参数
【发布时间】:2020-10-07 15:48:56
【问题描述】:

谁能弄清楚为什么我的 EventService 不能注入到我的 EventsComponent 中? 我在 chrome 控制台中收到此错误: Uncaught Error: Can't resolve all parameters for EventsComponent: (?, ?).

https://github.com/andrelin/ng-fundamentals

我试过用谷歌搜索这个,并在 StackOverflow 上查看了几个不同的答案,但一切似乎都是正确的。有人建议在项目的某个地方可能存在循环依赖,但我真的找不到任何循环依赖。有人对此有什么建议吗?

我的(简化的)AppModule:

import { EventService } from './events/services/event.service';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { EventsComponent } from './events/events.component';

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

我的事件组件:

import { Component, OnInit } from '@angular/core';
import { EventService } from './services/event.service';

@Component({
  selector: 'app-events',
  templateUrl: './events.component.html',
  styleUrls: ['./events.component.css']
})
export class EventsComponent implements OnInit {
  events: any[];

  constructor(private eventService: EventService) {}

  ngOnInit() {
    this.events = this.eventService.getEvents();
  }
}

我的活动服务

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

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

  getEvents() {
    return EVENTS;
  }
}

示例事件数组

const EVENTS = [
  {
    id: 1,
    name: 'Angular Connect',
    date: '9/26/2020',
    time: '8:00 am',
    price: 599.99,
    imageUrl: '/assets/images/angualrconnect-shield.png',
    location: {
      address: '1057 DT',
      city: 'London',
      country: 'England'
    }
  },
  {
    id: 2,
    name: 'Angular Netherlands',
    date: '10/10/2021',
    time: '10:00 am',
    price: 60,
    imageUrl: '/assets/images/ng-nl.png',
    location: {
      address: 'A very long address just to make this two lines!',
      city: 'Amsterdam',
      country: 'Netherlands'
    }
  }
];

【问题讨论】:

  • events.component.html中是否有JSON管道可能导致循环依赖问题
  • 只有一个 *ngFor 来迭代 events 属性:

    Upcoming Angular Events


  • 什么时候有循环依赖注入?我下载了你的项目,它对我来说工作正常
  • 我在浏览器的控制台中收到此错误:未捕获错误:无法解析事件组件的所有参数:(?, ?)。
  • 标签: angular typescript angular2-services


    【解决方案1】:

    我不知道为什么,但是将这一行 import 'core-js/es7/reflect'; 添加到 main.ts 的顶部为我解决了这个问题。 检查demo,从angular-cli 的github issue 得到这个解决方案。

    【讨论】:

      【解决方案2】:

      尝试从提供者数组 app.module.ts 中删除 EventService 或从 .service.ts 中删除 providedIn

      【讨论】:

        【解决方案3】:

        我在 tsconfig.json 中找到并添加了 "emitDecoratorMetadata": true

        {
          "extends": "../tsconfig.app.json",
          "compilerOptions": {
            "types": [
              "node"
            ],
            "emitDecoratorMetadata": true
          },
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-08-22
          • 1970-01-01
          • 2018-12-18
          • 2018-09-21
          • 2023-03-16
          • 2019-12-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多