Solution to Vue data assignment problem

Solution to Vue data assignment problem

Let me summarize a problem that I have encountered for a long time.

In the project, the background data is needed to render the front end. Axios integrated with Vue is used. The hook function in Vue is used to send a get request to the background after the page component is mounted, and then the returned data is assigned to the attributes defined in data():

After execution, the front end reports an error:

reason:

After the request is successfully executed, the contents of the callback function are executed. The callback function is inside other functions and this is not bound to any object and is undefined.

Solution:

1) Assign this pointing to the Vue object to the property defined by the external method, and then use the property in the internal method

2) Using arrow functions

Supplement: Solve the undefined problem of calling between data in vue data

Solution:

There is no solution, it cannot be called like this at all.

Although this in the data function points to VueComponent (understanding: the data in data can use this to call the data in props), when calling another attribute in data, the data in data has not been parsed yet, because when returning the {} object, all the data in them are rendered and parsed together, so the undefined problem will occur.

(The above is just my personal understanding. If there are any errors, please comment and correct them.)

So choose to complete the assignment operation in the mounted life cycle

export default {
 data(){
  return {
  firstName:'111',
  lastName:'222',
  fullName:''
  }
 },
 mounted(){
 this.fullName = this.firstName + '' + this.lastName;
 } 
 }

Display results:

Of course, if fullName does not need to be defined in data, it may be elegant to define it in the computed property.

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • How to set input to be uneditable in vue
  • Solution to the problem of Vue getting input value
  • Solution to invalid (unchanged) reassignment of vue data object
  • After the input in Vue is assigned, it can no longer be modified and solved

<<:  Linux lossless expansion method

>>:  MySQL turns off password strength verification

Recommend

Velocity.js implements page scrolling switching effect

Today I will introduce a small Javascript animati...

JavaScript to achieve simple tab bar switching case

This article shares the specific code for JavaScr...

MySQL example of getting today and yesterday's 0:00 timestamp

As shown below: Yesterday: UNIX_TIMESTAMP(CAST(SY...

JavaScript exquisite snake implementation process

Table of contents 1. Create HTML structure 2. Cre...

Learn more about the most commonly used JavaScript events

Table of contents JavaScript events: Commonly use...

Sample code for implementing radar chart with vue+antv

1. Download Dependency npm install @antv/data-set...

Example code for implementing an Upload component using Vue3

Table of contents General upload component develo...

Diagram of the process of implementing direction proxy through nginx

This article mainly introduces the process of imp...

Detailed steps to start the Django project with nginx+uwsgi

When we develop a web project with Django, the te...

Docker generates images through containers and submits DockerCommit in detail

Table of contents After creating a container loca...

React Router V6 Updates

Table of contents ReactRouterV6 Changes 1. <Sw...

Django2.* + Mysql5.7 development environment integration tutorial diagram

environment: MAC_OS 10.12 Python 3.6 mysql 5.7.25...

JavaScript macrotasks and microtasks

Macrotasks and Microtasks JavaScript is a single-...

Detailed explanation of basic data types in mysql8.0.19

mysql basic data types Overview of common MySQL d...

Ideas and practice of multi-language solution for Vue.js front-end project

Table of contents 1. What content usually needs t...