vue refs modify properties

WBOY
Release: 2023-05-11 10:04:36
Original
1665 people have browsed it

Vue is a progressive framework for building user interfaces. It is simple, flexible, and efficient, and is widely used in front-end development. Among them, refs is an important feature provided by Vue, which allows developers to directly access DOM elements and component instances to modify properties or call methods. In this article, we will explore the related techniques of modifying properties of Vue refs to help readers better master this important function.

1. Introduction to Vue Refs

In Vue, refs is a special attribute used to access components or DOM elements. It has two ways of writing:

  1. String form:

    <template>
      <div ref="myRef"></div>
    </template>
    Copy after login
  2. Function form:

    <template>
      <div ref="myRef"></div>
    </template>
    
    <script>
    export default {
      methods: {
     log() {
       console.log(this.$refs.myRef);
     }
      }
    }
    </script>
    Copy after login

    refs can be used To access a component instance, DOM element or subcomponent, the method is usually to declare it in the template and then access it through this.$refs in the JavaScript code of the component. It should be noted that refs can only be set after the component is rendered, so it is recommended to use it in the mounted life cycle function.

2. Modify attributes of Vue Refs

  1. Modify the attributes of DOM elements

You can directly modify the attributes of DOM elements through refs , for example, modify the placeholder of the text box:

<template>
  <div>
    <input type="text" ref="myInput">
  </div>
</template>

<script>
  export default {
    mounted() {
      this.$refs.myInput.placeholder = '请输入用户名';
    }
  }
</script>
Copy after login

When the component rendering is completed, the mounted life cycle function will be called, and refs can be used in this function to modify the attributes of the DOM element.

  1. Modification of component attributes

refs can also be used to modify the attributes of the component. In the JavaScript code of the component, obtain the component instance through this.$refs, and then you can modify The component performs operations such as adding, deleting, modifying, and checking attributes:

<template>
  <div>
    <my-component ref="myComp"></my-component>
    <button @click="changeText">点击修改</button>
  </div>
</template>

<script>
  import MyComponent from './MyComponent.vue'
  export default {
    components: {MyComponent},
    methods: {
      changeText() {
        this.$refs.myComp.text = '新的文本内容';
      }
    }
  }
</script>
Copy after login

In the above example, we call the changeText method through the click event of the button, and then use refs to obtain the component instance and modify the text attribute of the component.

It should be noted that if you want to modify the properties of the component, you need to ensure that the property is modifiable, that is, the property is not read-only (such as props). If you want to modify the properties in props, it is recommended to call the $emit method to notify the parent component to make modifications.

  1. Call component methods

In addition to being able to modify component properties, refs can also be used to call component methods. In the JavaScript code of the component, you can get the component instance through $refs, and then call the component's method:

<template>
  <div>
    <my-component ref="myComp"></my-component>
    <button @click="callMyMethod">调用组件方法</button>
  </div>
</template>

<script>
  import MyComponent from './MyComponent.vue'
  export default {
    components: {MyComponent},
    methods: {
      callMyMethod() {
        this.$refs.myComp.myMethod();
      }
    }
  }
</script>
Copy after login

In the above example, we called the myMethod method in the my-component component. Make sure that the method is Public, that is, defined in the component's methods.

It should be noted that calling component methods can not only be achieved through refs, but also through events, $emit, etc. However, using refs to call methods has the advantages of directness and simplicity, and can better achieve code readability and maintainability.

3. Summary

Vue Refs modifying properties is one of the important features of the Vue framework. It can be used to access component instances, DOM elements or subcomponents, to modify properties or call methods, etc. When applying Vue Refs to modify properties, you need to pay attention to some details, such as readability, simplicity, type of modified properties, methods to be called, etc. Through the explanation of this article, I believe readers can better master the skills of modifying properties of Vue Refs and optimize the development of Vue applications.

The above is the detailed content of vue refs modify properties. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template