正常定义全局变量:

data:function (){
return{
currentOrders:[]
}
},

  使用Axios发送请求并获取后端数据时,如果在then中使用this.currentOrders会出现TypeError: Cannot set property 'xxxx' of undefined错误。

  原因是使用this$axios().then(function(response){}).catch()格式获取参数,then的内部 this 没有被绑定,所以不能使用Vue的实例化的this。

  解决办法一:在this.$axios外部将this赋值为自定义变量:var that = this,然后在then里面使用that.currentOrders

var that = this
this.$axios({
method: 'get',
url: 'xxxxxx',
}).then(function (response) {
var jsonObj = JSON.parse(JSON.stringify(response.data))
if (jsonObj.code === ERR_ok){
that.currentOrders = jsonObj.data
}
}).catch(function (error) {
console.log(error);
})

第二种方法:用ES6箭头函数,箭头方法可以和父方法共享变量

this.$axios({
method: 'get',
url: 'xxxxxx',
}).then((response)=>{})
      .catch(){}

  

VUE使用axios数据请求时报错 TypeError: Cannot set property 'xxxx' of undefined 的解决办法的更多相关文章

  1. VUE.JS 使用axios数据请求时数据绑定时 报错 TypeError: Cannot set property 'xxxx' of undefined 的解决办法

    正常情况下在data里面都有做了定义 在函数里面进行赋值 这时候你运行时会发现,数据可以请求到,但是会报错 TypeError: Cannot set property 'listgroup' of ...

  2. VUE - 使用axios数据请求时数据绑定时 报错 TypeError: Cannot set property 'xxxx' of undefined 的解决办法

     created() {     var that=this     axios.get('http://jsonplaceholder.typicode.com/todos')     .then( ...

  3. 使用webpack命令打包时,报错TypeError: Cannot read property 'presetToOptions' of undefined的解决办法

    我只安装了webpack,没有安装webpack-cli,第一次输入webpack打包时,提示 One CLI for webpack must be installed. These are rec ...

  4. vue表单校验提交报错TypeError: Cannot read property 'validate' of undefined

    TypeError: Cannot read property 'validate' of undefined at VueComponent.submitForm (plat_users.html: ...

  5. Node中使用MySQL报错:TypeError: Cannot read property 'query' of undefined

    Node中使用MySQL报错: TypeError: Cannot read property 'query' of undefined at /Users/sipeng/Desktop/彭思/201 ...

  6. vue报错TypeError: Cannot read property 'protocol' of undefined

    错误信息如下所示: isURLSameOrigin.js?3934:57 Uncaught (in promise) TypeError: Cannot read property 'protocol ...

  7. Vue使用Typescript开发编译时提示“ERROR in ./src/main.ts Module build failed: TypeError: Cannot read property 'afterCompile' of undefined”的解决方法

    使用Typescript开发Vue,一切准备就绪.但npm start 时,提示“ ERROR in ./src/main.tsModule build failed: TypeError: Cann ...

  8. day 74 vue 2 axios数据请求 以及组件的学习

    前情提要:   vue 学习二: 一: 通过axios实现数据请求 1:json数据语法 json数据对象类似于JavaScript中的对象,但是它的键对应的值里面是没有函数方法的,值可以是普通变量, ...

  9. vue 使用axios 数据请求第三方插件的使用

    axios 基于http客户端的promise,面向浏览器和nodejs 特色 浏览器端发起XMLHttpRequests请求 node端发起http请求 支持Promise API 监听请求和返回 ...

  10. react报错 TypeError: Cannot read property 'setState' of undefined

    代码如下: class test extends Component { constructor(props) { super(props); this.state = { liked: false ...

随机推荐

  1. dotnet 8 preview 1 即将发布

    .Net 8 preview 1 即将到来,让我们来提前看看都要发布什么吧. .Net 8 preview 1 即将到来 .NET 8 的第一个预览版将在几周内发布,微软的 David Ortinau ...

  2. 【雅礼联考DAY01】数列

    #include<cstdio> #include<map> using namespace std; typedef long long LL; const int N = ...

  3. 代码随想录算法训练营day06 | leetcode 242、349 、202、1

    基础知识 哈希 常见的结构(不要忘记数组) 数组 set (集合) map(映射) 注意 哈希冲突 哈希函数 LeetCode 242 分析1.0 HashMap<Character, Inte ...

  4. ubuntu 启动脚本变化

    ubuntu-16.10 开始不再使用initd管理系统,改用systemd- 快速看了 systemd 的使用方法,发现改动有点大, 包括用 systemctl 命令来替换了 service 和 c ...

  5. c++多线程thread用法小例子

    测试分布式存储系统时,针对并发测试,同时创建500个文件,采用这种方法. #include<iostream> #include<thread> using namespace ...

  6. 【win+r】快速打开软件

    第一种设置方法: ①把要启动的文件夹路径添加到系统环境变量里面去 ②按win+r就可以快速启动了 第二种设置方法: 是直接把(程序的)快捷方式,放到安装目录的 Windows\System32\ 文件 ...

  7. 40.Ribbon和Feign

    优先级:全局代码 < 全局属性 < 细粒度代码 < 细粒度属性 推荐属性配置

  8. 快捷方法1:csdn如何不登录复制代码

    按F12,在console里执行下面两句代码 $("#content_views pre").css("user-select","text" ...

  9. [Spring] SpringBoot启动流程

    SpringBoot启动流程 实例化流程 推断应用类型,创建的是REACTIVE应用.SERVlET应用.NONE三种中的一种 使用SpringFactoreisLoader查找并加载classpat ...

  10. virtualenvwrapper使用命令

    virtualenvwrapper使用命令 创建虚拟环境:mkvirtualenv + test1 查看虚拟环境:lsvirtuslenv 删除虚拟环境:rmvirtualenv + test1 退出 ...