Let's talk about the common properties and methods of Vue instance objects

PHPz
Release: 2023-04-12 11:17:12
Original
1872 people have browsed it

Vue.js is a front-end framework that can implement data-driven and componentized view features. In a Vue.js application, each Vue instance object has its own properties and methods, which are very important in Vue development. This article will introduce the common properties and methods of Vue instance objects.

Vue instance object properties

data

Each Vue instance object must have the data property, which is the root object for storing data in the Vue instance object . This is one of the core aspects of Vue's data-driven implementation. For example, we define a Vue instance as follows:

var vm = new Vue({
  data: {
    message: 'Hello World!'
  }
})
Copy after login

In the above Vue instance, a data object is defined and a string 'Hello World!' is initialized. This message attribute can be used in Vue templates.

props

propsProperty is a way to receive data from the parent component. It is a property in the Vue component instance object and is used to automatically Pass parameters when defining the component. When a component is defined within another component, you can use props to pass data from the parent component to the child component. For example:

Vue.component('child-component', {
  props: ['message'],
  template: '<div>{{message}}</div>'
})

var vm = new Vue({
  el: '#app',
  data: {
    parentMessage: 'Hello World!'
  }
})

<div id="app">
  <child-component :message="parentMessage"></child-component>
</div>
Copy after login

In the above example, we created a component named child-component, which has a props attribute and defines a component named message attributes. By binding the message attribute in the parent component, the parent component can pass data to the customized child component, thereby using the data to customize the template in the child component.

computed

computedThe property is a computed property in the Vue instance object. When the data used in a template needs to change based on changes in other data, computed properties can be created through the computed property. For example:

var vm = new Vue({
  el: '#app',
  data: {
    message: 'Hello'
  },
  computed: {
    reversedMessage: function() {
      return this.message.split('').reverse().join('')
    }
  }
})
Copy after login

In the above example, we defined a calculated property reversedMessage, which is obtained by reversing the value of the message attribute. When the message changes, the computed properties also change.

methods

The methods property in the Vue instance object contains reusable methods, which are defined as functions in the Vue instance object. When a Vue instance object needs to handle dynamic events, the methods attribute can be used. For example:

var vm = new Vue({
  el: '#app',
  data: {
    message: 'Hello World!'
  },
  methods: {
    reverseMessage: function() {
      this.message = this.message.split('').reverse().join('')
    }
  }
})
Copy after login

In the above example, we defined a reverseMessage method, which reverses the value of the message attribute. This method can be called through the v-on directive in the Vue template.

watch

#watchThe property is used to monitor changes in data in the Vue instance object. When the monitored data in the Vue instance object changes, the watch attribute will automatically execute a function. For example:

var vm = new Vue({
  el: '#app',
  data: {
    message: 'Hello'
  },
  watch: {
    message: function(val) {
      console.log('message changed to', val)
    }
  }
})
Copy after login

In the above example, we defined a watch attribute. When the message attribute changes, it will be monitored, and the method in the watch attribute will be executed to print the changed value to the console.

Vue instance object method

$watch

Instance method$watch** is used to monitor changes in data in the Vue instance object. When the monitored data in the Vue instance object changes, the **$watch method will automatically execute a function. For example:

var vm = new Vue({
  el: '#app',
  data: {
    message: 'Hello'
  }
})

vm.$watch('message', function(val) {
  console.log('message changed to', val)
})
Copy after login

In the above example, we use the $watch method to monitor changes in the message attribute and print information when it changes. The

$on

$on method is used to register event listeners in the Vue instance object. For example:

var vm = new Vue({
  el: '#app',
  data: {
    message: 'Hello'
  }
})

vm.$on('message-changed', function(val) {
  console.log('message changed to', val)
})
Copy after login

In the above example, we use the $on method to register an event named message-changed in the Vue instance object. When the message attribute changes, we issue an event.

$emit

$emit method is used to trigger an event in the Vue instance object. For example:

var vm = new Vue({
  el: '#app',
  data: {
    message: 'Hello'
  }
})

vm.$on('message-changed', function(val) {
  console.log('message changed to', val)
})

vm.$emit('message-changed', 'Hello World!')
Copy after login

In the above example, we use the $emit method to trigger an event named message-changed and set the parameters of the event to Hello World!.

$nextTick

$nextTick method is used to execute a callback after the DOM of the Vue instance object is updated. For example:

var vm = new Vue({
  el: '#app',
  data: {
    message: 'Hello'
  }
})

vm.$nextTick(function() {
  console.log('DOM updated')
})
Copy after login

In the above example, we use the $nextTick method to execute a callback after the DOM of the Vue instance object is updated.

Summary

The Vue instance object is a very important concept in Vue development. When creating a Vue instance object, you must understand its properties and methods. This article briefly introduces the common properties and methods of Vue instance objects, including: data, props, computed, methods, watch, $watch, $on, $emit and $nextTick, etc. These properties and methods make Vue development more flexible and convenient.

The above is the detailed content of Let's talk about the common properties and methods of Vue instance objects. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!