您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

vue.js动态组件问题

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;
}

}
})

vue.js 2022/2/8 20:50:58 有535人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶