美文网首页VUE
Vue动态组件

Vue动态组件

作者: cain07 | 来源:发表于2021-07-18 22:02 被阅读0次

Vue中提供了<component>来挂载不同组件,使用 is 选择要展示的组件

效果

image

案例

index.vue

<template>
  <div>
      <component :is="cmpName"></component> <br/>
      <button @click="changeCmp('cmpone')">第一个组件</button>
      <button @click="changeCmp('cmptwo')">第二个组件</button>
      <button @click="changeCmp('cmpthree')">第三个组件</button>
  </div>
</template>
<script>
// 导入子组件
import cmpone from './cmpone'
import cmptwo from './cmptwo '
import cmpthree from './cmpthree '
export default {
  // 注册组件
  components: {
    cmpone,
    cmptwo,
    cmpthree 
  },
  methods: {
    changeCmp(cmpName) {
      this.cmpName = cmpName
    }
  },
  data(){
    return {
      cmpName: 'cmpone' //默认显示第一个组件
    }
  }
}
</script>

cmpone.vue

<template>
  <div>这是子组件1</div>
</template>

cmptow.vue

<template>
  <div>这是子组件2</div>
</template>

cmpthree.vue

<template>
  <div>这是子组件3</div>
</template>

作者:圆梦人生
链接:https://www.jianshu.com/p/28ee8444d957
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

相关文章

  • Vue-component实现动态组件切换传值问题

    Vue-component实现动态组件切换传值问题 使用动态组件 vue提供了一个内置的 ...

  • vue component组件

    使用vue的component组件实现动态点击按钮功能:

  • 【Vue】组件 - 让动态组件存活

    【Vue】组件 -动态组件 接着上面的例子。 动态组件用标签包着,再加上is属性来判断就能实...

  • vue 组件和路由

    === Vue组件Vue组件的创建vue.extend结合vue.component创建vue.component...

  • vue.component、vue.extend、vue

    vue.component 注册全局组件 vue.extend 创建组件构造器 vue.component(组件名...

  • vue component动态组件

    一、什么是动态组件 定义:多个组件挂载到同一个组件上,通过参数动态的切换不同组件就是动态组件。 书写形式: 内置组...

  • Vue动态组件

    Vue中提供了来挂载不同组件,使用 is 选择要展示的组件 效果 案例 index.vue ...

  • vue component 动态组件

    动态组件:在不同组件之间进行动态切换 实际项目代码设计中,为了保证复用性和可维护性,是会有一些可行的方案。这里我们...

  • Vue动态组件

    Vue 动态组件 通过 Vue 的 元素加一个特殊的 is 特性来实现。currentCo...

  • vue 动态组件

    component Vue 提供了一个 component 元素,可以用来动态挂载不同的组件,使用 is 特性来切...

网友评论

    本文标题:Vue动态组件

    本文链接:https://www.haomeiwen.com/subject/klyzpltx.html