0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SPAの詳細ページ実装を簡単にまとめる

Posted at

概要

vue.jsでアプリを作る際、詳細画面をどのように実装するのか少しわからなったので、自分が理解した範囲でひとつの方法をまとめていきたいと思います。

今回は、vue.jsのrouterを使用します。

routerを定義
index.ts
const routes = [
  {
    path: '/',           //パスの設定
    component: Index,    //表示するコンポーネント
    name: 'index',       //ルートの名前を指定
  },
  {
    path: '/search',            
    component: Search,
    name: 'Search',
  },
  {
    path: '/:id',            
    component: Detail,
    name: 'Detail',
  },
]

検索(Search)ページ
Child.vue
<template>
</template>

<script lang="ts">
import {Vue} from 'vue'

class Child extend Vue {
    /** 下記の方法でパラメータとして渡されたIDを読み取ることが可能 */
    private id = this.route.params.id
    /** 詳細ページへ遷移 */
    private router(){
        this.$router.push(`/ ${this.id}`)
}  
</script>

↓ idは、routerのpathに指定しているものと同様にする必要がある。

this.route.params.id

もしも、pathを'/:name'としていた場合、this.route.params.nameになる。

他にも実装方法はあるとは思いますが、こちらの方法が簡単な方法ではないかなと思います。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?