Vue.js:v-bind:axios请求后的类

时间:2017-12-12 10:48:25

标签: javascript vuejs2 axios

我有一个动态列表,由api请求通过axios生成。在这个列表中,我有一个元素的类,必须在生成之后生成。

所以这就是我到目前为止所做的:

<template>
    <div>
        <div v-if="article_count > 0" class="table-responsive">

            <table class="table table-striped">
                <thead>
                <tr>
                    <th>state</th>
                    <th>created at</th>
                    <th>headline</th>
                </tr>
                </thead>
                <tbody>
                <tr v-for="article in articles">
                    <td class="status">
                        <!-- the class of this element should change -->
                        <i class="fa fa-cog fa-spin fa-fw" v-bind:class="getArticleStatus(article.id)"></i>
                    </td>
                    <td>{{ article.created_at }}</td>
                    <td><strong>{{ article.headline }}</strong></td>
                </tr>
                </tbody>
            </table>

        </div>

        <div v-else class="text-center">no articles found</div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                article_count: 0,
                articles: []
            }
        },
        methods: {
            // get the articles
            getArticles() {
                axios.get('/api/get_articles'                    )
                .then((response) => {
                    this.article_count = response.data.article_count;
                    this.articles = response.data.articles;
                })
            },
            // get the article's state
            getArticleStatus(article_id) {
                axios.get('/api/article_status/' + article_id)
                .then((response) => {
                    switch(response.data) {
                        case 'ONL':
                            console.log('online'); // this works
                            return 'status online'; // this does not work...

                        case 'WAIT':
                            return 'status waiting';

                        case 'OFFL':
                            return 'status offline';

                        default:
                            return 'status unknown';
                    }
                }).catch((error) => {
                    console.error(error);
                })
            }
        },
        mounted() {
            this.getArticles()
        }
    }
</script>

正如我在控制台(网络标签)中看到的那样,api调用&#34; / api / article_status / {article_id}&#34;工作,所以ajax调用工作。但是return语句没有达到v-bind:class ...

1 个答案:

答案 0 :(得分:1)

如评论中所述,您无法将Promise从函数绑定到元素。更重要的是,有一个更好的选择。你可以使用这样的东西。

..
host=<hostname> rtt=562(280)ms/0ms delta=-3ms/-3ms Tue Dec 12 13:34:26 2017

现在你唯一需要的是像这样绑定类:

methods: {

  getArticles () {
    axios.get(
      '/api/get_articles'
    ).then(
      response => {
        this.article_count = response.data.article_count
        this.articles  = response.data.articles 
      }
    )
  },

  getArticleState (id) {
    axios.get(
      '/api/article_status' + id,
    ).then(
      response => {
        this.articles.forEach(function (article) {
          if(article.id === id){
            switch(response.data) {
              case 'ONL':
               article.class = 'status waiting'
            }
          }
        })
      }
    )
  }

}