[Vue] Vue Router

๊น€์ •๋ฏผยท2022๋…„ 6์›” 18์ผ
1
post-thumbnail
post-custom-banner

Vue Router

  • Vue์—์„œ SPA(Single Page Application) ์›น ํŽ˜์ด์ง€ ๋ณ€๊ฒฝ/์ „ํ™˜์„ ์ง€์›ํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
  • ํŽ˜์ด์ง€ ๋ณ€๊ฒฝ์„ ์œ„ํ•ด์„œ DOM์„ ์ƒˆ๋กœ ๊ฐฑ์‹ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ๋ณ€๊ฒฝ๋œ ์š”์†Œ์˜์—ญ(์ปดํฌ๋„ŒํŠธ)๋งŒ ๊ฐฑ์‹ 

Vue Router ํƒœ๊ทธ

<router-link to="URL ๊ฐ’">

  • ํŽ˜์ด์ง€ ์ด๋™ ํƒœ๊ทธ, HTML <a> ํƒœ๊ทธ๋กœ ํ‘œ์‹œ๋˜์–ด ํด๋ฆญ ์‹œ URL ๊ฐ’์œผ๋กœ ์ด๋™
<div>
    <router-link to="/login">Login</router-link>
</div>

<router-view>

  • ๋ณ€๊ฒฝ๋˜๋Š” URL์— ๋”ฐ๋ผ ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ‘œ์‹œํ•ด์ฃผ๋Š” ์˜์—ญ
<div>
  <router-view></router-view>
</div>

Vue Router ์ธ์Šคํ„ด์Šค

  • Vue Router ๊ธฐ๋Šฅ์€ Router์ธ์Šคํ„ด์Šค๋ฅผ ํ†ตํ•ด์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
// Router ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
var router = new VueRouter({
  mode: "history", // ๊ธฐ๋ณธ ๊ฐ’์€ Hash๋ชจ๋“œ(history ๋ชจ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ธŒ๋ผ์šฐ์ € ํžˆ์Šคํ† ๋ฆฌ ์Šคํƒ์— ๊ธฐ๋ก)
  routes: [    // ํŽ˜์ด์ง€์˜ ๋ผ์šฐํŒ… ์ •๋ณด
  {
    path: "/login", // ํŽ˜์ด์ง€ URL
    component: LoginComponent, // ํ‘œ์‹œ๋  ์ปดํฌ๋„ŒํŠธ
    components: {},  // ์ปดํฌ๋„ŒํŠธ ์—ฌ๋Ÿฌ๊ฐœ(Named View) ์ผ ๊ฒฝ์šฐ
    children: [ ... ],   // ํ•˜์œ„ ๋ผ์šฐํŒ… ์ •๋ณด
    redirect: ..., // redirect ์ฒ˜๋ฆฌ(url๊ณผ ํ™”๋ฉด ๋ชจ๋‘ redirect ์„ค์ •์œผ๋กœ ํ‘œ์‹œ)
    alias: ...    // url์€ alias๋กœ ํ‘œ์‹œ๋˜์ง€๋งŒ ํ™”๋ฉด์€ component์˜ ํ™”๋ฉด ํ‘œ์‹œ
  }
  ...
  ]
});
  • Vue Router 4๋ฒ„์ „๋ถ€ํ„ฐ๋Š” ์ธ์Šคํ„ด์Šค๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š๊ณ , createRouter()๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋ผ์šฐํ„ฐ๋ฅผ ์ƒ์„ฑ
const router = createRouter({
  history : createWebHistory(),
  routes : [ ... ]
});

Vue Router ์ƒ˜ํ”Œ(.html)

<body>
    <div id="app">
      <div>
        <!-- Router ๋งํฌ -->
        <router-link to="/login">Login</router-link>
        <router-link to="/main">Main</router-link>
      </div>
      <!-- URL์— ๋”ฐ๋ฅธ ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํ‘œ์‹œ๋˜๋Š” ์˜์—ญ-->
      <router-view></router-view>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router@3.5.3/dist/vue-router.js"></script>
    <script>
      var LoginComponent = {
        template: "<div>login</div>",
      };
      var MainComponent = {
        template: "<div>main</div>",
      };

      // Router ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
      var router = new VueRouter({
        routes: [
          {
            path: "/login", // ํŽ˜์ด์ง€ URL
            component: LoginComponent, // ํ‘œ์‹œ๋  ์ปดํฌ๋„ŒํŠธ
          },
          {
            path: "/main", // ํŽ˜์ด์ง€ URL
            component: MainComponent, // ํ‘œ์‹œ๋  ์ปดํฌ๋„ŒํŠธ
          },
        ],
      });
      new Vue({
        el: "#app",
        router: router, // ๋ผ์šฐํ„ฐ ๋“ฑ๋ก
      });
    </script>
  </body>

Login, Main ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ƒ์„ฑ ํ›„ ๊ฐ ๋งํฌ๋ฅผ ํด๋ฆญํ•  ๋•Œ ๋งˆ๋‹ค ํ•˜๋‹จ์— ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ ๋‚ด์šฉ ํ™”๋ฉด ํ‘œ์‹œ

Vue Router ๊ตฌ์„ฑ

์ผ๋ฐ˜์ ์œผ๋กœ vue router ๊ตฌ์„ฑ ์‹œ npm์„ ํ†ตํ•ด ์„ค์น˜ํ•˜๊ณ , SFC(์‹ฑ๊ธ€ํŒŒ์ผ์ปดํฌ๋„ŒํŠธ, .vueํŒŒ์ผ)์—์„œ ์‚ฌ์šฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค
  • vue router npm ์„ค์น˜

npm install vue-router --save

  • ํ”„๋กœ์ ํŠธ ๋ฃจํŠธ์— router/index.js ํŒŒ์ผ ์ƒ์„ฑ ํ›„ ๋ผ์šฐํ„ฐ ์„ค์ • ์ถ”๊ฐ€
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  routes: [
      {
        path: "/login", // ํŽ˜์ด์ง€ URL
        component: LoginComponent, // ํ‘œ์‹œ๋  ์ปดํฌ๋„ŒํŠธ
      },
      {
        path: "/main", // ํŽ˜์ด์ง€ URL
        component: MainComponent, // ํ‘œ์‹œ๋  ์ปดํฌ๋„ŒํŠธ
      },
  ],
})
export default router

Vue Router ๊ตฌ์„ฑ(Vue Router Plugin ์‚ฌ์šฉ)

Vue์—์„œ router ๊ตฌ์„ฑ ์ž‘์—…๋“ค์˜ ํŽธ์˜๋ฅผ ์œ„ํ•ด VueRouter Plugin(cli-plugin-router)์„ ์ œ๊ณตํ•œ๋‹ค
vue add router

์œ„ ๋ช…๋ น์–ด ํ•˜๋‚˜๋ฉด ์ž…๋ ฅํ•˜๋ฉด vue router ์„ค์น˜ ๋ฐ ๊ธฐ๋ณธ ์„ค์ •์„ ํ•œ๋ฒˆ์— ์ง„ํ–‰ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ง„ํ–‰๋˜๋Š” ์ž‘์—…๋“ค์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค

  1. Vue router ์„ค์น˜
  2. Vue Router ์„ค์ • ํŒŒ์ผ ์ƒ์„ฑ (router/index.js ํŒŒ์ผ)
  3. Vue Router ์ƒ˜ํ”Œ ์ปดํฌ๋„ŒํŠธ ์ƒ์„ฑ(AboutView.vue, HomeView.vue)
  4. App.vue์— Router ์ƒ˜ํ”Œ ์ปดํฌ๋„ŒํŠธ ์‹คํ–‰์„ ์œ„ํ•œ router-link, router-view ์ƒ์„ฑ
  5. main.js์— Vue Router ์ ์šฉ: .use(router)

์„ค์น˜ ํ›„ package.jsonํŒŒ์ผ์„ ๋ณด๋ฉด dependencies์— vue-router, devDependencies์— @vue/cli-plugin-router๊ฐ€ ์ถ”๊ฐ€๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

ํ”„๋กœ์ ํŠธ ๋ฃจํŠธ์— router/index.js ํŒŒ์ผ์—๋Š” ๊ธฐ๋ณธ router ์„ค์ •์ด ๋˜์–ด์žˆ๊ณ , ์ƒ˜ํ”Œ์„ ์œ„ํ•œ views/AboutView.vue, HomeView.vue ํŒŒ์ผ์ด ์ƒ์„ฑ๋˜์–ด ์žˆ๋‹ค.

App.vueํŒŒ์ผ์˜ template์— router-link, router-view ํƒœ๊ทธ๊ฐ€ ์ ์šฉ๋˜์–ด ์žˆ๋‹ค.

main.jsํŒŒ์ผ์—์„œ๋„ router์„ค์ •์„ importํ•˜์—ฌ ์ ์šฉ๋˜์–ด ์žˆ๋‹ค.

์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ npm run serve๋กœ ์‹คํ–‰ํ•ด์„œ ์ ‘์†ํ•ด๋ณด๋ฉด router ์ ์šฉ ๋™์ž‘์„ ํ™•์ผ ํ•  ์ˆ˜ ์žˆ๋‹ค.

Named View

ํŽ˜์ด์ง€์—์„œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋™์‹œ์— ํ‘œ์‹œํ•˜๋Š” ๋ผ์šฐํŒ… ๋ฐฉ์‹

<div>
  <router-view name="header"></router-view>
  <router-view></router-view>  <!-- name์ด ์—†๋Š” ๊ฒฝ์šฐ ๋””ํดํŠธ -->
  <router-view name="footer"></router-view>
</div>

Named View ์ƒ˜ํ”Œ

<div id="app">
  <!-- URL์— ๋”ฐ๋ฅธ ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํ‘œ์‹œ๋˜๋Š” ์˜์—ญ-->
  <router-view name="header"></router-view>
  <router-view></router-view>
  <router-view name="footer"></router-view>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router@3.5.3/dist/vue-router.js"></script>
<script>
  var BodyComponent = {
    template: "<div>Body</div>",
  };
  var HeaderComponent = {
    template: "<div>Header</div>",
  };
  var FooterComponent = {
    template: "<div>Footer</div>",
  };

  // Router ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
  var router = new VueRouter({
    routes: [
      {
        path: "/", // ํŽ˜์ด์ง€ URL
        components: {
          default: BodyComponent,
          header: HeaderComponent,
          footer: FooterComponent,
        },
      },
    ],
  });
  new Vue({
    router,
  }).$mount("#app"); // $mount()๋กœ 'el'์†์„ฑ๊ณผ ๋™์ผํ•œ ๊ธฐ๋Šฅ
</script>

<router-view name="...">์— name ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹นํ•˜๋Š” Router์ธ์Šคํ„ด์Šค์˜ 'components' ์†์„ฑ์˜ key์— ํ•ด๋‹นํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํ™”๋ฉด์— ํ‘œ์‹œ๋œ๋‹ค

์ถœ์ฒ˜ : https://happy-jjang-a.tistory.com/119 - jjang-a ๋ธ”๋กœ๊ทธ

post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€