无法解析' TypeDecorator'(?)的所有参数。请确保使用@injectable对typedecarator进行decaorated

时间:2016-07-13 04:35:56

标签: dependency-injection angular inject angular2-services

在修复了如何导入添加到应用程序的文件夹后,我收到此错误。

我的应用结构

enter image description here

我的" app.ts"包括

import { bootstrap } from "@angular/platform-browser-dynamic";
import { Component,Inject } from "@angular/core";
import {JsonService} from "services";
import {Http,HTTP_PROVIDERS} from "@angular/http";
import {provide} from '@angular/core';
@Component({
  selector: 'hello-world',
  template: `
  <div>
    Hello world
  </div>
  `,
})
class HelloWorld {
  constructor(@Inject(JsonService) private service :JsonService){

  }
}

bootstrap(HelloWorld,[Http,HTTP_PROVIDERS,JsonService]);

我的服务

import {Injectable,Inject} from "@angular/core";
import {Http} from "@angular/http";


    @Injectable
    export class JsonService {
        constructor(@Inject(Http) private http: Http) {

        }

    }

我的system.js.config文件如下

 var map = {
    'app':                        'app',
    'rxjs':                       'node_modules/rxjs',
    '@angular':                   'node_modules/@angular',
    'services':                   'services',
    'models':                     'models'
  };

  // packages tells the System loader how to load when no filename and/or no extension
  var packages = {
    'rxjs': { defaultExtension: 'js' },
     'models':                       { main: 'index.js',  defaultExtension: 'js' },
     'services':                       { main: 'index.js',  defaultExtension: 'js' }
  };

  var packageNames = [
    '@angular/common',
    '@angular/compiler',
    '@angular/core',
    '@angular/http',
    '@angular/platform-browser',
    '@angular/platform-browser-dynamic',
    '@angular/router',
    '@angular/router-deprecated',
    '@angular/testing',
    '@angular/upgrade',
    'services',
    'models'
  ];

在第一次我收到404错误,我的服务在修改system.js并添加服务模型后丢失了,我收到此错误

仍然得到这个404

enter image description here

错误:无法解析&#39; TypeDecorator&#39;(?)的所有参数。确保所有参数都使用Inject进行修饰或具有有效的类型注释以及&#39; TypeDecorator&#39;用Injectable

装饰

链接到完整代码https://1drv.ms/u/s!Ai1EHDfZ96E2wij-MtwLHdCI-hVI

1 个答案:

答案 0 :(得分:0)

解决了这个错误的是JGW96在this thread中的评论,即在Angular 2中你无法将服务注入服务。我正在为一些JSON数据创建一个视图模块,并将订阅拆分为一个视图模块。一旦我将其移回主服务模块,然后将该主服务作为提供程序包含在视图组件中,解析了装饰器错误。