【发布时间】:2016-06-18 04:30:53
【问题描述】:
我有一个自定义组件,可以将对象传递给该组件的视图模型。这个使用打字稿。对象到达构造函数,但通过敲除被包装在匿名对象中。
有没有办法防止这种情况,只获取强类型值对象?
在构造函数中我得到这个:
Object { value: Order, $raw: Object }
我想立即获得价值/订单。
这是我的代码:
index.html:
<div data-bind="foreach: Orders"/>
<my-component params="value: $data"></my-component>
</div>
component.html:
<span data-bind="text: Name"></span>
组件.ts
ko.components.register('my-component', {
viewModel: ComponentVm,
template: component.html"
});
组件.ts:
class ComponentVm{
public Name:string;
constructor(order:Order){
this.Name = order.Name; //Problem here because of anonymous object
}
}
订单.ts:
Class Order {
public Name:string = "Bob";
constructor(){}
}
一种解决方案是:
class ComponentVm{
public Name:string;
constructor(anonymousObject:any){
let order = anonymousObject.value;
this.Name = order.Name;
}
}
但这不是很好。
【问题讨论】:
标签: knockout.js typescript components