El *_*elo 4 slot vue.js computed-properties vuejs2
最近我发现我不能在组件槽中使用计算属性或数据属性.即使计算在组件中定义,我也无法在组件的插槽中使用它.有没有办法让它运作?
示例代码:
Vue.component('test-component', {
template: '<div><slot></slot></div>',
computed: {
my_computed: function(){
return 2+3; // not defined in slot
}
}
})
<div id="app">
<test-component>
<span v-text="my_computed"></span>
</test-component>
</div>
Run Code Online (Sandbox Code Playgroud)
在这里查看实例, https://jsfiddle.net/gu9jh4n0/1/
您可以使用Scoped Slot来实现这一目标.
在您的示例中,您的组件将如下所示:
Vue.component('test-component', {
template: '<div><slot :computed="my_computed"></slot></div>',
computed: {
my_computed: function(){
return 2+3; // not defined in slot
}
}
});
Run Code Online (Sandbox Code Playgroud)
主模板将检索插槽的范围并使用计算的:
<test-component>
<span slot-scope="data" v-text="data.computed"></span>
</test-component>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2084 次 |
| 最近记录: |