Home > Web Front-end > Vue.js > body text

How to solve the performance warning problem of vue3 using ref

王林
Release: 2023-05-13 15:10:06
forward
2364 people have browsed it

    Performance warning for vue3 using ref

    Problem

    The performance warning code for using ref is as follows

    <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>
    Copy after login

    Warning

    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 warning]: Vue received a component that has become Reactive objects. This causes unnecessary performance overhead and should be avoided by marking components with markRaw or using shallowRef instead of ref. Responded component:

    • markRaw: Marks an object so that it will never be converted to a proxy. Returns the object itself.

    • shallowRef: Create a ref that tracks its own .value changes, but does not make its value responsive.

    Solution

    I solved this problem by marking the object as shallowRef

    So instead of storing the component in your state, Store a keyed reference to it and perform a lookup against the object

    Full code

    <template>
      <div>
        <h2>带动画的Todolist</h2>
        <button
          v-for="(i,tab) in tabs"
          :key="i"
          :class="[&#39;tab-button&#39;, { 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>
    Copy after login

    vue3 ref function usage

    1. In the setup function , you can use the ref function to create a responsive data. When the data changes, Vue will automatically update the UI

    <template>
        <div>
            <h2>{{mycount}}</h2>
            <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>
    Copy after login

    The ref function can only monitor changes in basic types and cannot monitor changes in complex types (such as Object, array)

    To monitor changes in complex types, you can use the reactive function

    2. Obtain elements through the ref attribute

    vue3 requires the use of life cycle methods. When setup is executed, The elements in the template have not yet been mounted on the page, so the elements must be obtained after being 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>
    Copy after login

    The above is the detailed content of How to solve the performance warning problem of vue3 using ref. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!