2017-11-23 10 views
1

Vuex localcomputedオブジェクトとストアマッピングとのget/setを組み合わせると、構文エラーが発生します。getterとsetterを組み合わせたlocalcomputed関数によるVuex構文エラー

あなたのようなオブジェクトの広がりoperaterで、このようなあなたの店のメソッドをマッピングすることができVuexのドキュメントで見られるように

computed: { 
    fullName: { 
    // getter 
    get: function() { 
     return this.firstName + ' ' + this.lastName 
    }, 
    // setter 
    set: function (newValue) { 
     var names = newValue.split(' ') 
     this.firstName = names[0] 
     this.lastName = names[names.length - 1] 
    } 
    } 
} 
:あなたのような計算されたセッターを作成することができます。また

computed: { 
    localComputed() { /* ... */ }, 
    // mix this into the outer object with the object spread operator 
    ...mapState({ 
    // ... 
    }) 
} 

https://vuex.vuejs.org/en/state.html##object-spread-operator

https://vuejs.org/v2/guide/computed.html#Computed-Setter

I get setを持つ計算オブジェクトを作成することも、mapState/mapGettersなどを作成することもできますが、組み合わせることはできません。それは構文を破る(エラーは:関数宣言の後の期待される関数名)。

computed: { 
     localComputed() { 
      localMethod: { 
       get: function() { 
         // retrieve 
       }, 
       set: function (newContent) { 
        // set 
       } 
      } 
     }, ...mapState([ 

         ]), ...mapGetters([]) 

    } 

これら2つをどのように組み合わせますか?

答えて

2

localMethodlocalcomputedに定義しようとしています。

ドキュメントのlocalComputedは、コンポーネント内の計算されたプロパティの単なる名前の例です。その中に他のローカル計算されたプロパティをすべて入れる必要はありません。

したがって、次の操作を行うことができる必要があります:

computed: { 

    localComputed: { 
    get: function() { 
     // retrieve 
    }, 
    set: function (newContent) { 
     // set 
    } 
    }, 

    anotherLocalComputed: { 
    get: function() { 
     // retrieve 
    }, 
    set: function (newContent) { 
     // set 
    } 
    }, 

    ...mapState([]), 

    ...mapGetters([]) 

} 
関連する問題