Home > Web Front-end > JS Tutorial > body text

Regarding this pointing problem in vue (detailed tutorial)

亚连
Release: 2018-06-19 11:40:35
Original
7878 people have browsed it

I have been learning to use vue axios recently and found a problem during use. I will share it with you in the following summary. This article mainly introduces you to the relevant information about this pointing problem when vue uses axios. The article introduces it through sample code. It is very detailed, friends in need can refer to it.

Preface

As we all know, axios is a plug-in for Vue to request data that appears after vue-resource. After vue was updated to 2.0, the author Youda announced that he would no longer update vue-resource, but recommended axios. For more detailed introduction, you can refer here: //www.jb51.net/article/109444.htm

This article mainly introduces the pointing problem of this when Vue uses axios. I won’t say much more below. Let’s take a look at the detailed introduction.

1. Solution

When using axios to make network requests in vue, you will encounter that this does not point to vue, but is undefined , which can be solved using the arrow function "=>". As follows:

methods: {
 loginAction(formName) {
 this.$axios.post('http://127.0.0.1/u/subLogin', {
  username: this.username,
  password: this.password
 })
  .then(function(response){
  console.log(this); //这里 this = undefined
  })
  .catch((error)=> {
  console.log(error); //箭头函数"=>"使this指向vue
  });

 });
 }
}
Copy after login

2. Reason

The arrow function "=>" in ES6 internal this is the lexical scope, determined by the context Determined (that is, determined by the outer caller vue).

3. Off topic

Use the "=>" function, you can say goodbye to the previous two ways of writing:

bind(this)To change the this pointer of the anonymous function

hack writing methodvar _this= this;:

loginAction(formName) {
 var _this= this;
 this.$axios.post("...")
 .then(function(response){
  console.log(_this); //这里 _this 指向vue
 })
 });
 }
Copy after login

The above is what I compiled For everyone, I hope it will be helpful to everyone in the future.

Related articles:

How to implement random layout waterfall flow in ionic3

How to implement the return to top effect in JS

How to implement element scroll bar loop to append content in JavaScript

Deploy vue project on nginx (detailed tutorial)

How to convert the timestamp format in js

How to get the dom element in vue

How to implement it in vue Read the full text

How to build a vue project on webpack

How to implement OAuth2.0 authorization service authentication in nodejs

The above is the detailed content of Regarding this pointing problem in vue (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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!