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

Was this helpful?