Knockout observableArray in TypeScript

后端 未结 2 1417
春和景丽
春和景丽 2021-02-15 16:14

What is the proper way to initialize a Knockout observableArray in a TypeScript class?

I am doing something like this:

   module ViewModel { 

        de         


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-15 17:08

    This line is where your mistake is:

    agencies[i]
    

    When you access an observable array, it is actually wrapped in a function so you do access it like:

    agencies()[i]
    

    Also make yourself a favor and download the Knockout definitions from: https://github.com/borisyankov/DefinitelyTyped

    Then declare your attributes with types:

    module ViewModel {
    
        export class IndexPageViewModel {
    
            agencies: KnockoutObservableArray;
    
            constructor () {
                this.agencies = ko.observableArray([]);
            }
        }
    }
    

提交回复
热议问题