Ionic / Angular 2依赖注入根据模式

时间:2016-11-30 03:26:48

标签: angular dependency-injection ionic2

我正在遵循:http://devdocs.io/angular~2_typescript/cookbook/dependency-injection

的依赖注入模式

我的代码如下所示:

MyService.ts

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

@Injectable()
export class MyService {}

app.component.ts

import { Component, ViewChild } from '@angular/core';
import { MyService } from '../services/MyService';

@Component({
  templateUrl: 'app.html',
  providers: [MyService]
})

export class MyApp {}

MyComponent.ts

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

@Component({
  selector: 'theComponent',
  templateUrl: 'theComponent.html'
})
export class thisComponent {
  constructor(private thisService: MyService) {}

我收到以下错误:找不到名称“MyService”

我已经检查过以确保这一点 “emitDecoratorMetadata”:true

我也尝试了公共,私人和公共/私人的额外描述,我每次都得到同样的错误。

1 个答案:

答案 0 :(得分:1)

>>> data = 'abcd' >>> [[data[i] for i in n] for n in it.permutations(range(len(data))) ... if all(n[n[i]] == i for i in n)] [['a', 'b', 'c', 'd'], ['a', 'b', 'd', 'c'], ['a', 'c', 'b', 'd'], ['a', 'd', 'c', 'b'], ['b', 'a', 'c', 'd'], ['b', 'a', 'd', 'c'], ['c', 'b', 'a', 'd'], ['c', 'd', 'a', 'b'], ['d', 'b', 'c', 'a'], ['d', 'c', 'b', 'a']] 进入您的类文件允许您使用其他文件/模块中的导出定义。您需要将该类导入到使用该类定义的每个文件中。

因此,在import中,您需要包含

  

将{MyService}从' ./ path /导入/ services / MyService';

有关在Typescript

中执行此操作的详细信息,请参阅https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#11.3.2

这与Angular2中的模块的导入属性不同,后者是依赖链和注入器。

相关问题