【问题标题】:The `success` method has been deprecated. Use the `then` method instead`success` 方法已被弃用。改用 `then` 方法
【发布时间】:2016-04-09 03:12:42
【问题描述】:

您好,我是 Vue 世界的新手,这是我收到的警告: 成功方法已被弃用。请改用 then 方法。

这里是代码:

apiURL = 'api/movies';

new Vue({
   el: '#app',

   data: {
      'movies': ''
   },

   ready: function() {
      this.getMovies();
   },

   methods: {
      getMovies: function() {
         this.$http.get(apiURL, function(movies) {
            this.$set('movies', movies);
         });
      }
   }
});

这也是做这种事情的正确方法吗?

【问题讨论】:

    标签: vue.js


    【解决方案1】:

    你可以这样做:

    this.$http.get('/').then(function (response) {
        this.$set('movies', response.data);
    }
    

    总而言之,vue-resource 有点毛病和粗糙。如果您使用最新版本,唯一的解释是开发人员使用了他自己已弃用的方法。即success 而不是then

    【讨论】:

    • 这可行,但我需要使用v-for="movie in movies.data insted of v-for="movie in movies
    【解决方案2】:

    您的 GET 请求应使用 then 承诺,如下所示:

    this.$http.get(apiURL).then(function (movies) {
        this.$set('movies', movies);
    });
    

    如vue-resource自述页面所示:https://github.com/vuejs/vue-resource#example

    弃用警告来自这一行:https://github.com/vuejs/vue-resource/blob/ed85a38a1c88faf4e1ac1d7c15cca8376aa933c8/dist/vue-resource.js#L853

    要回答您的最后一个问题,您的方法本身并没有错。

    【讨论】:

    • 这可行,但我需要使用v-for="movie in movies.data insted of v-for="movie in movies"
    • @user3806764 将电影设置为数据属性后,您可以使用 v-for 指令列出电影中的每部电影。 vuejs.org/guide/list.html。否则,您可以使用普通的 JavaScript for 循环来遍历电影。
    猜你喜欢
    • 1970-01-01
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 2015-02-01
    • 1970-01-01
    • 2016-06-08
    • 2012-09-20
    相关资源
    最近更新 更多