javascript 开始使用 vue-resource
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32082488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting started with vue-resource
提问by Nicolas Marshall
Struggling to find any pre-made examples of use for the vue-resource plugin of vue.js, I tried this :
努力寻找 vue.js 的 vue-resource 插件的任何预制示例,我尝试了这个:
<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>
<div id="my_view">
<p>{{ origin }}</p>
</div>
<script>
var Vue = require('vue');
Vue.use(require('vue-resource'));
new Vue({
el: '#my_view',
data: {
origin: ''
},
ready: function() {
// GET request
this.$http.get('http://httpbin.org/ip', function (data, status, request) {
// set data on vm
this.$set('origin', data)
}).error(function (data, status, request) {
// handle error
})
}
})
</script>
to just query httpbin.org/ip(a random REST endpoint i could find) and display the result inside #myview > p
. It's just the example (an adapted version) provided on the vue-resource github pagethat I'm trying to run.
只需查询httpbin.org/ip(我能找到的随机 REST 端点)并在#myview > p
. 这只是我试图运行的vue-resource github 页面上提供的示例(改编版本)。
Can anyone see what I'm not getting right to achieve this ?
任何人都可以看到我没有正确实现这一目标吗?
Edit: added comma, and hereis the fiddle of it.
编辑:添加了逗号,这是它的小提琴。
回答by bobbybackblech
Its because you are using require
. If you use require
you need some lib like http://browserify.org/
这是因为您正在使用require
. 如果你使用require
你需要一些像http://browserify.org/ 这样的库
This example is now working: http://jsfiddle.net/dccbbkam/2/
这个例子现在正在工作:http: //jsfiddle.net/dccbbkam/2/
And here is another example for you: http://jsfiddle.net/dccbbkam/4/
这是你的另一个例子:http: //jsfiddle.net/dccbbkam/4/