在组件中声明动态数据
Declaring dynamic data in a component, Vuejs 2
我现在从vue js 2开始。我有这段代码,它从服务器(laravel 5.3应用程序)接收动态数据,问题是当我试图在组件上声明用户数组而不是在Vue()实例中声明(在这种情况下工作得很好):
HTML:<div id="app">
<my-component>
<ul id="users-list">
<li v-for="user in users">{{user.name}}</li>
</ul>
</my-component>
</div>
JS:
Vue.component('my-component', {
template: '#users-list',
data: function () {
return {
users: []
}
},
mounted: function() {
this.$http.get('http://127.0.0.1:8003/api/test').then((response) => {
console.log(response.body);
this.users = response.body;
}, function() {
alert('brrh');
});
}
});
new Vue({
el: '#app',
});
错误信息:"Uncaught ReferenceError: users is not defined"
看起来你正在尝试做一个inline-template
尝试将该指令添加到你的<my-component>
调用。
<div id="app">
<my-component inline-template>
<ul id="users-list">
<li v-for="user in users">{{user.name}}</li>
</ul>
</my-component>
</div>
你还必须从你的组件调用
中删除模板参数 Vue.component('my-component', {
data: function () {
return {
users: []
}
},
mounted: function() {
this.$http.get('http://127.0.0.1:8003/api/test').then((response) => {
console.log(response.body);
this.users = response.body;
}, function() {
alert('brrh');
});
}
});
new Vue({
el: '#app',
});
jsFiddle
相关文章:
- 如何在没有ajax的情况下将javascript动态数据发送到php变量
- 从要使用Protractor测试的服务器异步加载的动态数据列表的列表
- 不适用于动态数据的Angular指令来自$http
- 如何在字符串中的值之前删除字符串中的动态数据?/ 使用 Lodash 的 _.trimStart 与动态数据
- 剑道数据源,将动态数据发送到服务器
- 无限滚动,动态数据按上次修改状态排序
- 如何在高图表中设置动态数据
- 使用Select2和Meteor.js进行动态数据采集
- 谷歌图表与动态数据和分页是可能的
- 使用AngularJs在自定义TinyMCE编辑器中添加动态数据
- 如何为jQuery帖子输入动态数据参数
- 将不同的符号添加到具有动态数据的高图表中
- Aurelia@儿童动态数据
- 从动态数据生成AngularJS UI模板
- 我可以将初始动态数据从基于RESTful api的服务器端传递到基于角度的前端页面吗
- d3.js带有标签的动态数据
- 将sessionStorage(JSON)中的动态数据附加到Listview中
- 如何使用Javascript在html源中添加动态数据
- 试图在网站上抓取谷歌地图api生成的动态数据,但正常抓取返回空白
- 从angularjs中的$scope在$http.post中发送动态数据