How to get the value of ref attribute in vue3+ts

ref attribute
is equivalent to giving the tag id, and then using getElementById to get its method like js

When should I use the ref attribute?

element-plus
Such as calling the method in element-plus.
You can add the ref attribute in el-table, define a const multipleTable = ref() in ts, and you can directly call the method in the picture
For example:

       <el-table :data="tableData" 
          style="width: 100%" :lazy="true"
          ref="multipleTable"  ## 给table设置id
        >

Define variables,Note: It must be written in return, otherwise the HTML object will not be obtained.
Or add setup in
<script lang="ts" > 添加 <script lang="ts" setup>, choose one of the two

     setup() {
    
    
        const multipleTable= ref();
        return {
    
     multipleTable }
      }

Instructions

       /**
       * 点击全选事件
       */
      function checkAll(){
    
    
        multipleTable.value.toggleAllSelection();
      }

that's it

This is how it is used with vue2. You need to use this but vue3 does not need to use it.

this.$refs.multipleTable.toggleAllSelection();

Guess you like

Origin blog.csdn.net/qq_45287575/article/details/128296747