6

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.

1
  • Any syntax or other errors in the javascript console? There's a comma missing before "ready". Can help further if you turn this into a fiddle. Commented Aug 19, 2015 at 2:49

1 Answer 1

7

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/

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks ! no need to precise I'm starting with javascript, I think you spotted that already :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.