vue.js动态组件问题

image.png
试验网址:https://jsbin.com/walolaqiju/...,js,console,output

代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div id="toReplace">

<component :is="currentComponent" v-bind="componentProps"></component>
<div v-show="!currentComponent" v-for="component in componentsArray">
  <button @click="swapComponent(component)">{{component}}</button>
</div>

</div>

<button @click="swapComponent(null)">Close</button>

</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.16/vue.js"></script>

</body>
</html>

new Vue({
el: 'body',
data: {

currentComponent: null,
componentsArray: ['foo', 'bar'],
name:"fffff"

},
components: {

'foo': {
  props: ["cname"],
  template: '<h1>{{cname}}</h1>'
},
'bar': {
  template: '<h1>Bar component</h1>'
}

},
computed:{

  componentProps() {
  if (this.currentComponent === 'foo') {
    return {
      cname: this.name
    };
  }
  return {}; // return empty object
}

},
methods: {

swapComponent: function(component)
{
  this.currentComponent = component;
}

}
})

阅读 849
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题