vue 添加數組頁面及時顯示


第一次寫博客,寫的不好,還望大家多多包涵。

最近在做vue相關的項目,在過程中,遇到了很多問題,有的解決了,有的還沒解決,其中一個比較好的問題是,一個評論回復功能,點擊回復,可以把內容獲取到並且加入數組中,但頁面不會及時顯示。

經過很多百度摸索,終於解決了。

讓我們一起來看看吧。

//newwrite是定義的一個數組
//push進去用戶名和輸入的內容,changeitems
//changeitems是我監聽的輸入框的內容 
//<textarea id="reply_text" cols="30" rows="10" v-model="changeitems"></textarea>
//
this.newwrite.push({
          user_id:this.userid,
          req_content:changeitems
        })
console.log(this.newwrite);

 

 

由於我有的地方是用的二維數組,所以這種push的方法就不能及時顯示到頁面上去

js代碼:

//這的items是一個二維數組 
//多個評論下的回復
//點擊添加到對應的評論

_this.items[index].push({
       user_name:_this.username,
       user_id:_this.userid,
       req_content:text
 })

html代碼:

<div  v-for="(item,indexss) in items[index]" :key="indexss">
  <span class="my_commentname">{{username}} : </span><span class="my_comment">{{item}}</span>
</div>

 

能傳入到數組中,不能顯示在頁面上

 

因此就用了另一種方法,Vue.set(this.arr, this.arr.length, text);

其中這里的this要提前定義結構

js代碼:

//a=[]
//此處_this=this
_this.items[i] = new Array(); _this.a.push(_this.items[i]);

//點擊事件中:
  Vue.set(this.a, this.a.length, text);

html代碼:

<!---->
<
div v-for="(item,indexss) in a" :key="indexss"> <span class="my_commentname">{{username}} : </span><span class="my_comment">{{item}}</span> </div>

然后點擊回復就可以及時顯示到頁面上了

 

希望對大家有幫助


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM