我试图在一个小项目中使用NgZone,但无法弄清楚如何将其引入.angular.io中的文档说它们适用于javascript,但似乎使用的是打字稿。
我发现的每个例子似乎都与文档一致,即typescript:
export class NgZoneDemo {
progress: number = 0;
label: string;
constructor(private _ngZone: NgZone) {}
我们不使用打字稿,我无法弄清楚如何将NgZone带入我的班级以挽救我的生命。我需要在plunkr中更改什么才能使它在不使用打字稿的项目中使用?
答案 0 :(得分:2)
您可以使用ng.core.NgZone
对象并注入组件,如下所述:
var Cmp = ng.core.
Component({
selector: 'cmp'
}).
View({
template: '<div>Test</div>'
}).
Class({
constructor: [ng.core.NgZone, function(zone) {
// Use the zone
}]
});
希望它可以帮到你, 亨利
答案 1 :(得分:1)
发现将Ngzone传递给构造函数必须先加上一个静态参数数组,列出要传递的参数。昨晚我试过这个,但没有意识到为什么或那个案例很重要。无论如何,一旦我这样做,它现在可以在构造函数中使用。
export class NgZoneDemo {
progress: number = 0;
label: string;
static parameters = [NgZone];
constructor(ngZone) {
// use here
}
...
}