Detail the markRaw example in vue3
In Vue 3, markRaw is a function that tells Vue's responsiveness system not to transform an object or track its responsiveness. When you have an object and you're sure you don't need it to be a responsive object, you can mark it using markRaw. This is useful in situations where you need to integrate a third-party library or plug-in, and some parts of the library or plug-in do not need to be responsive.
Here is a detailed example of how to use markRaw in Vue 3:
import { reactive, markRaw, toRefs } from 'vue'; // Create a plain JavaScript Object const rawObject = {name: 'Raw Object', value: 'This is a raw object and it will not be reactive.' }; // Mark the object with markRaw, leaving it as it is const markedRawObject = markRaw(rawObject); // Create a reactive object const state = reactive({// Put the marked rawObject into the reactive object markedRawObject, // another normal object, It will be converted to a Reactive Object reactiveObject: {name: 'Reactive Object', value: 'This is a reactive object and it will track changes.' } }); // Use these objects in the component export default {setup() {// Use toRefs to deconstruct the properties of the responsive object, To use const {markedRawObject, reactiveObject} = toRefs(state) directly in the template; / / as a result of markedRawObject was markRaw tagged, modify it will not trigger a Vue markedRawObject responsive system. The value. The name = 'Modified Raw Object'; / / it won't trigger/update/modify reactiveObject will trigger a Vue responsiveness to update reactiveObject. Value. The name = 'Modified Reactive Object'; // This triggers an update // to return these properties for use in the template with return {markedRawObject, reactiveObject}; }};
In the above code, we create a plain JavaScript object, rawObject, and mark it as an as-is object using the markRaw function. We then put the tagged object into a responsive object state. In the component's setup function, we deconstruct markedRawObject and reactiveObject and try to modify their properties. Since markedRawObject is marked by markRaw, modifying its properties does not trigger Vue's responsive system. Modifying the properties of the reactiveObject triggers a responsive update.
In a template, you can use these properties like this:
<template> <div> <p>Marked Raw Object: {{ markedRawObject.name }} - {{ markedRawObject.value }}</p> <p>Reactive Object: {{ reactiveObject.name }} - {{ reactiveObject.value }}</p> </div> </template>
Note that even if markedRawObject's properties are displayed in the template, because it is an as-is object, modifying its properties does not trigger an update of the view. The modification of the reactiveObject property triggers the update of the view.
The primary use of markRaw is to avoid unnecessary performance overhead and potential errors when you determine that an object does not need responsiveness. This is especially useful in scenarios where you are working with third-party libraries, large data objects, or performance sensitive.
To this article on vue3 markRaw example detailed explanation of the article is introduced to this, more related to vue3 markRaw content please search the script home previous articles or continue to browse the following related articles hope that you will support the script home in the future!
Related article
-
Vue Project Packaging Optimization Practice Guide (Recommended!)
If there are many libraries introduced, the vvendor.js file volume will be quite large, affecting the first experience, this article mainly introduces the relevant information about the Vue project packaging optimization practice, and the friends who need it can refer to it2022-06-06 -
Details about vue2.0 project release online operation procedure
This article mainly introduces a detailed explanation of the vue2.0 project online operation steps, Xiaobian feel very good, now share with you, but also give you a reference. Let's take a look2018-09-09 -
In-depth understanding of Vue.js source code event mechanism
This article mainly introduces the event mechanism of Vue.js source code, Xiaobian feel very good, now share to you, also give you a reference. Let's take a look2017-09-09 -
vue mobile screen adaptation details
This article mainly introduces the vue mobile screen adaptation, the article through the example code is very detailed, for everyone's study or work has a certain reference learning value, the need of friends below with the small series to study together2019-04-04 -
element-ui table row Click to get the row index and use the index to change the row order
This article mainly introduces the element-ui table row click to get the row index (index) and use the index to change the row order, the article through the example code introduction is very detailed, for everyone's study or work has a certain reference learning value, need friends to learn together with the small series below2020-02-02 -
Why isn't there a solution for dist when a vue project is packaged and committed to git
This article mainly introduces why there is no dist file solution after the vue project is packaged and submitted to git. This article gives you a very detailed introduction, which has certain reference value for your study or work. Friends in need can refer to it2020-09-09 -
Vue implements dynamic style in a variety of ways summary
This article will introduce you to Vue to achieve a variety of methods of dynamic style, the following to bring you a few cases, need friends can learn from the study.2021-06-06 -
How does the for loop in vue.js implement synchronous execution of asynchronous methods
This article mainly introduces the vue.js for loop in how to achieve asynchronous method synchronous execution problem, has a good reference value, I hope to help you, if there are mistakes or not considered completely, please give me advice2024-02-02 -
vue-axios requests multiple interfaces at the same time until all interfaces are loaded
This article mainly introduces vue-axios requests multiple interfaces at the same time and other reprocessing operations when all interfaces are loaded, which has a good reference value, and I hope it will be helpful to you. Let's take a look2020-11-11
Latest comments