【问题标题】:Vue.js: unwanted effect every time the view is renderedVue.js:每次渲染视图时都不需要的效果
【发布时间】:2021-06-17 15:39:44
【问题描述】:

问题是在视图之间浏览时,会导致这种效果。首先呈现状态活动(灰色),然后呈现状态完成(红色)。我不知道我是以错误的方式构建它还是它的正常行为

我在这个视图中有以下内容:

模板

 <div class="col"  v-for="item in category" v-bind:key="item.id">
    <router-link :id="item.id" class="btn-category" :class="item.status"
       v-bind:to="{ name: 'words', params : { id: item.id} }" >
         <span>{{item.content}}
           <div class="icon-circle" > 
              <icon v-if="item.status == 'cat-done'" name="check" class="icon icon-s"></icon>
              <icon v-else name="arrow-go" class="icon icon-s"></icon>
           </div> 
         </span>
    </router-link>
 </div>

然后

data(){
    return {
      id:'',
      category:[],
      countCatDone:0,
    }
 },
 created(){
        this.getCatLevel()
    },

 getCatLevel(){
    this.id = this.$route.params.id
        db.collection("da-content").where("contenido_id", "==", this.$route.params.id )
        .get()
        .then( query=> {    
            let items = [] 
            query.forEach( doc => {
                let object = doc.data();
                object.id = doc.id;
                object.status = "null";
                db.collection("da-content-log").where("contenido_id", "==", doc.id)
                .where("usuario_id", "==", this.user.email )
                .get()
                .then(respond => {
                    if(respond.empty){
                        object.status = "cat-active";
                    } else {
                        respond.forEach((doc) => {
                            object.status = doc.data().status;
                            if (object.status == "cat-done") {
                                this.countCatDone++
                            }
                        })
                    }    
                })
                .catch(function(error) {
                    console.log("Error getting documents: ", error);
                });
                items.push(object);
            });
            this.category = items;                        
            
        })
},

谢谢!

【问题讨论】:

    标签: vue.js view vue-component vue-router rerender


    【解决方案1】:

    我认为这是正常行为,因为选中类别的数据取自this.getCatLevel(),这是一个async 函数,因此您的项目将在数据完全加载之前提前呈现。我的建议是在 UI 中添加一些 loading 信息,或者在数据未完全加载时隐藏它,例如添加数据变量 isLoading 然后在 getCatLevel() 中调用它,例如:

    getCatLevel(){
    this.isLoading = true; //set the loading value to true before calling data
    ....
    .catch(function(error) {
      console.log("Error getting documents: ", error);
    }).finally(()=> {this.isLoading = false}); // set loading value to false when data is fully loaded
    ....
    

    然后在元素中调用它

    <div v-if="!isLoading" class="col"  v-for="item in category" v-bind:key="item.id">
        <router-link :id="item.id" class="btn-category" :class="item.status"
           v-bind:to="{ name: 'words', params : { id: item.id} }" >
             <span>{{item.content}}
               <div class="icon-circle" > 
                  <icon v-if="item.status == 'cat-done'" name="check" class="icon icon-s"></icon>
                  <icon v-else name="arrow-go" class="icon icon-s"></icon>
               </div> 
             </span>
        </router-link>
     </div>
    

    【讨论】:

    • 谢谢!这改善了渲染。
    • 如果您觉得这有帮助,请点赞或查看答案
    猜你喜欢
    • 2021-12-14
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 1970-01-01
    • 2021-08-11
    • 1970-01-01
    相关资源
    最近更新 更多