2017-08-07 6 views
0

同じインスタンスに対して新しい属性を作成するときのオブジェクトのデフォルト動作は、プロパティを変更して古いものを参照するということです。だから、私は段落の変更の内側にボタンをデータをクリックしvuejsを使ってオブジェクトを参照しないようにします

export default { 
    data() { 
     return { 
      paragraph: { 
       text: "", 
       fontSize: 14, 
       key: "Paragraph", 
       align: "left" 
      } 

     } 
    }, 
    methods: { 
     addParagraph() { 
      this.$set(this.paragraph, 'key', this.paragraph.key); 
      this.$set(this.paragraph, 'text', this.paragraph.text); 
      this.$set(this.paragraph, 'fontSize', this.paragraph.fontSize); 
      this.$set(this.paragraph, 'align', this.paragraph.align); 
      this.$store.commit("appendToDocument", this.paragraph) 
     }, 
     alignment(option) { 
      this.paragraph.align = option; 
     } 
    } 

毎回、私はJSONに追加するストアをvuexするデータをPASたい:

私はVUEデータ上でこのような何かを持っています私はパラグラフの木を持つことができます、問題は、そのeverttime私は前に作成した私の他の段落の値を変更する新しいパラグラップを作成する、それを変更する方法はありますか?

答えて

2

@Potray答えは良いです。しかし、ステージ3(バーコードオペレータ)でBabelを使用している場合は、さらに短くなる可能性があります。その後、その構文ですべてのプロパティをコピーすることができます

addParagraph() { 
    this.$store.commit("appendToDocument", { ...this.paragraph }) 
}, 
+0

どういう意味ですか? –

+0

ここで良い例が見つかりますhttps://github.com/tc39/proposal-object-rest-spread/blob/master/Spread.md この場合、3ドット演算子は意味します: 新しいオブジェクトを作成し、すべてをコピーします。オブジェクトBのプロパティを貼り付けてペーストし、オブジェクトAに追加します。 基本的には、{... this.paragraph}では、どのような@potrayコードを行うのですか? 配列のスプレッド/レスト演算子に慣れているかもしれません。それはES2015に上陸しました。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator。 オブジェクトのスプレッド/レットはほとんど標準です –

+0

ちょっと考えてください、何が参考になるのですか、IDは気にしませんか? D –

2

これを試してみてください:

addParagraph() { 
     var paragraph = { 
      key: this.paragraph.key, 
      text: this.paragraph.text, 
      fontSize: this.paragraph.fontSize, 
      align: this.paragraph.alignkey, 
     } 
     this.$store.commit("appendToDocument", paragraph) 
    }, 
+0

私はもう一度書く必要があるので、私のお気に入りのソリューションではありませんが、それは動作します:D、ありがとうございます! –

関連する問題