Vue2.0在工程中加入vue-resource--一个简单示例

来源:互联网 发布:索尼smartwatch2 软件 编辑:程序博客网 时间:2024/05/17 21:55

本文主要参考了Vue2.0 新手完全填坑攻略—从环境搭建到发布——Jinkey 原创。跟其主要区别在于本文使用了element ui和其提供的element starter,同时本文不涉及vue-resource。之前的构建过程见一个初学者的vue2.0练习程序。源码可以在github看到。

  1. 首先在main.js中引入vue-resource

    import VueResource from 'vue-resource'
    ...
    Vue.use(VueResource)
  2. 在secondCom.vue中使用vue-resource

    <script>export default {data() {  return {        ...  }},mounted: function() {  this.$http.jsonp('https://api.douban.com/v2/movie/top250?count=10', {}, {      headers: {      },      emulateJSON: true  }).then(function(response) {    // 这里是处理正确的回调      this.articles = response.data.subjects  }, function(response) {      // 这里是处理错误的回调      console.log(response)  });}  }</script>
原创粉丝点击