-
Notifications
You must be signed in to change notification settings - Fork 101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Angular 2 DI系统中 函数forwardRef 的作用? #11
Labels
Comments
kittencup
changed the title
Angular 2 DI系统中 函数forwardRef 的作用
Angular 2 DI系统中 函数forwardRef 的作用?
Jan 14, 2016
首先来看下面代码 import {Component,Inject} from 'angular2/core';
@Component({
selector: 'app',
template: `
<h1>test</h1>
`,
})
export class App {
constructor(service:Service) {
console.log(service);
}
}
@Injectable()
class Service {
}
bootstrap(App,[Service]) 如果运行代码,会抛出一个错误 原因呢很简单。 var App = (function () {
function App(service) {
console.log(service);
}
App = __decorate([
core_1.Component({
selector: 'app',
template: "\n <h1>test</h1>\n ",
}),
__metadata('design:paramtypes', [Service])
], App);
return App;
})();
exports.App = App;
var Service = (function () {
function Service() {
}
Service = __decorate([
core_2.Injectable(),
__metadata('design:paramtypes', [])
], Service);
return Service;
})();
bootstrap(App,[Service]) class 最终被编译成 函数 并赋值给变量,变量不会被编译器提升到最顶部,所以在App里的Service是undefined。 最简单的解决方案就是,改变顺序 import {Component,Inject} from 'angular2/core';
@Injectable()
class Service {
}
@Component({
selector: 'app',
template: `
<h1>test</h1>
`,
})
export class App {
constructor(service:Service) {
console.log(service);
}
}
bootstrap(App,[Service]) 当然,程序写到后面可能越来越复杂,在最后合并的时候可能无法保证所有的顺序,那么就可以通过 import {Component,Inject,forwardRef} from 'angular2/core';
@Component({
selector: 'app',
template: `
<h1>test</h1>
`,
})
export class App {
constructor(@Inject(forwardRef(() => Service)) service) {
console.log(service);
}
}
@Injectable()
class Service {
}
bootstrap(App,[Service]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
forwardRef函数在
src/core/di/forward_ref.ts
The text was updated successfully, but these errors were encountered: