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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:49:59  来源:igfitidea点击:

Getting started with vue-resource

javascriptjavascript-frameworkvue.js

提问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 requireyou 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/