2017-02-28 16 views
0

App.vueとします。 Query.vueResult.vueはその子です。vue.jsのコンポーネント間でデータをやり取りする最善の方法は何ですか?

とルータは、次のようになります

import Query from '@/components/Query' 
import Result from '@/components/Result' 

export default new Router({ 
    routes: [ 
    { 
     path: '/', 
     name: 'query', 
     component: Query 
    }, { 
     path: '/result', 
     name: 'result', 
     component: Result 
    } 
    ] 
}) 

Query.vueでボタン、クリックされたとき、それがAPIにPOSTリクエストが送信されますがあります:Query.vue

this.$http.post(url, { 
    arg1: arg1, 
    arg2: arg2 
    }).then(function (response) { 
    data = response.data 

    // I want to do something like: 
    this.$router.push({path: 'result', payload: data}) 

    }, function (response) {}) 

そして、私は​​を処理できます。

しかし、これは不可能なようです。それを作るのにstoreを使うべきですか?

this.$http.post(url, { 
    data: "some data" 
    }).then(function (response) { 
    store.store(response.data) 
    this.$router.push({path: 'result'}) 
    }, function (response) {}) 

答えて

0
  1. あなただけのいくつかのデータは、親から子への一方向の渡す必要がある場合 - 利用PROPSを。
  2. あなたは親に子供からのデータを渡す必要がある場合 - のUSE EVENTS
  3. あなたは多くのコンポーネント(ない親/子)間で同期いくつかのデータを保持する必要がある場合 - 使用VUEX
+0

私はいくつかを渡す必要がありますデータは一方向ですが、 'Result.vue'は' Query.vue'の子ではありません。だから 'vuex'を使う? – HungryMilk

+0

はい。それは最良の方法であり、Vue –

+0

によって推奨されています。どうもありがとう! – HungryMilk

関連する問題