2017-08-30 6 views
1

私は何が...このvueが必要なのか理解できません!できる場合は をお手伝いください! これは、「作成」、「読み取り」、「更新」私は4ブールフィールドを持つ例えばcurrentFullDataでVue JSが1つの配列でデータを変更すると、

var groupadding = new Vue({ 
el: '#groupAdd', 
data:{ 
    currentFullData: [], 
    localData: [] 
    }, 
    methods: { 
    getDepartment() { 
     var sendData = id; // this is example 
     this.$http.post('some api ', sendData, {emulateJSON: true}) 
     .then(resp => { 
     this.currentFullData = resp.data;  
     } 
    }, 
    getLocalDepartment() { 
    this.localData = this.currentFullData; 
    } 
    } 
}) 

私のコードで、

「を削除」は、この4つのフィールドには、あまりにものlocalDataを取得しますが、とき私はそれらのいくつかのlocalDataの変更、彼らはあまりにもcurrentFullDataの変更!!!!!!!!!!!!!!!

SOOOOOO誰もがWTFはこれです、私を説明できますか?!?!?!

答えて

1

(あなたがthis.localData = this.currentFullData;を書いたように)あなたが実際に同じオブジェクトを変更しているので、これはあなたがcurrentFullDataを使用してthis.localDataを初期化したい場合は、あなたがそれをコピーする必要があり

です:

this.localData = Object.assign({}, this.currentFullData);

質問のhttps://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

+0

NOP、同じ結果を参照することができ、より理解するために、このthis.localData = this.currentFullData.slice()

ようthis.localDatathis.currentFullDataの参照を渡す... :( – Jorjini

+0

ああNVM、私はそれを見ていません配列であってオブジェクトではありません。クローンと配列のためのanchal20答えを参照してください。 – reinarg

+0

私の答えは申し訳ありません。 this.currentFullData = cloneArray(this.currentFullData、data); function cloneArray($ name、$ array) { リターン$名= JSON.parse(JSON.stringify($配列));} このWOOOOOOOOOOOOOOOOOORKS !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ありがとうございました!!!! – Jorjini

1

感謝を参照してください。問題はvueではなく、データ値 'currentFullData'と 'localData'でもありません。あなたのデータ変数が配列されているとして、あなたはこのthis.localData = this.currentFullDataのように値を割り当てるときに、したがって、その後、this.localDatathis.currentFullDataへのポインタです。したがって、localDataを変更すると、currentFullDataも変更されます。 したがって、これを避けるために、あなたはthis question about arrays on stackoverflow

+0

キャッチされない(約束で)例外TypeError:this.currentFullData.sliceは、あなたが私のcodepen https://codepen.io/anchal20/pen/QMzQQGをチェックしても、私は上記の共有リンクできる機能 – Jorjini

+0

ではありません。私はあなたが正しく入力することを願って、それはthis.currentFullData.slice()でなければならず、resp.dataは配列型でなければなりません。 – anchal20

関連する問題