Vuejs:component动态组件的使用示例

简介: Vuejs:component动态组件的使用示例

image.png

文档

component动态组件很适合在不同组件之间切换,相比v-if判断,要优雅得多

项目结构

$ tree -I node_modules
.
├── package.json
└── src
    ├── App.vue
    ├── components
    │   ├── ComponentA.vue
    │   ├── ComponentB.vue
    │   └── ComponentC.vue
    └── main.js

依赖 package.json

{
  "scripts": {
    "dev": "vue-cli-service serve",
    "build": "vue-cli-service build"
  },
  "dependencies": {
    "vue": "^2.6.14"
  },
  "devDependencies": {
    "@vue/cli-service": "^5.0.8",
    "vue-template-compiler": "^2.6.14",
    "less": "^4.0.0",
    "less-loader": "^8.0.0"
  }
}

入口文件 main.js

import Vue from "vue";
import App from "./App.vue";
new Vue({
  el: "#app",
  render: (h) => h(App),
});

组件 ComponentA.vue

<template>
  <div class="">ComponentA</div>
</template>
<script>
// created at 2023-03-31
export default {
  name: "ComponentA",
};
</script>
<style lang="less"></style>

组件 ComponentB.vue

<template>
  <div class="">ComponentA</div>
</template>
<script>
// created at 2023-03-31
export default {
  name: "ComponentA",
};
</script>
<style lang="less"></style>

组件 ComponentC.vue

<template>
  <div class="">ComponentC</div>
</template>
<script>
// created at 2023-03-31
export default {
  name: "ComponentC",
};
</script>
<style lang="less"></style>

App.vue

<template>
  <div class="app-container">
    <h2>Vue.js component动态组件示例</h2>
    <!-- tabs -->
    <div class="tabs">
      <div
        v-for="item in componentConfigs"
        class="tab-item"
        :class="{ 'tab-item--active': item.value == tab }"
        @click="handleTabClick(item)"
      >
        {{ item.label }}
      </div>
    </div>
    <!-- content -->
    <div class="content">
      <component v-bind:is="currentComponent"></component>
    </div>
  </div>
</template>
<script>
// created at 2022-12-26
import ComponentA from "./components/ComponentA.vue";
import ComponentB from "./components/ComponentB.vue";
import ComponentC from "./components/ComponentC.vue";
const componentConfigs = [
  {
    component: ComponentA,
    value: "A",
    label: "组件 A",
  },
  {
    component: ComponentB,
    value: "B",
    label: "组件 B",
  },
  {
    component: ComponentC,
    value: "C",
    label: "组件 C",
  },
];
export default {
  name: "App",
  data() {
    return {
      tab: "A",
      componentConfigs,
    };
  },
  computed: {
    currentComponent() {
      return componentConfigs.find((item) => item.value == this.tab).component;
    },
  },
  methods: {
    handleTabClick(item) {
      this.tab = item.value;
    },
  },
  created() {},
};
</script>
<style lang="less">
body {
  display: flex;
  justify-content: center;
}
.app-container {
  text-align: center;
}
.tabs {
  display: flex;
  justify-content: center;
  border: 1px solid #808080;
  border-bottom: none;
}
.tab-item {
  width: 200px;
  line-height: 40px;
  text-align: center;
  background-color: #808080;
  color: #fff;
  cursor: pointer;
}
.tab-item--active {
  background-color: #fff;
  color: #000;
}
.content {
  line-height: 500px;
  text-align: center;
  border: 1px solid #808080;
}
</style>

完整代码:https://github.com/mouday/vue-demo/tree/main/packages/vue-component

在线示例:https://mouday.github.io/vue-demo/packages/vue-component/dist/index.html


相关文章
|
17天前
|
资源调度 JavaScript API
vue3封装城市联动组件
vue3封装城市联动组件
|
20天前
|
JavaScript 前端开发 开发者
哇塞!Vue.js 与 Web Components 携手,掀起前端组件复用风暴,震撼你的开发世界!
【8月更文挑战第30天】这段内容介绍了Vue.js和Web Components在前端开发中的优势及二者结合的可能性。Vue.js提供高效简洁的组件化开发,单个组件包含模板、脚本和样式,方便构建复杂用户界面。Web Components作为新兴技术标准,利用自定义元素、Shadow DOM等技术创建封装性强的自定义HTML元素,实现跨框架复用。结合二者,不仅增强了Web Components的逻辑和交互功能,还实现了Vue.js组件在不同框架中的复用,提高了开发效率和可维护性。未来前端开发中,这种结合将大有可为。
61 0
|
21天前
|
JavaScript 开发者
[译] 监听第三方 Vue 组件的生命周期钩子
[译] 监听第三方 Vue 组件的生命周期钩子
|
21天前
|
JavaScript 前端开发
[译] 复用 Vue 组件的 6 层手段
[译] 复用 Vue 组件的 6 层手段
|
21天前
|
JavaScript API
vue 批量自动引入并注册组件或路由
vue 批量自动引入并注册组件或路由
|
30天前
|
JavaScript
Vue学习之--------组件自定义事件(绑定、解绑)(2022/8/21)
这篇文章介绍了Vue中组件自定义事件的绑定和解绑方法,通过代码实例详细说明了在父子组件间如何传递数据,包括使用`$emit`触发事件、`$on`和`$off`绑定和解绑事件,以及在实际项目中的应用和调试技巧。
Vue学习之--------组件自定义事件(绑定、解绑)(2022/8/21)
|
29天前
|
JavaScript 程序员 开发者
vue组件的使用与基础知识你真的完全明白吗?
【8月更文挑战第16天】vue组件的使用与基础知识你真的完全明白吗?
33 3
vue组件的使用与基础知识你真的完全明白吗?
|
17天前
|
存储 JavaScript
vue组件的五种传值方法(父子\兄弟\跨组件)
vue组件的五种传值方法(父子\兄弟\跨组件)
|
21天前
|
JavaScript 前端开发 Serverless
[译] VueJS 中更好的组件组合方式
[译] VueJS 中更好的组件组合方式
|
21天前
|
JavaScript
Vue3相关组件项目依赖依赖版本信息
这篇文章展示了一个Vue 3项目中使用的组件和库的依赖版本信息,包括`ant-design-vue`、`swiper`、`vue`、`vue-router`等,以及开发依赖如`vite`、`vue-tsc`、`eslint`等。
Vue3相关组件项目依赖依赖版本信息