037.Vue3入门,动态组件

1、App.vue代码如下:

复制代码
<template>
  <component :is="tabComponent"></component>
  <button @click="change">切换组件</button>
</template>

<script>
import Child1 from "./view/Child1.vue"
import Child2 from "./view/Child2.vue"

export default {
  data() {
    return {
      tabComponent: "Child1"
    }
  },
  components: {
    Child1,
    Child2
  },
  methods: {
    change() {
      console.log('change', this.tabComponent == "Child1")
      this.tabComponent = this.tabComponent == "Child1" ? "Child2" : "Child1";
    }
  }
}
</script>
复制代码

2、Child1.vue代码如下;

复制代码
<template>
  <div>我是子页面1</div>
</template>

<script>
export default {
  beforeCreate() { console.log('beforeCreate') },
  created() { console.log('created') },
  beforeMount() { console.log('beforeMount') },
  beforeUpdate() { console.log('beforeUpdate') },
  beforeDestroy() { console.log('beforeDestroy') },
  mounted() { console.log('mounted') },
  updated() { console.log('updated') },
  destroyed() { console.log('destroyed') },
  methods: { change() { console.log('change') }
  }
}
</script>
复制代码

3、Child.vue代码如下:

复制代码
<template>
  <div>我是子页面2</div>
</template>

<script>
export default {
  beforeCreate() { console.log('beforeCreate') },
  created() { console.log('created') },
  beforeMount() { console.log('beforeMount') },
  beforeUpdate() { console.log('beforeUpdate') },
  beforeDestroy() { console.log('beforeDestroy') },
  mounted() { console.log('mounted') },
  updated() { console.log('updated') },
  destroyed() { console.log('destroyed') },
  methods: { change() { console.log('change') }
  }
}
</script>
复制代码

4、效果如下:

 

posted @   像一棵海草海草海草  阅读(71)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2019-08-11 九、航材_航材申请和库房发料
2019-08-11 八、航材_MCC工作指令下发与航材保障
2019-08-11 七、航材_SAP中查看所有件号清单
2019-08-11 六、航材_查看件号修改记录
2019-08-11 五、航材_件号冻结
2019-08-11 四、航材_互换件_添加单向互换
2019-08-11 三、航材_互换件_删除完全互换
点击右上角即可分享
微信分享提示