前提・実現したいこと
特定の要素をクリックするとaxiosでjsonを取得してその値を子コンポーネントに渡したいのですが、うまくいかないのでご教授頂きたくお願いいたします。
取得したjsonは親の変数に保存してpropsで子コンポーネントに渡すという処理です。
また、axiosによるjsonの取得は複数のmethodsで使用するため、個別にメソッドとして作成してクリックイベントで読み込む処理にしています。
例としてconsole.log()で出力しているだけですが、1回目のクリックでは子コンポーネントのtoChildに渡されず2回目のクリックで"toChild"propsに値が代入されていることが確認できています。
該当のソースコード(一部を抜粋)
親
php
1<html> 2... 3<button @"onClick(id)"></button> 4... 5<modal :to-child="child"></modal> 6<script> 7 data: { 8 items: "", 9 child: "", 10 }, 11 methods: { 12 onClick: function(id) { 13 thi.getitems(id); 14 this.child = this.items; 15 }, 16 17 getItems: function (id) { 18 axios.get("./url", { 19 params: {id: id}, 20 }).then( 21 function (res) { 22 this.items= res.data; 23 }.bind(this) 24 ).catch(function (e) { 25 console.error(e); 26 }); 27 }; 28</script> 29</html>
子
vue
1<template> 2 ... 3</template> 4export default { 5 props: ["toChild"], 6 mounted: function () { 7 console.log("to-child : mouted ↓ "); 8 console.log(this.toChild); 9 }, 10 updated: function () { 11 console.log("to-child updated ↓ "); 12 console.log(this.toChild); 13 }, 14};
以上よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/03/13 06:04