1 ko.components.register( 'component-name' , { 2 viewModel : something… , 3 template : something… 4 });
- viewmodel的构建可以有多种方式
1 var shareViewModelInstance = {...}; 2 ko.components.register( 'my-component' , { 3 viewModel : { instance : shareViewModelInstance }, 4 template : ... 5 });
1 ko.components.register(' my-component' , { 2 viewModel: { 3 createViewModel: function(params, componentInfo){ 4 return new MyViewModel( params ); 5 } 6 }, 7 template: ... 8 });
1 ko.components.register( 2 viewModel: { require: 'some/module/name' }, 3 template: ... 4 )
1 define( ['knockout'], function(ko){ 2 function MyViewModel(){ 3 ... 4 } 5 return MyViewModel; 6 // return { instance: new MyViewModel()}; 7 //... 8 });
- template的构建方式
1 <template id='component-template'> 2 <h1>...</h1> 3 </template>
1 ko.components.register('my-component',{ 2 template: {element: 'component-template'}, 3 viewModel: ... 4 });
template:{ require: '/some/template'} template:{ require: 'text!path/file.html'}
- 设定component的同步或异步加载方式
synchronous: true
默认为false,异步加载