Home > Web Front-end > uni-app > How to assign value to data after uniapp interface request

How to assign value to data after uniapp interface request

PHPz
Release: 2023-04-17 13:56:23
Original
1970 people have browsed it

With the popularity of mobile Internet, more and more people are beginning to use mobile applications. For developers, developing mobile applications has also become an important task. I believe everyone knows that a good mobile application must have a stable and reliable data interface. During the development process, uniapp is a very excellent development framework, which allows us to quickly develop an application that not only has a good user experience, but also has a stable and reliable data interface. So how to assign a value to the data after requesting the uniapp interface? Let’s introduce it in detail below.

1. Data assignment through vuex

vuex is a state management mode that plays a very important role in vue. In uniapp, we can also use vuex for data management.

First, we need to create a data management js file under the store folder. In this file, we need to introduce vuex and create a global state to store data.

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    data: null
  }
})

export default store
Copy after login

Then, introduce this vuex file into the page, and after requesting the data, assign the data to the state in the store.

import store from '../../store/index.js'

uni.request({
    url: 'http://xxx.com/api/data',
    success(res) {
        store.state.data = res.data
    }
})
Copy after login

In this way, the data stored in the store can be used in other components.

2. Data transfer through props

In uniapp, we can also use props to transfer data.

In components, we can define props.

props: {
    data: {
        type: Object,
        default: {}
    }
}
Copy after login

Then, after requesting the data, save it in the parent component and pass this data to the child component through props.

uni.request({
    url: 'http://xxx.com/api/data',
    success(res) {
        this.data = res.data
    }
})
Copy after login
<template>
  <div>
    <child :data="data"></child>
  </div>
</template>
Copy after login

In this way, the data passed by the parent component can be used in the child component.

Summary:

Through the above two methods, we can easily assign data in uniapp and use these data in different components. When developing, we choose different methods for data processing based on actual application conditions.

The above is the detailed content of How to assign value to data after uniapp interface request. 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