鍍金池/ 問(wèn)答/動(dòng)漫  HTML/ vue數(shù)據(jù)更新了,視圖沒(méi)有更新,但是在不刷新頁(yè)面的情況下子組件中調(diào)用create

vue數(shù)據(jù)更新了,視圖沒(méi)有更新,但是在不刷新頁(yè)面的情況下子組件中調(diào)用created,視圖就更新了,但是一刷新就又不更新了

功能是點(diǎn)擊多選框,切換表格的表頭數(shù)據(jù),在頁(yè)面中的data已經(jīng)定義好了,三個(gè)不同的表頭,
數(shù)據(jù)是更新了,就是試圖更新的是不完整的,比如有10列數(shù)據(jù),展示出來(lái)就只有5列,詭異的是,不刷新頁(yè)面的時(shí)候,我只要在子組件的created中隨便打印個(gè)什么,試圖就更新正常,但是一刷新就又缺失了。
我已經(jīng)試過(guò)了this.$set,不行。

<template>
<!--頁(yè)面中的選擇器-->
         <el-select v-model="sectionValue" placeholder="查看計(jì)劃" @change="changeData" >
              <el-option
                v-for="item in dateSection"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
<!--表格-->
        <table :columns="columnsData"
                   :columnsTable="columnsTier"
                   :table="dataSource">
        </table>
</template>
<script>
data(){
    return {
        columns:[
            {
                name:1,
                age:2,
            },
            {
                tierData:{
                   text:'多級(jí)表頭',
                   content:[
                       { 
                           name:1,
                           age:2,
                       }
                   ] 
                }
                
                
            },
        ],
        mColumns:['結(jié)構(gòu)和columns相同,就是少一個(gè)多級(jí)表頭的對(duì)象'],
        yColumns:['結(jié)構(gòu)和columns相同,就是少一個(gè)多級(jí)表頭的對(duì)象'],
        sColumns:['結(jié)構(gòu)和columns相同,就是少一個(gè)多級(jí)表頭的對(duì)象'],
    }
},
 computed:{
      columnsData(){
        return this.columns.filter(function(v){
          if(v.text !== undefined){
            return v
          }
        })
      },
      columnsTier(){
        return this.columns.filter(function(v){
          if(v.tierData !== undefined){
            return v
          }
        })
      },
}
methods:{
      changeData(value){
        console.log(value);
        if(!value){
          alert(0);
          return;
        }
        else if(value === '當(dāng)月計(jì)劃'){
          alert(1)
          this.columns=this.mColumns
        }else if(value === '當(dāng)季計(jì)劃'){
          alert(2)
           this.columns=this.sColumns
        }else if(value === '當(dāng)年計(jì)劃'){
          alert(3)
          this.columns=this.yColumns
      },
}
</script>

子組件

子組件:
<template>
//部分視圖代碼不全,使用遍歷的方式,將數(shù)據(jù)循環(huán)出來(lái)
<el-table-column v-for="(list,index) in columns"
              :key="list.text"
              :label="list.text"
              :prop="list.dataIndex">
          <template scope="scope">
              {{scope.row[list.dataIndex]}}
          </template>
 </el-table-column>
</template>
<script>
Props:{
    // 接收父組件數(shù)據(jù)
      columns: {
        type: Array,
        default: function () {
          return []
        }
      },
      columnsTable: {
        type: Array,
        default: function () {
          return []
        }
      },
}
created(){
      console.log(this.columns); //打印出來(lái)是新數(shù)據(jù),單數(shù)不更新
      console.log(this.columnsTable);//打印出來(lái)是新數(shù)據(jù),單數(shù)不更新
},
</script>
回答
編輯回答
喜歡你

this.$nextTick(()=>{

this.$set(......)

})

2017年3月11日 19:11
編輯回答
澐染

你 key 等于一個(gè)隨機(jī)數(shù),當(dāng)隨機(jī)數(shù)一樣的時(shí)候還是不行
還不如 v-for="(v, i) in xxx" :key="i"

2017年11月12日 12:51
編輯回答
孤慣

子組件中定義兩個(gè)變量來(lái)存從父組件傳過(guò)來(lái)的值,然后用watch監(jiān)聽(tīng)columns、columnsTable這兩個(gè)值,在watch中給子組件中的變量賦值,試一下

2017年4月12日 23:44
編輯回答
爆扎

自問(wèn)自答一下:這個(gè)原因處于element-ui的bug,具體的原因問(wèn)百度,解決的方法就是:key='改成一個(gè)隨機(jī)函數(shù)Math.random()',就可以了

2017年7月21日 20:47