正常定义全局变量:

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. Vue23 ref属性

    1 简介 在js中,我们获取一个元素可以通过document.getElementById()去获取,在vue中,使用了ref属性来替代id,通过ref属性可以获取html元素以及vue组件实例对象 ...

  2. Docker安装Tomcat应用服务器

    1.安装镜像 1. Install the image: 可以先到https://hub.docker.com/  搜索镜像 You can get there first. https://hub. ...

  3. C++并发-同步并发

    1.等待事件 std::mutex m; void wait() { std::unique_lock<std::mutex> lk(m); lk.unlock(); std::this_ ...

  4. EPICS Archiver Appliance存储waveform记录的尝试

    https://blog.csdn.net/u013894429/article/details/79724454 按上面的指导很容易跑起来,试了一下,也能archive短些的waveform记录,很 ...

  5. [POI2014]HOT-Hotels 加强版

    长链剖分优化 \(dp\) 模板 不过这 \(dp\) 真毒 \(\text{Code}\) #include <cstdio> #define RE register #define I ...

  6. JZOJ 2937. 【NOIP2012模拟8.9】监听还原

    题面 分析 注意读题 然后显然字符串哈希 \(Code\) #include<cstdio> #include<cstring> using namespace std; ty ...

  7. Oracle中表字段加中文注释,应该怎么写呢?

    首发微信公众号:SQL数据库运维 原文链接:https://mp.weixin.qq.com/s?__biz=MzI1NTQyNzg3MQ==&mid=2247485212&idx=1 ...

  8. .NET周报 【2月第4期 2023-02-25】

    国内文章 .NET微服务系统迁移至.NET6.0的故事 https://www.cnblogs.com/InCerry/p/microservice-migration-net-6.html 本次迁移 ...

  9. 【11】java之抽象类

    一.抽象类基本概念 1.1 抽象类 抽象类:是指在普通类的结构里增加抽象方法的组成部分,抽象类要使用 abstract 声明. 抽象方法:没有方法体且必须使用 abstract 关键字进行定义. 拥有 ...

  10. Windows 下安装 Bun:像 Node 或 Deno 一样的现代 JavaScript 运行时

    背景 最近前端工具链又火了一个项目 Bun,可以说内卷非常严重.Bun 是一个新的 JavaScript 运行时,内置了打包器.转译器.任务运行器和 npm 客户端. Bun 是像 Node 或 De ...