我如何在 Ionic2 供应商上创建和使用模块 class?
How I can create and use module class on Ionic2 providers?
我想用模块扩展我的提供者,以便像 classA.moduleClassB
那样使用,但我不能也不知道如何将模块 class 对象导入我的控制器页面作为提供者
示例(如我所愿):
providers/my-provider/my-provider.ts
export class MyParentClass {
//...
}
export module a {
class MyChildClass {
//...
}
}
pages/home/home.ts
import { Component } from '@angular/core';
import { MyParentClass, MyChildClass } from '../../providers/my-provider'
@Component({
selector: 'page-home',
templateUrl: 'home.html',
providers: [
MyParentClass,
MyChildClass
]
});
export class homePage {
constructor(public parentClass: MyParentClass, public childClass: MyChildClass) {
}
}
您只能注入标记为可注入的 classes。将您的 classB 标记为 Injectable ,然后像您一样将其注入构造函数。
为了使用正确的依赖注入,将访问修饰符更改为私有。然后,您可以在 classA 中访问您的 classB。
收藏
我想用模块扩展我的提供者以像 classA.moduleClassB 那样使用,但我不能也不知道如何将模块 class object 作为提供者[=12= 导入到我的控制器页面]
应该看起来像这样:
providers/my-provider/my-provider.ts
import {Injectable} from "@angular/core";
@Injectable()
export class MyParentClass {
//...
}
export module a {
@Injectable()
export class MyChildClass {
//...
}
}
pages/home/home.ts
import { Component } from '@angular/core';
import { MyParentClass, MyChildClass } from '../../providers/my-provider'
@Component({
selector: 'page-home',
templateUrl: 'home.html',
providers: [
MyParentClass,
MyChildClass
]
});
export class homePage {
constructor(private parentClass: MyParentClass, private childClass: MyChildClass) {
}
}
我想用模块扩展我的提供者,以便像 classA.moduleClassB
那样使用,但我不能也不知道如何将模块 class 对象导入我的控制器页面作为提供者
示例(如我所愿):
providers/my-provider/my-provider.ts
export class MyParentClass {
//...
}
export module a {
class MyChildClass {
//...
}
}
pages/home/home.ts
import { Component } from '@angular/core';
import { MyParentClass, MyChildClass } from '../../providers/my-provider'
@Component({
selector: 'page-home',
templateUrl: 'home.html',
providers: [
MyParentClass,
MyChildClass
]
});
export class homePage {
constructor(public parentClass: MyParentClass, public childClass: MyChildClass) {
}
}
您只能注入标记为可注入的 classes。将您的 classB 标记为 Injectable ,然后像您一样将其注入构造函数。
为了使用正确的依赖注入,将访问修饰符更改为私有。然后,您可以在 classA 中访问您的 classB。
收藏 我想用模块扩展我的提供者以像 classA.moduleClassB 那样使用,但我不能也不知道如何将模块 class object 作为提供者[=12= 导入到我的控制器页面]
应该看起来像这样:
providers/my-provider/my-provider.ts
import {Injectable} from "@angular/core";
@Injectable()
export class MyParentClass {
//...
}
export module a {
@Injectable()
export class MyChildClass {
//...
}
}
pages/home/home.ts
import { Component } from '@angular/core';
import { MyParentClass, MyChildClass } from '../../providers/my-provider'
@Component({
selector: 'page-home',
templateUrl: 'home.html',
providers: [
MyParentClass,
MyChildClass
]
});
export class homePage {
constructor(private parentClass: MyParentClass, private childClass: MyChildClass) {
}
}