【发布时间】:2017-04-20 07:27:15
【问题描述】:
html>
<head>
<title>Vue App</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.3/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/0.12.7/vue.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue-resource/0.1.13/vue-resource.min.js"></script>
</head>
<body>
<!--
<h1>
<a href="http://localhost:1337/"> Home Page</a>
</h1>
<h1>
<a href="http://localhost:1337/form"> Form Page</a>
</h1>
-->
<div id="my_view">
<p>{{ responseOptions| json }}</p>
<br />
<p>{{ origin | json }}</p>
</div>
<script>
new Vue({
el: '#my_view',
data: {
origin: ''
},
//it is showing an empty array. It's not returning any data.
//leave name:"name" out and it returns the whole object?
// example from https://github.com/pagekit/vue- resource/blob/master/docs/http.md
ready: function () {
var resource = this.$resource('https://jsonplaceholder.typicode.com/users');
resource.get({name: "name"}).then((response) => {
this.$set('responseOptions', response.status)
this.$set('origin', response)
});
}
})
</script>
</body>
</html>
你好, 试图弄清楚 vue-resource $http 对 api 的访问是如何工作的。所以创建了这个基本页面,当我将它加载到浏览器时,我得到的只是一个空数组。我按照官方页面(https://github.com/pagekit/vue-resource/blob/master/docs/http.md)上发布的说明进行操作
我能够在页面上显示 response.status,因此脚本正在通信,但根本不显示数据。如果您访问页面 (https://jsonplaceholder.typicode.com/users) 数据就在那里。
我做错了什么? 请指教...
【问题讨论】:
-
如果你使用的是 Vue 2,ready hook 已被弃用 - 改用mounted,或者更好的选择是created。还有什么是
this.$resource?在那里你应该定义this.$http
标签: javascript vue.js vue-resource