【发布时间】:2017-09-22 14:41:34
【问题描述】:
所以我有以下 Vue 文件:
<template>
<li class="notifications new">
<a href="" data-toggle="dropdown"> <i class="fa fa-bell-o"></i> <sup>
<span class="counter">0</span>
</sup>
</a>
<div class="dropdown-menu notifications-dropdown-menu animated flipInX">
<ul v-for="notification in notifications" @click="" class="notifications-container">
<li>
<div class="img-col">
<div class="img" style="background-image: url('assets/faces/3.jpg')"></div>
</div>
</li>
</ul>
</div>
</li>
</template>
<script>
export default {
data: function() {
return {
notifications: [],
message: "",
}
},
methods: {
loadData: function() {
Vue.http.get('/notifications').then(function(response) {
console.log(response.data);
//this.notifications = response.data;
//this.notifications.push(response.data);
this.message = "This is a message";
console.log(this.message);
});
},
},
mounted() {
this.loadData();
},
}
</script>
这编译得很好,但是,在加载网页时,我收到以下错误:
app.js:1769 Uncaught (in promise) TypeError: Cannot set property 'message' of undefined
我也尝试过创建另一种方法,但没有任何乐趣。我似乎无法弄清楚为什么在这里无法访问this。
【问题讨论】:
-
不知道怎么用http,因为我用的是axios,不过你可以试试 let that = this; Vue.http.get()..{ that.message = "这是一条消息"; }
-
@GerardoRosciano 嗨,不...仍然得到相同的响应:(
-
你在使用 Vue 资源吗?
-
看起来您正在使用 Vue 资源。如果你是,那么除了下面的答案之外,使用
this.$http.get代替Vue.http.get也应该可以解决问题。
标签: javascript vue.js