Vue3中使用component :is 加载组件

03-19 1615阅读 0评论

1.不使用setup语法糖,这种方式和vue2差不多,is可以是个字符串

Vue3中使用component :is 加载组件,Vue3中使用component :is 加载组件,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,性能,第1张
(图片来源网络,侵删)
 
  
  
  
 
  切换组件
 


  import { ref } from 'vue'
  import Child1 from './Child1.vue'
  import Child2 from './Child2.vue'
  export default {
    components: {
      Child1,
      Child2
    },
    setup() {
      let currentComp = ref('Child1')
 
      // 切换组件
      const compChange = () => {
        if(currentComp.value == 'Child1') {
          currentComp.value = 'Child2'
        }else {
          currentComp.value = 'Child1'
        }
      }
      return {
        currentComp,
        compChange
      }
    }
  }
 
 

2. 使用setup语法糖,这时候的is如果使用字符串会加载不出来,得使用组件实例

第一种方式

 
  
  
  
 
  切换组件
 


  import { ref } from 'vue'
  import Child1 from './Child1.vue'
  import Child2 from './Child2.vue'
 
  const flag = ref(true)
 

第二种方式

 
  
  
  
 
  切换组件
 


  import { shallowRef, ref } from 'vue'
  import Child1 from './Child1.vue'
  import Child2 from './Child2.vue'
 
  //这里用ref的话,vue给出警告Vue接收到一个组件,该组件被制成反应对象。这可能会导致不必要的性能开销,应该通过将组件标记为“markRaw”或使用“shallowRef”而不是“ref”来避免。
  // 如果使用 markRaw 那么currentComp将不永远不会再成为响应式对象。 所以得使用 shallowRef
  let currentComp = shallowRef(Child1)  
 
  // 切换组件
  const compChange = () => {
    if(currentComp.value == Child1) {
      currentComp.value = Child2
    }else {
      currentComp.value = Child1
    }
  }
 
Vue3中使用component :is 加载组件,Vue3中使用component :is 加载组件,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,性能,第2张
(图片来源网络,侵删)
Vue3中使用component :is 加载组件,Vue3中使用component :is 加载组件,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,性能,第3张
(图片来源网络,侵删)

免责声明
本网站所收集的部分公开资料来源于AI生成和互联网,转载的目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复: 表情:
评论列表 (暂无评论,1615人围观)

还没有评论,来说两句吧...

目录[+]