Vue.js3のx-templateを使った時に、親コンポーネントから子コンポーネントのメソッドを実行したいのですが、わからなくて困っています。
どなたか解決方法をご存知であれば教えて下さい。
javascript
1// index.js 2const app = Vue.createApp({ 3 data() { 4 return { 5 rowData: [] 6 }, 7 }, 8 9 methods: { 10 do () { 11 // this.$refs.child2がundefindなのでエラー 12 this.$refs.child2.send(); 13 }, 14 15 receive(data) { 16 this.rowData = data; 17 }, 18 } 19}); 20 21const childComponent2 = { 22 // 親のデータをpropsで受け取る 23 props: { 24 rowData: Array, 25 }, 26 27 // 子で再定義 28 data() { 29 return { 30 childRowData: this.rowData 31 } 32 }, 33 34 methods: { 35 // $emitで親のイベントを実行 36 send() { 37 this.$emit("receive", childRowData); 38 }, 39 }, 40 41 template: "#child-component2" 42}; 43 44app.component("child-component2", childComponent2); 45 46app.mount("#app");
html
1<!-- index.html --> 2<div id="app"> 3 <button type="button" v-on:click="do()"> 4</div> 5 6// Partial view (modal) 7// child-component2.html
html
1<!-- child-component2.html --> 2<script type="text/x-template" id="child-component2"> 3 <div> 4 custom component2★ 5 </div> 6</scirpt> 7 8<child-component2 ref="child2"></child-component2>
2022/02/22 16:17
タイトルと本文を修正しました。
親コンポーネントから子コンポーネントのメソッドを実行したいです。
次のそれぞれの宣言だとは思うのですが、
html : <child-component2 ref="child2"></child-component2>
javascript: this.$refs.child2.send();
回答1件
あなたの回答
tips
プレビュー