Vue动态组件 component :is的使用代码示范

 更新时间:2023年09月19日 11:33:27   作者:Ynnelf  
vue 动态组件用于实现在指定位置上,动态加载不同的组件,这篇文章主要介绍了Vue动态组件 component :is的使用,需要的朋友可以参考下

vue 动态组件用于实现在指定位置上,动态加载不同的组件,核心代码为:

<component :is="componentTag"></component>
data() {
    return {
        componentTag: '',
    }
}

componentTag 为自定义的变量,将需要加载的组件名赋值给它,即可在<component />标签出现的位置,渲染该组件。

代码示范

<template>
    <div style="padding: 30px">
        <button @click="change('1')">组件1</button>
        <button @click="change('2')">组件2</button>
        <button @click="change('3')">组件3</button>
        <component :is="componentTag"></component>
    </div>
</template>
<script>
    import component1 from './component1'
    import component2 from './component2'
    import component3 from './component3'
    export default {
        components: {component1, component2, component3},
        data() {
            return {
                componentTag: '',
            }
        },
        methods: {
            change(index) {
                this.componentTag = 'component' + index
            },
        }
    }
</script>
<style scoped>
</style>

src/page/component1.vue

<template>
    <div>
        <h3>组件1—文字</h3>
        <span>我爱你,中国!</span>
    </div>
</template>
<script>
    export default {
        name: "component1"
    }
</script>
<style scoped>
</style>

src/page/component2.vue

<template>
    <div>
        <h3>组件2-图片</h3>
        <img src="https://ss2.bdstatic.com/70cFvnSh.jpg" alt="">
    </div>
</template>
<script>
    export default {
        name: "component2"
    }
</script>
<style scoped>
</style>

src/page/component3.vue

<template>
    <div>
        <h3>组件3—输入框</h3>
        <input type="text">
    </div>
</template>
<script>
    export default {
        name: "component3"
    }
</script>
<style scoped>
</style>

效果展示

点击按钮组件1

点击按钮组件2

点击按钮组件3

以上内容转自:vue 动态组件【详解】component :is

 component :is用法进阶之组件内引入多个组件

<component :is="detailComponentName" />
import components from './components'
export default {
    components: {
        ...components
    }
}

src/components/index.js

const ctx = require.context('./common', false, /\.vue$/)
const components = {}
console.log(ctx, 'ctx---打印出./common文件下(不包含子文件夹),以.vue结尾的文件')
console.log(
  ctx.keys(),
  'ctx.keys()---返回./common文件下(不包含子文件夹),以.vue结尾的文件的数组'
)
for (const key of ctx.keys()) {
  // 剥去文件名开头的 `./` 和`.vue`结尾的扩展名
  const module = key.replace(/^\.\//, '').replace(/\.vue$/, '')
  components[module] = ctx(key).default
  console.log(module, 'module---去掉`./`开头 和`.vue`结尾后的文件名')
  console.log(
    components[module],
    'components[module]---拿到ctx文件(包括html和default)'
  )
}
console.log(components, 'components---这些ctx文件集合')
export default components

此处解释该index.js文件:

require.context( directory, useSubdirectories, regExp )

  • directory{String}-读取文件的路径。
  • useSubdirectories{Boolean}-是否遍历文件的子目录。
  • regExp{RegExp}-匹配文件的正则。

require.context('./', false, /\.vue$/) 检索components文件下的文件,不检索子文件夹,匹配以.vue结尾的文件。

到此这篇关于Vue动态组件 component :is的使用的文章就介绍到这了,更多相关Vue  component :is 使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • vue.js简单配置axios的方法详解

    vue.js简单配置axios的方法详解

    axios是一个基于Promise 用于浏览器和 nodejs 的 HTTP 客户端,下面这篇文章主要给大家介绍了关于vue.js简单配置axios的相关资料,需要的朋友们可以参考借鉴,需要的朋友们下面来一起学习学习吧。
    2017-12-12
  • vue插件--仿微信小程序showModel实现模态提示窗功能

    vue插件--仿微信小程序showModel实现模态提示窗功能

    这篇文章主要介绍了vue插件--仿微信小程序showModel实现模态提示窗,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • VueJs中如何使用Teleport及组件嵌套层次结构详解

    VueJs中如何使用Teleport及组件嵌套层次结构详解

    这篇文章主要为大家介绍了VueJs中如何使用Teleport及组件嵌套层次结构详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-04-04
  • vue3+ts实际开发中该如何优雅书写vue3语法

    vue3+ts实际开发中该如何优雅书写vue3语法

    近尝试上手 Vue3+TS+Vite,所以下面这篇文章主要给大家介绍了关于vue3+ts实际开发中该如何优雅书写vue3语法的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2022-10-10
  • vue框架编辑接口页面下拉级联选择并绑定接口所属模块

    vue框架编辑接口页面下拉级联选择并绑定接口所属模块

    这篇文章主要为大家介绍了vue框架编辑接口页面实现下拉级联选择以及绑定接口所属模块,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • VeeValidate 的使用场景以及配置详解

    VeeValidate 的使用场景以及配置详解

    这篇文章主要介绍了VeeValidate 的使用场景以及配置详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • 解决vue2中使用axios http请求出现的问题

    解决vue2中使用axios http请求出现的问题

    下面小编就为大家分享一篇解决vue2中使用axios http请求出现的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-03-03
  • vue中如何防止用户频繁点击按钮详解

    vue中如何防止用户频繁点击按钮详解

    在后台使用过程中经常会因为按钮重复点击,而造成发送多次重复请求 以下方法可以避免这种情况,下面这篇文章主要给大家介绍了关于vue中如何防止用户频繁点击按钮的相关资料,需要的朋友可以参考下
    2022-09-09
  • vue实现ToDoList简单实例

    vue实现ToDoList简单实例

    这篇文章主要为大家详细介绍了vue实现ToDoList简单实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-02-02
  • Vue实现Hover功能(mouseover与mouseenter的区别及说明)

    Vue实现Hover功能(mouseover与mouseenter的区别及说明)

    这篇文章主要介绍了Vue实现Hover功能(mouseover与mouseenter的区别及说明),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-10-10

最新评论