[英]Passing data properties in vue js laravel component
Am new to Vue.js and I would like to pass the data propert value to my template. Vue.js的新手,我想将数据属性值传递给我的模板。
I have: 我有:
export default {
name: "profile",
data: () => ({
user:[]
}),
mounted() {
axios
.get('/api/user')
.then(function (response) {
this.user = response
})
.catch(function (response) {
console.error(response);
});
}
}
In the HTML template I have: 在HTML模板中,我有:
<template>
<div>{{user.name }}</div>
</template>
Now I am getting an error of 现在我得到一个错误
cannot set property
user
ofundefined
无法将属性user
设置为undefined
A console.log(response)
produces: console.log(response)
产生:
name: "user 1", // name exists 名称:“用户1”,//名称存在
email ... 电子邮件...
Try using arrow function
for axios callbacks 尝试将arrow function
用于axios回调
.then((response) => {
this.user = response
})
arrow function
binds the components context as you expect. arrow function
按预期方式绑定了组件上下文。
In mounted: 已安装:
mounted() {
var self = this;
axios.get('/api/user')
.then(function (response) {
self.user = response
})
.catch(function (response) {
console.error(response);
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.