我刚刚从2.5.21更新到vue.js 2.6.0,我的某些页面停止了工作。
最终我发现问题出在以下方面:
Vue.component('foo' , {
data : function() { return { a : 42 /*this is computed dynamically in real life*/ } } ,
template : `<div><slot v-bind:a="a">Default Text</slot></div>`
});
我像这样使用它
<foo><template slot-scope="slotProps" v-if="slotProps.a==42"> Fourty two</template></foo>
基本上,我只想在a为42时使用传递的模板,否则保留默认值。
使用vue.js v 2.5.21,当a为42时我得到“四十二”,否则为“默认文本”
使用vue.js v 2.6.0时出现错误
Property or method "slotProps" is not defined on the instance but referenced during render.
是vue 2.6.0中引入的回归,还是我使用不正确,但是由于2.5.21中的错误,它正在按预期运行?
在后一种情况下,根据a的值,建议使用条件模板的方式是什么?
PS:我知道最终应该使用v插槽,但是2.6.0不应破坏向后兼容性。
最佳答案
这确实是this commit引入的回归。值得庆幸的是,大约一个小时前,它已在version 2.6.2中修复。
Vue.component('foo', {
data: function() {
return {
a: 42 /*this is computed dynamically in real life*/
}
},
template: `<div><slot v-bind:a="a">Default Text</slot></div>`
});
new Vue({
el: '#app',
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.2/vue.min.js"></script>
<div id="app">
<foo><template slot-scope="slotProps" v-if="slotProps.a==42"> Fourty two</template></foo>
</div>