Vue3中操作dom的四种方式

1.通过ref直接拿到dom引用

通过对div元素添加了ref属性,为了获取到这个元素,我们声明了一个与ref属性名称相同的变量sectionRef,然后我们通过 sectionRef.value 的形式即可获取该div元素。

<template>
  <div class="demo1-container">
      <p>通过ref直接拿到dom</p>
      <div ref="sectionRef" class="ref-section"></div>
      <button @click="higherAction" class="btn">变高</button>
  </div>
</template>

<script setup lang="ts">
import {
    
    ref} from 'vue'
const sectionRef = ref()
let height = 100;

const higherAction = () => {
    
    
  height += 50;
  sectionRef.value.style = `height: ${
      
      height}px`;
}
</script>

<style scoped>
.demo1-container {
    
    
  width: 100%;
  height: 100%;
}


.ref-section {
    
    
  width: 200px;
  height: 100px;
  background-color: pink;
  transition: all .5s ease-in-out;
}

.btn {
    
    
  width: 200px;
  height: 50px;
  background-color: gray;
  color: #fff;
  margin-top: 100px;
}
</style>

通过父容器的ref遍历拿到dom引用

<template>
  <div class="demo2-container">
      <p>通过父容器遍历拿到dom</p>
      <div ref="listRef" class="list-section">
          <div @click="higherAction(index)" class="list-item" v-for="(item, index) in state.list" :key="index">
              <span>{
    
    {
    
    item}}</span>
          </div>
      </div>
  </div>
</template>

<script setup lang="ts">
import {
    
     ref, reactive } from 'vue'
const listRef = ref()
const state = reactive({
    
    
  list: [1, 2, 3, 4, 5, 6, 7, 8]
})

const higherAction = (index: number) => {
    
    
  let height = listRef.value.children[index].style.height ? listRef.value.children[index].style.height : '20px';
  height = Number(height.replace('px', ''));
  listRef.value.children[index].style = `height: ${
      
      height + 20}px`;
}
</script>

<style scoped>
.demo2-container {
    
    
  width: 100%;
  height: 100%;
}

.list-section {
    
    
  width: 200px;
}

.list-item {
    
    
  width: 200px;
  height: 20px;
  background-color: pink;
  color: #333;
  transition: all .5s ease-in-out;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>

通过:ref将dom引用放到数组中

<template>
  <div class="demo2-container">
      <p>通过:ref将dom引用放到数组中</p>
      <div class="list-section">
          <div :ref="setRefAction" @click="higherAction(index)" class="list-item" v-for="(item, index) in state.list" :key="index">
              <span>{
    
    {
    
    item}}</span>
          </div>
      </div>
  </div>
</template>

<script setup lang="ts">
import {
    
     reactive } from 'vue'

const state = reactive({
    
    
  list: [1, 2, 3, 4, 5, 6, 7],
  refList: [] as Array<any>
})

const higherAction = (index: number) => {
    
    
  let height = state.refList[index].style.height ? state.refList[index].style.height : '20px';
  height = Number(height.replace('px', ''));
  state.refList[index].style = `height: ${
      
      height + 20}px`;
  console.log(state.refList[index]);
}

const setRefAction = (el: any) => {
    
    
  state.refList.push(el);
}
</script>

<style scoped>
.demo2-container {
    
    
  width: 100%;
  height: 100%;
}

.list-section {
    
    
  width: 200px;
}

.list-item {
    
    
  width: 200px;
  height: 20px;
  background-color: pink;
  color: #333;
  transition: all .5s ease-in-out;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>

通过子组件emit传递ref

<template>
    <div ref="cellRef" @click="cellAction" class="cell-item">
        <span>{
    
    {
    
    item}}</span>
    </div>
</template>

<script setup lang="ts">
import {
    
    ref} from 'vue';

const props = defineProps({
    
    
    item: Number
})
const emit = defineEmits(['cellTap']);
const cellRef = ref();
const cellAction = () => {
    
    
    emit('cellTap', cellRef.value);
}
</script>

<style scoped>
.cell-item {
    
    
    width: 200px;
    height: 20px;
    background-color: pink;
    color: #333;
    transition: all .5s ease-in-out;
    display: flex;
    justify-content: center;
    align-items: center;
}
</style>
<template>
    <div class="demo2-container">
        <p>通过子组件emit传递ref</p>
        <div class="list-section">
            <Cell :item="item" @cellTap="cellTapHandler" v-for="(item, index) in state.list" :key="index">
            </Cell>
        </div>
    </div>
</template>

<script setup lang="ts">
import {
    
     reactive } from 'vue'
import Cell from '@/components/Cell.vue'
const state = reactive({
    
    
    list: [1, 2, 3, 4, 5, 6, 7],
    refList: [] as Array<any>
})

const cellTapHandler = (el: any) => {
    
    
    let height = el.style.height ? el.style.height : '20px';
    height = Number(height.replace('px', ''));
    el.style = `height: ${
      
      height + 20}px`;
}
</script>

<style lang="scss" scoped>
.demo2-container {
    
    
    width: 100%;
    height: 100%;
}

.list-section {
    
    
    width: 200px;
 }
</style>

猜你喜欢

转载自blog.csdn.net/qq_52099965/article/details/128253874