js
1data () { 2 return { 3 list: [ 4 { 5 id: 1, 6 title: 'hoge', 7 image: 'block.jpg' 8 } 9 ] 10 } 11}
があったとして、
js
1axios 2.get(url) 3.then(function (res) { 4 this.list.push({ id: res.data.id, title: res.data.title, image: res.data.image }) 5})
ていう感じに追加したいけど、v-forが更新されません。
調べてみたところ、レンダリングができていないところまではわかったのですが、どう対策したらいいかがわかりません…
解決策わかる方いましたらご教授お願いしますm(_ _)m
追記:全体
vue
1<template> 2 <v-ons-page> 3 <div class=""> 4 <p style="margin-left:15px; float:left;">参加中のカテゴリー</p> 5 <p class="join-category-link" @click="CategoryEdit">編集</p> 6 </div> 7 <div style="clear:both;"></div> 8 <component 9 :is="JoinCategoryPage" 10 :page-stack="pageStack"></component> 11 <p style="margin-left:15px;">すべてのカテゴリー</p> 12 <component 13 v-for="category in allCategories" 14 :is="CategoryIndexPage" 15 :key="category.id" 16 :title="category.title" 17 :image="category.image" 18 PropsWidth="25" 19 :page-stack="pageStack" 20 class="all-category" 21 ></component> 22 </v-ons-page> 23</template> 24<script> 25import JoinCategory from '../Category/components/JoinCategory' 26import MenuPage from './MenuPage' 27import CategoryIndex from '../Category/Index' 28const axios = require('axios') 29export default { 30 name: 'home', 31 props: ['pageStack'], 32 data () { 33 return { 34 title: 'title', 35 CategoryIndexPage: CategoryIndex, 36 JoinCategoryPage: JoinCategory, 37 allCategories: [ 38 { 39 id: 1, 40 title: 'hoge', 41 image: 'block.jpg' 42 } 43 ] 44 } 45 }, 46 created: function () { 47 const self = this 48 this.axios 49 .get(process.env.BASE_URL + 'all_category') 50 .then(function (res) { 51 self.allCategories = res.data 52 self.allCategories.push({ title: 'hogehoge', image: 'block.jpg' }) 53 console.log(self.allCategories) 54 }) 55 }, 56 mounted: function () { 57 axios 58 .get('https://api.coindesk.com/v1/bpi/currentprice.json') 59 .then(response => { 60 }) 61 .catch(error => { 62 console.log(error) 63 this.errored = true 64 }) 65 }, 66 methods: { 67 push () { 68 this.pageStack.push(MenuPage) 69 }, 70 CategoryEdit () { 71 console.log('CategoryEdit READY!') 72 } 73 } 74} 75</script> 76 77<!-- Add "scoped" attribute to limit CSS to this component only --> 78<style lang="scss"> 79.center { 80 text-align: center; 81} 82.join-category-link { 83 float: right; 84 margin-right: 15px; 85 color: gray; 86} 87 88</style> 89
HTML側には問題ありませんか?
v-forが更新されません、という質問であればその該当のv-forを含むHTMLテンプレート部分を提示して質問してください。
いま質問ページを更新しました(シンタックスの関係で)おそらく問題ないはずです…
なお、 axiosのコードそのものが実行されていない可能性もあるので、部分的に切り出さずに全部提示することをおすすめします。
とりあえず一旦コンポーネント全体を載せました。axiosはconsole.logで都度確認し、実行されてdataに変更があり、変更あったデータを呼び出すところまでは確認しています。
もう一つ補足になりますが、今回詰まっている箇所はレンダリングであるところまでは想像できています。が、それの更新の仕方がわからない状態です…
回答1件
あなたの回答
tips
プレビュー