当前位置:首页 » 《随便一记》 » 正文

Vue动态组件 component :is的使用

5 人参与  2023年04月19日 16:22  分类 : 《随便一记》  评论

点击全文阅读


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_朝阳39的博客-CSDN博客_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结尾的文件。

 

 


点击全文阅读


本文链接:http://m.zhangshiyu.com/post/59825.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • (此去经年无故人)南初陆南城:结局+番外精品选集起点章节+阅读即将发布预订
  • 沈凝夏叶晚怡附加完整在线阅读(归雁不栖故人枝)最近更新列表
  • 剧情人物是时初,白浩雄的玄幻言情小说《召诸神,踏万界,天命帝女逆乾坤》,由网络作家&ldquo;海鸥&rdquo;所著,情节扣人心弦,本站TXT全本,欢迎阅读!本书共计381345字,185章节,:结局+番外免费品鉴:结局+番外评价五颗星
  • 凤青禾,江明远,***枢小说(别人修仙我捡漏,卷王们破防了)最近更新(凤青禾,江明远,***枢)整本无套路阅读
  • 薛梨小说无删减+后续(曾经亲情似草芥)畅享阅读
  • 沈南栀小说(穿越时空,我要修补时空裂缝)章节目录+起点章节(沈南栀)全篇清爽版在线
  • 未婚妻被巨蟒缠身,我该吃就吃该喝就喝前言+后续_阿豪林月周然后续+番外_小说后续在线阅读_无删减免费完结_
  • 陆骁,陆本初小说(陆骁,陆本初)(癫!睁眼穿成老太太挥鞭***逆子)前传+阅读全新作品预订
  • 姐姐含冤而死后冥王另娶,我杀穿整个地府在线阅读_阎罗殿殷红别提一口气完结_小说后续在线阅读_无删减免费完结_
  • (书荒必看)毒后重生:疯王的神医小娇妻沈清歌,萧绝:+后续热血十足
  • 重生后我和太监联手灭了敌国喻辰,林雪续集(重生后我和太监联手灭了敌国)终极反转(喻辰,林雪)全篇一口气阅读
  • 我不做灵媒后,自称灵媒摆渡人的养妹害怕了内容精选_苏晓霍老阿姐无广告_小说后续在线阅读_无删减免费完结_

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1