Angular2中带有可选参数的依赖注入
Dependency injection with optional parameter in Angular2
我有多个组件需要相同的依赖性,而构造函数需要一个字符串。我如何告诉 angular2 为 DI 使用特定类型的实例?
例如:
ChatUsers.ts:
@Component({
selector: "chat-users"
})
@View({
directives: [],
templateUrl: '/js/components/ChatUsers.html'
})
export class ChatUsers {
constructor(public currentUser : User) {
}
}
和app.ts:
/// <reference path="../libs/typings/tsd.d.ts" />
import {Component, View, bootstrap} from 'angular2/angular2';
import {User} from "User";
// How to create a user, e.g. new User('John') and use it for DI?
@Component({
selector: 'chat-app'
})
@View({
directives: [ ],
template: `
<div> Some text
</div>`
})
class ChatApp {
constructor(public user: User) {
// do something with user
}
}
bootstrap(ChatApp, [ User ]);
User.ts
export class User {
name: string;
constructor(name: string) {
this.name = name;
}
}
如果运行这段代码,错误是:
Cannot resolve all parameters for User(?). Make sure they all have
valid type or annotations.
我使用的是最新的 angular2 版本:2.0.0-alpha.44
要使依赖项可选,只需使用 @Optional
parameter decorator (see this plunker):
class User {
name: string;
constructor(@Optional() name: string) {
this.name = name;
}
}
如果你想将 name
注入 User
你有两个解决方案:
- 将一些
'userName'
提供程序添加到应用程序提供程序并使用 @Inject('userName')
parameter decorator to inject it into User
(see this plunker)。
class User {
name: string;
constructor(@Inject('userName') name: string) {
this.name = name;
}
}
// ...
bootstrap(ChatApp, [
User,
provide('userName', { useValue: 'Bob'})
]);
- 使用
useFactory
to specifically instantiate your user (see this plunker):
bootstrap(ChatApp, [
provide(User, { useFactory: () => new User('John') })
]);
我有多个组件需要相同的依赖性,而构造函数需要一个字符串。我如何告诉 angular2 为 DI 使用特定类型的实例?
例如:
ChatUsers.ts:
@Component({
selector: "chat-users"
})
@View({
directives: [],
templateUrl: '/js/components/ChatUsers.html'
})
export class ChatUsers {
constructor(public currentUser : User) {
}
}
和app.ts:
/// <reference path="../libs/typings/tsd.d.ts" />
import {Component, View, bootstrap} from 'angular2/angular2';
import {User} from "User";
// How to create a user, e.g. new User('John') and use it for DI?
@Component({
selector: 'chat-app'
})
@View({
directives: [ ],
template: `
<div> Some text
</div>`
})
class ChatApp {
constructor(public user: User) {
// do something with user
}
}
bootstrap(ChatApp, [ User ]);
User.ts
export class User {
name: string;
constructor(name: string) {
this.name = name;
}
}
如果运行这段代码,错误是:
Cannot resolve all parameters for User(?). Make sure they all have valid type or annotations.
我使用的是最新的 angular2 版本:2.0.0-alpha.44
要使依赖项可选,只需使用 @Optional
parameter decorator (see this plunker):
class User {
name: string;
constructor(@Optional() name: string) {
this.name = name;
}
}
如果你想将 name
注入 User
你有两个解决方案:
- 将一些
'userName'
提供程序添加到应用程序提供程序并使用@Inject('userName')
parameter decorator to inject it intoUser
(see this plunker)。
class User {
name: string;
constructor(@Inject('userName') name: string) {
this.name = name;
}
}
// ...
bootstrap(ChatApp, [
User,
provide('userName', { useValue: 'Bob'})
]);
- 使用
useFactory
to specifically instantiate your user (see this plunker):
bootstrap(ChatApp, [
provide(User, { useFactory: () => new User('John') })
]);