Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度2:没有(2.0.0.0)的提供程序_Angular_Module - Fatal编程技术网

Angular 角度2:没有(2.0.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

我最近迁移到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 } 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安装

对于单例服务,您应该将服务注入主AdminAppMoude。 这种方式将在所有功能模块之间共享。如果您想为公共服务、指令和管道创建一个单独的层,那么应该继续使用Angular2中提供的SharedModule功能

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";