Vue2.X升级成vue3.X 后如何获取DOM节点

vue3.x中使用ref获取dom

方法一

在标签中 增加ref属性

<div id="myChart" ref="myChart"></div>

在setup中引用

setup(props) {
    let myChart = ref(null);
    const state = reactive({
      chart:null,
      myChart
    });
    

    onMounted(() => {
      state.chart = echarts.init(myChart.value);
    })
    return toRefs(state);
  }

方法二

*** 导入 getCurrentInstance

import { getCurrentInstance, onMounted, reactive,  toRefs } from 'vue';
export default {
  setup() {
    const {ctx} = getCurrentInstance();
  
    onMounted(()=>{
		// 当前dom节点
    	const _this = ctx.$el;
	})
    }

版权声明:本文为weixin_43860703原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。