Javascript ';无法设置未定义的属性';应用绑定时出错
因此,我得到控制台错误Javascript ';无法设置未定义的属性';应用绑定时出错,javascript,knockout.js,typescript,Javascript,Knockout.js,Typescript,因此,我得到控制台错误无法设置未定义的属性“stepModels”,但typescript编译良好。我做错了什么?我似乎不明白。非常感谢您的帮助 class ViewModel { public index: number; public percentage: number; public getTemplate: any; public goNext: any; public goPrevious: any; public stepModel
无法设置未定义的属性“stepModels”
,但typescript编译良好。我做错了什么?我似乎不明白。非常感谢您的帮助
class ViewModel {
public index: number;
public percentage: number;
public getTemplate: any;
public goNext: any;
public goPrevious: any;
public stepModels: KnockoutObservable<Array<Step>>;
public currentStep: KnockoutObservable<Step>;
public currentIndex: KnockoutComputed<number>;
public currentPercentage: KnockoutComputed<number>;
public canGoNext: KnockoutComputed<boolean>;
public canGoPrevious: KnockoutComputed<boolean>;
constructor() {
let self = this;
self.stepModels = ko.observableArray([
new Step(1, 'emailTmpl'),
new Step(2, 'usernameTmpl'),
new Step(3, 'passwordTmpl'),
new Step(4, 'questionsMainTmpl'),
new Step(5, 'questionsTmpl'),
new Step(6, 'questionsFinalTmpl'),
new Step(7, 'verifyTmpl'),
new Step(8, 'successTmpl')
]);
}
}
ko.applyBindings的第一个参数应该是表示敲除视图模型的对象。由于Knockout对typescript类没有任何了解,因此在将类发送到ko.applyBindings之前,必须将类实例化到预期的对象中
从理论上讲,如果不想实例化类,可以将其本身发送进来,但随后必须将类的属性定义为静态字段,以便它们直接到达传入的对象上。尽管我认为这样做没有任何意义。似乎您没有初始化
stepModels
属性。如何实例化此视图模型?@Alexko.applyBindings(ViewModel,element)代码>@keithh。您不必这样做:ko.applyBindings(newviewmodel(),element)代码>?尝试ko.applyBindings(新的ViewModel(),元素);
ko.applyBindings(ViewModel, element);