Vue.js3で親コンポーネントから子コンポーネントのpropsへ動的な値を渡す方法がわからなくて困っています。
参考URL
https://rennnosukesann.hatenablog.com/entry/2019/09/12/185701
html側の設定として、オブジェクトにラップしたものを v-bind:props-id="{ propsId } のようにし、
配列の文字として受け取るようなことの記載があったのですが、
どなたかご存知であれば教えて下さい。
html
1<div id="app"> 2 <button type="button" v-on:click="testAction()">TestAction</button> 3 4 <modal-component ref="child1" v-bind:props-id="{ propsId }"></modal-component> 5</div> 6 7<script type="text/x-template" id="modal-component-id"> 8 <div> 9 modal component 10 </div> 11</script>
javascirpt
1const modalComponent = { 2 template: "#modal-component-id", 3 4 props: [ "propsId" ], 5 6 data() { 7 return { 8 // 取得できなかった 9 id: this.propsId 10 } 11 }, 12 13 methods: { 14 initialModal() { 15 // 検証用 this.idは失敗している、this.propsIdは1回目は取得失敗、2回目は取得される 16 const id1 = this.id; 17 const id2 = this.propsId; 18 19 console.log("ok"); 20 }, 21 } 22}; 23 24const app = Vue.createApp({ 25 data() { 26 return { 27 propsId: "0" 28 } 29 }, 30 31 components: { 32 "modal-component": modalComponent 33 }, 34 35 methods: { 36 testAction() { 37 // 検証用にランダムの整数文字 1~10 38 this.propsId = Math.floor(Math.random() * 10).toString(); 39 40 this.$refs.child1.initialModal(); 41 } 42 } 43}); 44 45app.mount("#app");
回答2件
あなたの回答
tips
プレビュー