Vue Router

URL에 λ”°λ₯Έ λ‹€λ₯Έ component rendering

Router κ°€ ν•„μš”ν•œ 이유

  • SPA 의 νŠΉμ„±μ„ μ‚΄λ¦¬λ˜, URL을 달리 ν•˜κΈ° μœ„ν•΄

    • URL에 λ”°λ₯Έ λ‹€λ₯Έ component rendering

  • Route/view mapping

Vue 의 νŠΉμ§•

  • μž‘κ³  κ°€λ³μ§€λ§Œ, ν•„μš”ν•œ κΈ°λŠ₯을 μΆ”κ°€ ν•  수 있게 ν•˜μž!

  • κ·Έλž˜μ„œ router μ‚¬μš©ν•˜λ €λ©΄ λ”°λ‘œ 좔가해야함!

Install Vue Router

일반 μ„€μΉ˜

npm install vue-router

vue의 νž˜μ„ 빌렀 add ν•˜κΈ°

  • Project μ΅œμƒλ‹¨μ—μ„œ μ‹€ν–‰!

$ vue add router

WARN  There are uncommited changes in the current repository, it's recommended to commit or stash them first.
? Still proceed? Yes

πŸ“¦  Installing @vue/cli-plugin-router...

+ @vue/cli-plugin-router@4.4.1
added 2 packages from 1 contributor, updated 1 package and audited 1283 packages in 5.023s

46 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

βœ”  Successfully installed plugin: @vue/cli-plugin-router

? Use history mode for router? (Requires proper server setup for index fall
back in production) Yes

πŸš€  Invoking generator for @vue/cli-plugin-router...
βš“  Running completion hooks...

βœ”  Successfully invoked generator for plugin: @vue/cli-plugin-router
  • κ°€λŠ₯ν•œ project μƒμ„±ν•˜μž 마자 add ν•˜κΈ°!

    • 이런 λ©”μ‹œμ§€ λœ¨μ§€ μ•Šκ²Œ 해라~

ex)

App.vue

<template>
  <div id="app">
    <div id="nav">
      <!-- μ΄λ ‡κ²Œ a tag μ“°λ©΄ page reload 됨 -->
      <a href="/">Home</a> &nbsp;
      <a href="/about">About</a>
      <br>

      <!-- κ·Έλž˜μ„œ router-link λ₯Ό 써야 함! page reload 되면 μš°λ¦¬κ°€ vueλ₯Ό μ“Έ μ΄μœ κ°€ μ—†μŒ! -->
      <router-link to="/">Home</router-link> |
      <router-link to="/about">About</router-link>
    </div>
    <router-view/>
  </div>
</template>

router > index.js

router μ•ˆμ— μžˆλŠ” index.js λŠ” Django μ—μ„œ urls.py 같은 것!

import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import About from '../views/About.vue'

Vue.use(VueRouter)

  // Django μ—μ„œ urls.py 같은 것!
  const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/about',
    name: 'About',
    component: About
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router

views/ directory

  • URL(/) κ³Ό mapping 될 component듀이 λ“€μ–΄κ°€λŠ” directory

  • URL을 ν•˜μ‚¬ 받을 μ• λ“€~!

  • views/ 에 μžˆλŠ” Component듀은, router/index.js 둜 κ°€μ„œ import ν•œλ‹€!

variable routing

<template>
  <div>
      <h1>Hello, {{ name }}</h1>
  </div>
</template>

<script>
export default {
    name: 'HelloName',
    data: function() {
        return {
            name: this.$route.params.name,
        }
    }
}
</script>

Ping - Pong λ§Œλ“€κΈ°

Ping.vue

<template>
  <div>
      <h1>Ping</h1>
      <input type="text" @keyup.enter="sendToPong" v-model="inputText"/>

  </div>
</template>

<script>
export default {
    name: 'Ping',
    data() {
        return {
            inputText: '',
        }
    },
    methods: {
        sendToPong: function() {
            // $router
            // ver1)
            // this.$router.push(`pong?message=${this.inputText}`)
            // ver2)
            this.$router.push({ name: 'Pong', query: {message: this.inputText}})
        }
    }
}
</script>

<style>

</style>

Pong.vue

<template>
  <div>
      <h1>Pong</h1>
      <h3>{{messageFromPing}}</h3>
  </div>
</template>

<script>
export default {
    name: 'Pong',
    data(){
        return {
            messageFromPing: this.$route.query.message
        }
    }
}
</script>

<style>

</style>

Last updated