vue3使用ref的性能警告问题如何解决

发布时间:2023-05-17 16:09:19 作者:iii
来源:亿速云 阅读:144

本篇内容主要讲解“vue3使用ref的性能警告问题如何解决”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“vue3使用ref的性能警告问题如何解决”吧!

vue3使用ref的性能警告

问题

使用 ref 的性能警告 代码如下

<template>
  <div>
    <component :is="currentTabComponent"></component>
  </div>
</template>

<script setup>

import { ref,shallowRef } from "vue";
import TodoList from "./components/TodoList.vue";
import Rate from "./components/Rate.vue";

let tabs ={
  TodoList,
  Rate
}
let currentTabComponent = ref(TodoList)
</script>
警告

runtime-core.esm-bundler.js:6591 [Vue warn]: Vue received a Component which was made a reactive object. This can lead to unnecessary performance overhead, and should be avoided by marking the component with markRaw or using shallowRef instead of ref. Component that was made reactive:
译文:
runtime-core.esm-bundler.js:6591 [Vue 警告]:Vue 收到一个组件,该组件已成为响应式对象。这会导致不必要的性能开销,应该通过使用 markRaw 标记组件或使用 shallowRef 代替 ref 来避免。被响应的组件:

解决

我通过将对象标记为shallowRef解决了这个问题

因此,不要将组件存储在您的状态中,而是存储对它的键控引用,并针对对象进行查找

完整代码

<template>
  <div>
    <h3>带动画的Todolist</h3>
    <button
      v-for="(i,tab) in tabs"
      :key="i"
      :class="['tab-button', { active: currentTabComponent === tab }]"
      @click="fn(tab)"
    >
      {{ tab }}
    </button>
    <component :is="currentTabComponent"></component>
  </div>
</template>

<script setup>

import { ref,shallowRef } from "vue";
import TodoList from "./components/TodoList.vue";
import Rate from "./components/Rate.vue";

let tabs ={
  TodoList,
  Rate
}
let currentTabComponent = shallowRef(TodoList)

function fn (tab){
  currentTabComponent.value = tabs[tab]
}
</script>

vue3 ref函数用法

1.在setup函数中,可以使用ref函数,用于创建一个响应式数据,当数据发生改变时,Vue会自动更新UI

<template>
    <div>
        <h3>{{mycount}}</h3>
        <button @click="changeMyCount">changeMyCount</button>
    </div>
</template>
 
<script>
import { ref } from "vue";
export default {
    name: "ref",
    setup(){
        const mycount = ref(2);
        const changeMyCount = ()=>{
            mycount.value = mycount.value + 2 ;
        }
        
        return {
            mycount,
            changeMyCount,
        }
    }
};
</script>

ref函数仅能监听基本类型的变化,不能监听复杂类型的变化(比如对象、数组)

监听复杂类型的变化可以使用reactive函数

2.通过ref属性获取元素

vue3需要借助生命周期方法,在setup执行时,template中的元素还没挂载到页面上,所以必须在mounted之后才能获取到元素。

<template>
    <div>
        <div ref="box"><button>hehe</button></div>
    </div>
</template>
 
<script>
import { ref } from "vue";
export default {
    name: "ref",
    setup(){
        const box = ref(null)
        onMounted(()=>{
            console.log(box.value)
        })
    }
};
</script>

到此,相信大家对“vue3使用ref的性能警告问题如何解决”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. 如何用vue3开发一个打砖块小游戏
  2. Vue3中Typescript的基本使用方法有哪些

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vue3 ref

上一篇:Vue3中setup如何添加name

下一篇:SpringBoot+Spring Security无法实现跨域如何解决

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》