在vue-cli中使用axios时报错TypeError: Cannot set property 'lists' of undefined at eval

在vue-cli中使用axios拿取数据时

export default {

data(){

return{

lists:{

}

}

},

methods: {

getList(){

axios.get(‘https://cnodejs.org/api/v1/topics‘,{

})

.then(function(response){

window.console.log(response.data.data);

this.lists = response.data.data;

})

.catch(function (error) {

window.console.log(error);

})

}

},

beforeMount() {

this.getList();

},

报错

TypeError: Cannot set property ‘lists‘ of undefined at eval (list.vue?51fc:19)

能get到数据,但无法传给lists数组

报错信息lists未定义,查找调试许久,发现是this指向问题。

在vue-cli里.then里使用function就会乱了this指向

把.then里function改用es6的写法就能正常传参了

.then((response)=>{
        window.console.log(response.data.data);
        this.lists = response.data.data;
      })

为什么用function定义就会乱this指向,奈何一时半会儿也没搞懂,留此疑惑,日后解答

在vue-cli中使用axios时报错TypeError: Cannot set property 'lists' of undefined at eval

原文地址:https://www.cnblogs.com/raonet/p/11581997.html

时间: 2024-08-27 04:32:20

在vue-cli中使用axios时报错TypeError: Cannot set property 'lists' of undefined at eval的相关文章

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

正常情况下在data里面都有做了定义 在函数里面进行赋值 这时候你运行时会发现,数据可以请求到,但是会报错 TypeError: Cannot set property 'listgroup' of undefined 主要原因是: 在 then的内部不能使用Vue的实例化的this, 因为在内部 this 没有被绑定.可以看下 Stackoverflow 的解释: 解决办法: 1.用ES6箭头函数,箭头方法可以和父方法共享变量 2.在请求axios外面定义一下 var that=this 问题

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

created() { var that=this axios.get('http://jsonplaceholder.typicode.com/todos') .then(function (res) { // handle success // console.log(res); that.todos = res.data }) .catch(function (error) { // handle error console.log(error); }) .finally(function

axios报错: Cannot read property 'protocol' of undefined ....

错误: Uncaught (in promise) TypeError: **Cannot read property 'protocol' of undefined ... 源码: 完整错误: import axios from 'axios' import VueAxios from 'vue-axios' Vue.use(axios, VueAxios) 修正一:(亲测) import axios from 'axios' import VueAxios from 'vue-axios'

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

我只安装了webpack,没有安装webpack-cli,第一次输入webpack打包时,提示 One CLI for webpack must be installed. These are recommended choices, delivered as separate packages: - webpack-cli (https://github.com/webpack/webpack-cli) The original webpack full-featured CLI. We wi

Vue ElementUI Axios报错: Uncaught (in promise) TypeError: Cannot read property '$message' of undefined

从头再来!!! 出错的代码如下: login() { this.loading = true let userInfo = {account: this.loginForm.account, password: this.loginForm.password, captcha: this.loginForm.chptcha} this.$api.login.login(userInfo).then( function(res) { if (res.msg != null) { this.$mes

linux中进入mysql时报错Access denied for user 'root'@'localhost' (using password: YES)解决方案

之前在linux中装完mysql后直接在命令行窗口输入mysql就会进入数据库了,但是今天输入mysql命令后直接报错,如下图: 之后输入:mysql -uroot -p 提示输入密码:***** 还是报同样的错误,在网上查说是因为root用户没有设置mysql密码导致的,然后根据网上给出的方案进行调试解决,步骤如下: 1.先停掉mysql服务,然后以安全模式后台方式启动,此时光标会一直闪动,表理它! 2.然后新打开一个会话窗口,直接在命令行输入:mysql,会直接进入到数据库命令行 3.然后就

UEditor 粘贴 Excell 中的表格时报错导致无法粘贴的解决办法

开始->运行->gpedit.msc,打开策略组编辑器,在树状菜单中选 择计算机配置->管理模板->终端服务,在右侧窗口中打开"限制 连接数量",选择"已启用",修改"TS允许的最大连接数",确定 . 完成以上两步可以解决远程连接最大值你能为3个(包括本地控制台 )的问题.如果需要使多用户可以同时使用同一个用户名登录远程连 接,那么还需要进行一下设置: 开始->运行->tscc.msc,打开终端服务配置,点击&

Python2.7 在使用BSTestRunner.py时报错TypeError: unicode argument expected, got 'str'

python3往这个库中加入了一些新的内容,使得该库在Python2.7中报错. 解决方法是将导入语句 from io import StringIO as StringIO 更换为: from io import BytesIO as StringIO   Python2.7 在使用BSTestRunner.py时报错TypeError: unicode argument expected, got 'str'

statsmodels.tsa.arima_model预测时报错TypeError: int() argument must be a string, a bytes-like object or a number, not 'Timestamp'

在 python 中用 statsmodels创建 ARIMA 模型进行预测时间序列: import pandas as pd import statsmodels.api as sm df = pd.read_csv("data.csv", index_col=0, parse_dates=True) mod = sm.tsa.statespace.SARIMAX(df['price'], enforce_stationarity=False, enforce_invertibili