Angular 角度2:没有(2.0.0.0)的提供程序
我最近迁移到Angular 2 RC6,现在迁移到2.0.0 我有一个模块正在从另一个模块注入模块 模块声明代码:Angular 角度2:没有(2.0.0.0)的提供程序,angular,module,Angular,Module,我最近迁移到Angular 2 RC6,现在迁移到2.0.0 我有一个模块正在从另一个模块注入模块 模块声明代码: import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AdminAppComponent } from './Components/admin-app.component'; import { Module2 } f
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AdminAppComponent } from './Components/admin-app.component';
import { Module2 } from "./module2/module2";
@NgModule({
imports: [BrowserModule, Module2],
declarations: [AdminAppComponent],
bootstrap: [AdminAppComponent],
providers: []
})
export class AdminAppModule {
}
组件代码:
import { Component } from "@angular/core";
import { TestService } from "./../module2/services/TestService";
@Component({
selector: "yadm-app",
templateUrl: "/templates/admin/app/admin-app.tpl.html"
})
export class AdminAppComponent {
constructor(private ser: TestService) {
}
}
导入的模块(模块2)代码:
以及服务代码:
import { Injectable } from '@angular/core';
@Injectable()
export class TestService {
getText(): string {
return "texxxt";
}
}
我得到一个错误:“TestService没有提供程序”。知道它在RC5中正常工作
我还注意到TestService.js正在加载两次
那么,发生了什么事
谢谢我测试了你的代码,它正在工作 我还刚刚将测试设置从RC6更新到2.0.0。我确实遇到了打字和包装的问题。我做了以下操作来修复:
- 检查Angular2软件包版本的
package.json
- 删除
和节点\ u模块
目录键入
- 运行
和npm安装
typings安装
import { TestService } from "./../module2/services/TestService";
@NgModule({
imports: [BrowserModule, Module2],
declarations: [AdminAppComponent],
bootstrap: [AdminAppComponent],
providers: [TestService ] //<------here. This will create a singleton service. Probably now its fine If you remove its reference for other feature module.
})
export class AdminAppModule {
}
从“/./module2/services/TestService”导入{TestService};
@NGD模块({
导入:[浏览器模块,模块2],
声明:[AdminAppComponent],
引导:[AdminAppComponent],
提供者:[TestService]/我想经过几天的努力,我找到了答案
这都是关于进口商品的大写字母
所以当我用打字脚本写的时候:
import {AdminHomeComponent} from "./components/admin-home.component";
import {AdminHomeComponent} from "./Components/admin-home.component";
angular 2将其视为两个单独的文件,并触发两个get查询以加载文件
之后的结果非常奇怪,例如组件未被识别为模块的一部分,以及著名的消息称给定服务没有提供者
谢谢大家!它有一个非常随机的行为。起初它工作得很好,然后当我添加其他服务时,它不再识别提供的服务。我认为它处理的是文件夹结构。哈哈,你需要更好的ide/编辑器来自动检查这些:DI使用VS 2015和VS代码,这两个工具都没有任何建议你使用网页包吗d osx?那将是个问题。
import {AdminHomeComponent} from "./components/admin-home.component";
import {AdminHomeComponent} from "./Components/admin-home.component";