実現したいこと
vue-cli3.5.0環境でのmountedメソッドの呼び出し方法
Vuetifyのサンプルを参考に作成しました。
発生している問題・エラーメッセージ
Items.vue
にmountedメソッドを定義したが、http://localhost:8082/item
にアクセスしたときgetItems ()
の結果が表示されません。
該当のソースコード
Items.vue
vue.js
1<template> 2 <v-data-table 3 :headers="headers" 4 :items="itemList" 5 6 class="elevation-1" 7 > 8 <template slot="items" slot-scope="props"> 9 <td>{{ props.item.id }}</td> 10 <td>{{ props.item.name }}</td> 11 <td >{{ props.item.price }}</td> 12 </template> 13 </v-data-table> 14</template> 15<script> 16 import axios from 'axios' 17 export default { 18 data () { 19 return { 20 itemList: [] , 21 22 headers: [ 23 { text: 'ID', align: 'left', sortable: true, value: 'id'}, 24 { text: '商品名', sortable: true,value: 'name' }, 25 { text: '価格',sortable: false, value: 'price' }, 26 ], 27 } 28 }, 29 methods: { 30 // ユーザ一覧 31 getItems () { 32 this.itemList = []; 33 axios.get("http://localhost:8888/item") 34 .then( response => { 35 this.itemList = response.data; 36 console.log("Index : record num=" + this.itemList.length); 37 }) 38 }, 39 mounted :function () { 40 console.log('mounted'); 41 this.getItems() 42 } 43 44 } 45 46 } 47</script>
main.js
vue.js
1import Vue from 'vue' 2import './plugins/vuetify' 3import App from './App.vue' 4import router from './router' 5import store from './store' 6 7Vue.config.productionTip = false 8new Vue({ 9 router, 10 store, 11 render: h => h(App) 12}).$mount('#app'); 13
App.vue
vue.js
1<template> 2 <v-app> 3 <v-toolbar app> 4 <v-toolbar-title class="headline text-uppercase"> 5 <router-link to="/"> 6 <v-btn color="primary">Home</v-btn> 7 </router-link> 8 <router-link to="/item"> 9 <v-btn color="primary">商品</v-btn> 10 </router-link> 11 </v-toolbar-title> 12 <v-spacer></v-spacer> 13 </v-toolbar> 14 <v-content> 15 <router-view/> 16 </v-content> 17 </v-app> 18</template> 19 20<script> 21export default { 22 name: 'App', 23 components: { 24 }, 25 data () { 26 return { 27 // 28 } 29 }, 30} 31</script> 32
router.js
vue.js
1import Vue from 'vue' 2import Router from 'vue-router' 3import Home from './views/Home.vue' 4import Item from './views/Items.vue' 5 6 7Vue.use(Router) 8 9export default new Router({ 10 mode: 'history', 11 base: process.env.BASE_URL, 12 routes: [ 13 { 14 path: '/', 15 name: 'home', 16 component: Home 17 }, 18 { 19 path: '/item', 20 name: 'item', 21 component: Item 22 } 23 ] 24}) 25
index.html
index.html
1<!DOCTYPE html> 2<html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 6 <meta name="viewport" content="width=device-width,initial-scale=1.0"> 7 <link rel="icon" href="<%= BASE_URL %>favicon.ico"> 8 <title>golangEC</title> 9 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900"> 10 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Material+Icons"> 11 </head> 12 <body> 13 <noscript> 14 <strong>We're sorry but golang_ec doesn't work properly without JavaScript enabled. Please enable it to continue.</strong> 15 </noscript> 16 <div id="app"></div> 17 <!-- built files will be auto injected --> 18 </body> 19</html> 20
試したこと
Items.vue
のgetItems ()
の結果を取得できることはコンソールに出力することで確認できました。
下記が取得できるjsonデータです。
{"items":[{"id":1,"name":"APPLE","price":100},{"id":2,"name":"ORANGE","price":300},{"id":3,"name":"BANANA","price":200}]}
mountedメソッドに記述してあるconsole.log('mounted')
でコンソール上に出力されないことからmountedメソッドの定義方法が原因だと思っています。
どうかお力添えをお願い申し上げます。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/06/26 13:27