Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting started with vue-resource

Struggling to find any pre-made examples of use for the vue-resource plugin of vue.js, I tried this :

<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 page that I'm trying to run.

Can anyone see what I'm not getting right to achieve this ?

Edit: added comma, and here is the fiddle of it.

like image 490
Nicolas Marshall Avatar asked Aug 18 '15 21:08

Nicolas Marshall


Video Answer


1 Answers

Its because you are using require. If you use require you need some lib like http://browserify.org/

This example is now working: http://jsfiddle.net/dccbbkam/2/

And here is another example for you: http://jsfiddle.net/dccbbkam/4/

like image 180
bobbybackblech Avatar answered Sep 25 '22 02:09

bobbybackblech