当前位置:   article > 正文

Vue动态组件 component :is的使用_vue component :is

vue component :is

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

<component :is="componentTag"></component>
  1. data() {
  2. return {
  3. componentTag: '',
  4. }
  5. }

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

代码示范

  1. <template>
  2. <div style="padding: 30px">
  3. <button @click="change('1')">组件1</button>
  4. <button @click="change('2')">组件2</button>
  5. <button @click="change('3')">组件3</button>
  6. <component :is="componentTag"></component>
  7. </div>
  8. </template>
  9. <script>
  10. import component1 from './component1'
  11. import component2 from './component2'
  12. import component3 from './component3'
  13. export default {
  14. components: {component1, component2, component3},
  15. data() {
  16. return {
  17. componentTag: '',
  18. }
  19. },
  20. methods: {
  21. change(index) {
  22. this.componentTag = 'component' + index
  23. },
  24. }
  25. }
  26. </script>
  27. <style scoped>
  28. </style>

src/page/component1.vue

  1. <template>
  2. <div>
  3. <h3>组件1—文字</h3>
  4. <span>我爱你,中国!</span>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. name: "component1"
  10. }
  11. </script>
  12. <style scoped>
  13. </style>

src/page/component2.vue

  1. <template>
  2. <div>
  3. <h3>组件2-图片</h3>
  4. <img src="https://ss2.bdstatic.com/70cFvnSh.jpg" alt="">
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. name: "component2"
  10. }
  11. </script>
  12. <style scoped>
  13. </style>

src/page/component3.vue

  1. <template>
  2. <div>
  3. <h3>组件3—输入框</h3>
  4. <input type="text">
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. name: "component3"
  10. }
  11. </script>
  12. <style scoped>
  13. </style>

效果展示

  • 点击按钮组件1

  • 点击按钮组件2

  • 点击按钮组件3

以上原文链接:vue 动态组件【详解】component :is_朝阳39的博客-CSDN博客_component is

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

<component :is="detailComponentName" />
  1. import components from './components'
  2. export default {
  3. components: {
  4. ...components
  5. }
  6. }

src/components/index.js

  1. const ctx = require.context('./common', false, /\.vue$/)
  2. const components = {}
  3. console.log(ctx, 'ctx---打印出./common文件下(不包含子文件夹),以.vue结尾的文件')
  4. console.log(
  5. ctx.keys(),
  6. 'ctx.keys()---返回./common文件下(不包含子文件夹),以.vue结尾的文件的数组'
  7. )
  8. for (const key of ctx.keys()) {
  9. // 剥去文件名开头的 `./` 和`.vue`结尾的扩展名
  10. const module = key.replace(/^\.\//, '').replace(/\.vue$/, '')
  11. components[module] = ctx(key).default
  12. console.log(module, 'module---去掉`./`开头 和`.vue`结尾后的文件名')
  13. console.log(
  14. components[module],
  15. 'components[module]---拿到ctx文件(包括html和default)'
  16. )
  17. }
  18. console.log(components, 'components---这些ctx文件集合')
  19. export default components

此处解释该index.js文件:

require.context( directory, useSubdirectories, regExp )

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

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

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/198750
推荐阅读
相关标签
  

闽ICP备14008679号