2016-09-17 89 views
2

我最近已经迁移到Angular 2 RC6,现在到2.0.0。Angular 2:没有供应商(2.0.0.0)

我有一个模块是从另一个模块注入模块。

模块声明代码:

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) { 


    } 
} 

导入模块(单词数)码:

import { NgModule } from '@angular/core'; 
import { CommonModule } from '@angular/common'; 
import { BrowserModule } from '@angular/platform-browser'; 

import {TestService } from "./Services/TestService"; 

@NgModule({ 
    imports: [BrowserModule], 
    declarations: [], 
    bootstrap: [], 
    providers: [{ provide: TestService, useClass: TestService }] 
}) 
export class Module2 { } 

和服务代码:

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

@Injectable() 
export class TestService { 
    getText(): string { 
     return "texxxt"; 
    } 
} 

我得到了错误:“没有TestService的提供者”。知道它在RC5中正确工作。

我也注意到TestService.js加载两次。

那么,这是怎么回事?

谢谢

回答

6

我认为经过几天的努力,我找到了答案!

这是关于进口的大写字母。

所以,当我写打字稿:

import {AdminHomeComponent} from "./components/admin-home.component"; 
import {AdminHomeComponent} from "./Components/admin-home.component"; 

角2将这种情况视为两个独立的文件,并触发二送查询加载文件!

之后的后果非常奇怪,例如组件未被识别为模块的组成部分,以及着名的消息指出没有给定服务的提供者。

谢谢大家!

+0

大声笑,你需要更好的IDE /编辑器来自动检查那些:D –

+0

我使用VS 2015和VS代码,并且这两种工具都没有任何建议 – mostefaiamine

+0

你使用的是webpack和osx吗?这将是一个问题。 –

0

我测试了你的代码,它正在工作。

我也刚刚更新我的测试设置从RC6到2.0.0。我确实遇到类型和软件包的问题。我下面来解决:

  • 检查package.json为Angular2包版本
  • 删除node_modulestypings目录
  • 运行npm installtypings install
+0

它有一个非常随机的行为。它首先运行正常,然后当我添加其他服务时,它不再识别提供的服务。我认为它处理文件夹结构。 – mostefaiamine

0

对于服务,你应该注入服务为主要AdminAppMoudle。 这种方式将在所有功能模块之间共享。如果您想拥有共同服务指令管道一个单独的层,你应该SharedModule功能在Angular2可继续。

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 { 
} 
+0

不工作,仍然加载testservice两次! – mostefaiamine