【发布时间】:2016-12-14 04:25:07
【问题描述】:
我与提供者有一些困难。我尝试在组件中导入新的自定义提供程序,但它不起作用。 第二个提供程序基于我制作的第一个提供程序,并且运行良好...
这是我的提供者:
import { Injectable} from "@angular/core";
import { Router, Routes } from '@angular/router';
import ... // All components needed
@Injectable()
export class RoutesHelper {
private userRoutes: Routes = [
{ path: '' , component: HeaderComponent, outlet: 'header' },
...
];
constructor(
private router:Router
) {}
public load() {
this.router.resetConfig(this.userRoutes);
}
}
这是我的“问题组件”
import { Component, OnInit } from '@angular/core';
import { RoutesHelper } from '../_utils/routes.helper';
@Component({
selector: 'questions-list',
templateUrl: './app/question/questions.component.html',
providers: [RoutesHelper]
})
export class QuestionsComponent implements OnInit {
constructor(private routes:RoutesHelper) {}
ngOnInit() {
this.routes.load();
}
}
但我有这个错误: “QuestionsComponent”的提供者无效 - 只允许提供 Provider 和 Type 的实例,得到:[?undefined?]
我不知道为什么我得到“未定义”对象,我也没有这个错误。
感谢您的帮助。
【问题讨论】:
-
你是否在 app.module.ts 文件中包含了你的提供者?
-
是的,提供程序包含在 app;module.ts 中
-
正在导入链接并包含在提供者列表中?