前提・実現したいこと
Vue.jsとLaravelを組み合わせてレビューサイトを作成しています。
メインのページ('/')
発生している問題・エラーメッセージ
created(){
this.getShares(),
}
メインのページ('/')にて上記のメソッドを使用しデータを取得して表示しています。
一度目の描画後、新しいレビューを投稿するページへ行き、POSTした後にrouter.push('/')でメインのページに戻るのですが新しく投稿したレビューが表示されませんでした。
なのでwatchを使用してデータの変更があった際に this.getShares()を呼び出すことで新しいレビューも画面上には描画されるようになりました。
ですがこれでは一瞬だけ以前のページが表示されてしまいますし、コンソールで(Too Many Requests)とエラーが出てしまいます。
どのように対応すればcreated(){this.getShares()}のメソッドを遷移時に毎回発火させることが可能になりますか?
export
1 data(){ 2 return{ 3 user_id:this.$store.state.user.id, 4 login:this.$store.state.auth, 5 reviews_length:"", 6 posteds:[], 7 user_review:[], 8 shares:[ 9 ], 10 } 11 }, 12watch:{ 13 shares:function(){ 14 this.getShares(); 15 }, 16}, 17 methods:{ 18 async getShares(){ 19 const sharesdata= await axios.get("https://intense-falls-67346.herokuapp.com/api" 20 ); 21 this.shares=sharesdata.data.data; 22 }, 23async getUserReview(){ 24let data=[]; 25const reviews= await axios.get( 26"https://intense-falls-67346.herokuapp.com/api/user_review?id="+this.$store.state.user.id); 27 data.push(reviews.data.data); 28 this.reviews_length=data[0].length; 29 this.user_review=data[0]; 30 } 31}, 32 created(){ 33 this.getShares(), 34 this.getUserReview() 35 }, 36};
試したこと
コンポーネントの再利用をなくすため
<router-view :key="$route.fullPath"></router-view>
を使用しましたがダメでした。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/04/29 08:06
2021/04/29 09:32
2021/04/29 09:49
2021/04/29 15:11