SoFunction
Updated on 2025-04-05

Vue uses onMounted to ensure that asynchronous operations are performed after component mounts. Detailed explanation of the example

Vue uses onMounted to ensure asynchronous operations are performed after the component is mounted

In or other similar frameworks, useonMountedThis is to ensure that asynchronous operations are performed after the component is mounted.

Component's life cycle hook functiononMountedIs a hook function executed after the component is mounted to the DOM. Performing asynchronous operations in this hook function ensures that the component has been fully rendered and that the DOM element already exists, thus avoiding problems caused by performing asynchronous operations when the component is not mounted or has not been fully initialized.

Here is an example to illustrate whyonMountedPerform asynchronous operations:

import { onMounted } from 'vue';
export default {
  setup() {
    onMounted(async () => {
      // Asynchronous operation      await fetchData();
    });
  }
}

In the above code, the Composition API from Vue 3 is usedonMountedHook function. BysetupCalled in a functiononMounted, asynchronous operations can be bound to the component to be mounted and executed.

If the asynchronous operation is not placedonMountedbut directlysetupExecute in a function, the following problems may occur:

  • The component is not fully mounted: in the componentsetupDuring the phase, the DOM element of the component may not be fully created or rendered. If an asynchronous operation is performed at this stage, the correct DOM element may not be obtained or the expected operation may be performed.
  • Dependencies for asynchronous operations are not available: in the componentsetupDuring the phase, the data that some asynchronous operations depend on may not have been initialized or loaded. If asynchronous operations are performed at this stage, the data of dependencies may not be properly retrieved or processed.

By placing the asynchronous operationonMountedIn the hook function, you can ensure that asynchronous operations are performed after the component has been mounted to the DOM, ensuring that the component has been fully initialized and the relevant DOM elements and data are available.

It should be noted thatonMountedIt is a hook function in Vue 3, which can be used in Vue 2mountedHook function to achieve similar effects.

How to use data in data in onMounted hook

Summary: The onMounted hook is a newly added feature of vue3. It belongs to a combination API and is called in the setup. The setup is equivalent to the entry point for component compilation. The setup is run before the beforeCreate hook is executed. At this time, only prop (properties) and context are initialized, and data is executed before created after the beforeCreate hook.

Note: Although onMounted is written in the setup function, it is called when the component is mounted to the parent component.

Method: Since this cannot be used in setup, you need to use the getCurrentInstance method to obtain the currently active components. See the code for details.

<template>
  <div>
    <p>{{  }}</p>
    <p>{{ b }}</p>
  </div>
</template>
<script>
import { reactive, onMounted,getCurrentInstance } from "vue";
export default {
  data() {
    return {
      b: 111,
    };
  },
  setup(props) {
    const aa = reactive({
      a: 1,
    });
    const ins = getCurrentInstance();
    // (b)
    onMounted(() => {
      ("hello");
      (ins)
      // Pay attention to how data b and a are used      ()
      ()
    });
    return {
      aa,
    };
  },
};
</script>
<style>
</style>

This is the article about using onMounted to ensure that Vue uses onMounted to perform asynchronous operations after component mount. For more related contents of Vue using onMounted component mount, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!