SoFunction
Updated on 2025-04-06

Analysis of application examples of dynamic assignment ref in Vue 3

introduction

Vue 3 introduces many new features, one of which is the Composition API. The Composition API provides a new programming paradigm that enables developers to organize and reuse logic more flexibly. inrefis a core concept that allows us to declare responsive states inside components. This article will explore how to use it in Vue 3refPerform dynamic assignments and illustrate this process with a specific example.

What is ref

In Vue 3,refis a function that takes a value as an argument and returns a.valueObject of attribute. This object's.valueThe attribute is responsive, which means any pair.valueChanges trigger re-rendering of views that depend on this data. This is very useful for managing state within a component.

Dynamic assignmentrefScene of

In some cases, we need to dynamically give it at runtimerefAssign values, especially when processing DOM elements. For example, when we need to initialize a chart and the chart container is provided by a DOM element, we need to obtain this element after the DOM renders it and assign it toref, for subsequent operations.

Implementation example

Let's demonstrate how to dynamically assign values ​​through a simple Vue 3 componentref. Suppose we want to create a component that contains a chart and we need to pass a DOM element as the chart container when the chart is initialized.

1. Create Vue 3 components

First, we need to create a Vue 3 component and define arefCome to store our chart container.

<template>
  <div :ref="setChartRef" style="width: 100%; height: 350px"></div>
</template>
<script setup>
import { ref } from 'vue';
const chartRef = ref(null); // Initialize to empty// Dynamic assignment functionconst setChartRef = (e) => {
   = e;
};
</script>

2. Initialize the chart

Next, we need to initialize the chart after the component mount is completed and usechartRefGet the chart container.

<script setup>
import { ref, onMounted } from 'vue';
import * as echarts from 'echarts';
const chartRef = ref(null); // Initialize to empty// Dynamic assignment functionconst setChartRef = (e) => {
   = e;
};
// Life cycle hook after component mount is completedonMounted(() => {
  if () {
    // Destroy existing chart instances    ();
  }
  const chartInstance = ();
  ({
    title: { text: 'Dynamic assignment ref example' },
    tooltip: {},
    xAxis: {
      data: ["shirt", "Cardwasher", "Chiffon shirt", "Pants", "High heel", "sock"]
    },
    yAxis: {},
    series: [{
      name: 'Sales',
      type: 'bar',
      data: [5, 20, 36, 10, 10, 20]
    }]
  });
  // Save chart example   = chartInstance;
});
</script>

In this example, we used Vue 3'sonMountedLifecycle hook, initializes the chart after component mount is completed.chartInstanceUsed to store chart instances so that we can access it when we need it.

3. Summary

With the above example, we show how to use it in Vue 3refto dynamically assign values ​​and use this feature to initialize a chart. This approach not only improves the maintainability of the code, but also makes state management clearer and more intuitive.

Dynamic assignmentrefThe application scenarios are very wide. In addition to chart initialization, it can also be used to process user input, DOM operations, etc.

This is the end of this article about the application of dynamic assignment ref in Vue 3. For more related content of Vue 3 dynamic assignment ref, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!