最近在使用vue过程中,使用axios进行接口请求,确发现取不到值,返回为undefined。

show (item) {
let searchText = item.keyword
  console.log(this)              // 返回vue实例
axios.get('http://localhost:3000/search/multimatch?keywords=' + searchText, {}, {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
  .then(function(res) {
  console.log(this)          // undefined
if (res.data.code == 200) {
  this.artistData = res.data.result.artist[0]
  this.albumData = res.data.result.album[0]
}
})
.catch((err) => {
console.log(err)
})
}

在vue中,this都指向vue,然而在axios中,this却指向axios,因此需要使用箭头函数,不进行this的绑定

show (item) {
let searchText = item.keyword
  console.log(this)              // 返回vue实例
axios.get('http://localhost:3000/search/multimatch?keywords=' + searchText, {}, {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
  .then((res) => {
  console.log(this)          // 返回vue实例
if (res.data.code == 200) {
  this.artistData = res.data.result.artist[0]
  this.albumData = res.data.result.album[0]
}
})
.catch((err) => {
console.log(err)
})
}
或者将this的值赋给内部变量
show (item) {
let searchText = item.keyword
  console.log(this)              // 返回vue实例
  let that = this;
axios.get('http://localhost:3000/search/multimatch?keywords=' + searchText, {}, {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
  .then((res) => {
  console.log(that)          // 返回vue实例
if (res.data.code == 200) {
  this.artistData = res.data.result.artist[0]
  this.albumData = res.data.result.album[0]
}
})
.catch((err) => {
console.log(err)
})
}
 

axios中的this指向问题的更多相关文章

  1. vue的爬坑之路-------axios中this的指向问题

    在自己的vue小项目中使用了axios这个插件,但是发现在axios请求数据成功之后的回调函数中this并不是指向当前vue实例, 在如下代码中 谷歌浏览器中报  this.goodsArr 未被定义 ...

  2. 理解js中this的指向

         学习自原文  http://www.cnblogs.com/pssp/p/5216085.html后的一点小结(原文作者总结的很棒^_^)! 关于js中this的指向,在函数定义的时候还无法 ...

  3. javascript中的this指向问题

    在深入学习JavaScript之后,我们越来越多的会遇到函数或者在对象内部中,对于this的指向问题的疑惑,其实基本上每一个编程语言中都有一个this,这个this的指向都是大同小异,你也可以汉化它的 ...

  4. 关于setInterval和setTImeout中的this指向问题

    前些天在练习写一个小例子的时候用到了定时器,发现在setInterval和setTimeout中传入函数时,函数中的this会指向window对象,如下例: var num = 0; function ...

  5. Js中的this指向问题

    函数中的this指向和当前函数在哪定义的或者在哪执行的都没有任何的关系分析this指向的规律如下: [非严格模式]1.自执行函数中的this永远是window [案例1] var obj={ fn:( ...

  6. js中this的指向

    在js中this的指向对于新手来说一定是个难题,但是如果你真正理解了的话,也就没什么问题啦,下面就来讲讲this吧. JS中,this的值取决于调用的模式(调用对象),而JS中共有4种调用模式: 1. ...

  7. JavaScript 中的this指向问题

    全局执行     首先,我们在全局环境中看看它的 this 是什么:     浏览器:     console.log(this);     // Window {speechSynthesis: S ...

  8. JavaScript中this的指向问题

    this是面向对象语言中一个重要的关键字,理解并掌握该关键字的使用对于我们代码的健壮性及优美性至关重要.而javascript的this又有区别于Java.C#等纯面向对象的语言,这使得this更加扑 ...

  9. Javascript定时器中的this指向

    使用js中的定时器(setInterval,setTimeout),很容易会遇到this指向的问题. 直接上例子: var name = 'my name is window'; var obj = ...

随机推荐

  1. [Swift]LeetCode144. 二叉树的前序遍历 | Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...

  2. [Swift]LeetCode768. 最多能完成排序的块 II | Max Chunks To Make Sorted II

    This question is the same as "Max Chunks to Make Sorted" except the integers of the given ...

  3. Python面向对象中的类和对象

    类和对象 目标 类和对象的概念 类和对象的关系 类的设计 01. 类和对象的概念 类 和 对象 是 面向对象编程的 两个 核心概念 1.1 类 类 是对一群具有 相同 特征 或者 行为 的事物的一个统 ...

  4. 20行以内python代码画出各种减压图

    一.太阳花 看到一个很有意思的代码,你若安好,便是晴天!太阳花向你开~ 绘画效果如下: 代码如下: from turtle import * color('red', 'yellow') begin_ ...

  5. eclipse neon 发布

    2016年6月28日,Eclipse基金会宣布发布Eclipse Neon,这个版本的IDE支持Java.JavaScript.C/C++.PHP和Fortran等多种编程语言.这一次的发布集成了77 ...

  6. PyCharm无法激活

    如果你激活软件遇到问题 (Pycharm.GoLand.idea.phpstorm.webstorm.sublime.ultraEdit.win10等等) 比如: 激活框提示Key is invali ...

  7. MySQL casting from decimal to string(mysql decimal 转 varchar)

    今天群里一个哥们问我mysql怎么将decimal转成varchar,经过查阅资料发现,mysql好像不能将decimal直接转换成varchar,但是可以转成char,原文链接:http://sta ...

  8. Java的内部类真的那么难以理解?

    01 前言 昨天晚上,我把车停好以后就回家了.回家后才发现手机落在车里面了,但外面太冷,冷到骨头都能感受到寒意——实在是不想返回一趟去取了(小区的安保还不错,不用担心被砸车玻璃),于是打定主意过几个小 ...

  9. 网络协议 9 - TCP协议(下):聪明反被聪明误

    网络协议 1 - 概述 网络协议 2 - IP 是怎么来,又是怎么没的? 网络协议 3 - 从物理层到 MAC 层 网络协议 4 - 交换机与 VLAN:办公室太复杂,我要回学校 网络协议 5 - I ...

  10. Centos 7 .Net core后台守护进程Supervisor配置

    环境: Centos 7 已安装.Net core 2.0.0  .Net core 1.1.2 1.Supervisor安装 yum 安装 yum install supervisor (阿里云验证 ...