diff --git a/src/api/products.js b/src/api/products.js new file mode 100644 index 0000000..b3b6423 --- /dev/null +++ b/src/api/products.js @@ -0,0 +1,18 @@ +//商品のリスト +const database = [ + {id:1, name:'商品A', price:345, content:'商品Aの紹介です'}, + {id:2, name:'商品B', price:9876, content:'商品Bの紹介です'}, + {id:3, name:'商品C', price:123, content:'商品Cの紹介です'}, + ] +//インポート先で使用できる関数をオブジェクトとしてまとめたもの +export default { + // databaseを取得 + fetch(id) { return database }, + // idパラメーターに従ってdatabaseからdataを抽出 + find(id) { return database.find(el => el.id === id) }, + anyncFind(id, callback) { + setTimeout(() => { + callback(database.find(el => el.id === id)) + }, 1000) + } +} \ No newline at end of file diff --git a/src/router.js b/src/router.js index cc7e261..2427e2a 100644 --- a/src/router.js +++ b/src/router.js @@ -16,8 +16,12 @@ const router = new VueRouter({ routes: [ { path: '/', component: Home}, { path: '/product', component: ProductList}, - // { path: '/product/:id', component: Product}, :idとしておけばパラメータに可変の値を入れられる - { path: '/product/:id(\\d+)', component: Product}, //(\\d+)を付ければパラメータには数字しか入らない正規表現となる + + + { path: '/product/:id(\\d+)', + component: Product, + // propsにidとデータ型の指定をする + props: route => ({ id: Number(route.params.id) })}, ] }) diff --git a/src/views/Product.vue b/src/views/Product.vue index d55c521..ff2b868 100644 --- a/src/views/Product.vue +++ b/src/views/Product.vue @@ -3,6 +3,12 @@

商品情報

-

このページは、ID.{{ $route.params.id }}の詳細を表示する

+

このページは、ID.{{ id }}の詳細を表示する

- \ No newline at end of file + + + \ No newline at end of file diff --git a/src/views/ProductList.vue b/src/views/ProductList.vue index 7a37217..30c5961 100644 --- a/src/views/ProductList.vue +++ b/src/views/ProductList.vue @@ -1,5 +1,21 @@ \ No newline at end of file + + + \ No newline at end of file