How to get external data in vue

PHPz
Release: 2023-04-10 09:32:39
Original
565 people have browsed it

Vue is a very popular JavaScript framework that allows us to build elegant, responsive web interfaces. Using Vue in our application, we need to bind data to the template, which allows us to update DOM elements without refreshing the page. This article aims to introduce how Vue handles external data.

Vue provides a concept called "components" that allow us to combine reusable blocks of code in our applications. Each component has its own data and behavior and can easily interact with other components. So, what do we do when our data is not in the component?

Vue provides two main ways to handle external data: Prop and Vuex.

1. Prop

Prop is a way to transfer data between components in Vue. It allows us to pass data from parent components to child components. Child components can use this data as their own properties and update them as needed.

When using Prop, we need to declare the data we need to receive in the component. This can be achieved by adding props attributes to the component. The following is an example of receiving a Prop named "message":

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

We can pass data to the component where it is used, for example:

<my-component message="Hello, world!"></my-component>
Copy after login

The component sees this message attribute , and can be used to display the desired message.

2. Vuex

Vuex is the official state management library for Vue, which allows us to share data throughout the application. Vuex uses a central repository called a "store" to store the state of your application. Components can get state from the store and can update them.

When using Vuex, we need to first define a store. This can be achieved by creating an object containing the application state and update state. Here is a simple example:

const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment (state) {
      state.count++
    }
  }
})
Copy after login

When using Vuex in a component, we need to import the store first. This can be achieved by adding the following to the component:

import { mapState, mapMutations } from 'vuex'

export default {
  computed: {
    ...mapState(['count'])
  },
  methods: {
    ...mapMutations(['increment'])
  }
}
Copy after login

We can use computed properties to get the state from the store, for example:

<p>Count: {{ count }}</p>
Copy after login

We can also use methods to update the state, for example :

<button v-on:click="increment">Increment</button>
Copy after login

When the button is clicked, calling the increment method will update the status.

To summarize, Vue provides two main ways to handle external data: Prop and Vuex. Props allow us to pass data from parent components to child components and update them as needed. Vuex allows us to share data throughout the application and update them anywhere. Depending on the specific application scenario, we can choose the appropriate way to process external data.

The above is the detailed content of How to get external data in vue. 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!