javascript - Vue2.0 如何动态切换组件?
本文介绍了javascript - Vue2.0 如何动态切换组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
点击按钮切换组件 但切换不了
js:
var Rm={
template:'#aa',
}
var Wr={
template:'#bb',
}
var vm=new Vue({
el:'#app',
data:{
currentView:Rm,
picked:0
},
mounted:function(){
},
watch:{
'picked':function(val){
this.currentView=Wr
console.log(this.currentView)
}
},
})
html
<script type="text/x-template" id="rm">
<div>123</div>
<script>
<script type="text/x-template" id="wr">
<div>456</div>
<script>
<component :is="currentView"></component>
报错内容是
vue2.0.js:2611 [Vue warn]: Cannot find element: #wr
vue2.0.js:2611 [Vue warn]: You are using the runtime-only build of Vue where the template option is not available. Either pre-compile the templates into render functions, or use the compiler-included build.
(found in anonymous component - use the "name" option for better debugging messages.)
解决方案
<component :is="currentView"></component>
中currentView
是string
类型的组件名称,而不是初始化组件的options object
官方文档的动态组件例子很详细: https://vuejs.org/v2/guide/co...
这篇关于javascript - Vue2.0 如何动态切换组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文