親(呼び出し側の記述)
index.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import Vue from "vue" import childComponent from "./components/child.vue" const v = new Vue({ el: "#app" , components: { childComponent, }, data() { return { parentValue: "test" , } }, methods: { // }, }) |
index.html(抜粋)
1 2 3 | < div id = "app" > < child-component >( v-bind:child-value="parentValue" ) </ div > |
上記は動的に親のプロパティを受け渡すケース。
直値の場合child-value="test"のように書く。
child-value ⇔ childValue(子コンポーネントのプロパティ) に変換される。
子コンポーネントの記述
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <template> <div> <!-- --> </div> </template> <script lang= "ts" > import Vue from "vue" export default Vue.extend({ props: { childValue: String, }, data() { return { // } }, /** Vue構築完了時の処理 */ created: function init() { console.log( this .childValue) }, methods: { // }, }) </script> |
1 2 3 | props: { childValue: String, }, |
参考
https://jp.vuejs.org/v2/guide/components.html
Vueで子から親へのコールバック形式による値受け渡し (TypeScript) も一緒に見ておくといいかと