Vue.js: v-bind:class 在 axios 请求之后

Vue.js: v-bind:class after axios request

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

这是我目前的情况:

<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/article_status/{article_id}”的 api 调用有效,因此 ajax 调用有效.但是 return 语句没有到达 v-bind:class...

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

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'
            }
          }
        })
      }
    )
  }

}

现在您唯一需要做的就是像这样绑定 class:

 <tr v-for="article in articles">
    <i class='fa fa-cog' :class='article.class'> 
 </tr>