KOLint
Search…
View Models

Defining & exporting viewmodels

All ES6 exports, exporting a class or a instance of a class are supported. The exports has to be done in a TypeScript file, if not allowJs is enabled in current tsconfig.
1
// ES6 class exports
2
export class ViewModel { ... }
3
export = class ViewModel { ... }
4
export default class ViewModel { ... }
5
6
// Singleton exports
7
export = new ViewModel()
8
export default new ViewModel()
Copied!

Using viewmodels

The ko-import directive uses the same syntax as ES6, but with a different prefix. The importing of the viewmodel is separate from the declaration of the viewmodel. The ko-viewmodel directive is used to set the current viewmodel. It can also be a globally defined type/interface/class.
The ko-viewmodel directive can be used multiple times in the same view to re-define the viewmodel.
1
Class default import
2
<!-- ko-import ViewModel from './viewmodel' -->
3
<!-- ko-viewmodel ViewModel -->
4
5
Singleton typeof import
6
<!-- ko-import * as ViewModel from './viewmodel' -->
7
<!-- ko-viewmodel typeof ViewModel -->
8
9
Globally defined viewmodel
10
<!-- ko-viewmodel GobalViewModel -->
Copied!
Last modified 4mo ago