๐Ÿค– Vue.js ๋ผ์šฐํŒ…์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž (3)

YOU KNOW I MEANยท2021๋…„ 3์›” 18์ผ
1
post-thumbnail
post-custom-banner

๋ผ์šฐํŠธ์—์„œ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ

๋ผ์šฐํŠธ ์ปดํฌ๋„ŒํŠธ์— ์†์„ฑ ์ „๋‹ฌ

  • ์ปดํฌ๋„ŒํŠธ์—์„œ $route๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ปดํฌ๋„ŒํŠธ๊ฐ€ URL์— ์˜์กด์ ์ด๊ฒŒ ๋˜์–ด ์žฌ์‚ฌ์šฉ์„ฑ์ด ๋–จ์–ด์ง‘๋‹ˆ๋‹ค.

  • ์ด ์˜์กด์„ฑ์„ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด์„œ props ์˜ต์…˜์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

    ์˜์กด์„ฑ ์ถ”๊ฐ€

    const User = {
      template: '<div>User {{ $route.params.id }}</div>'
    }
    const router = new VueRouter({
      routes: [
        { path: '/user/:id', component: User }
      ]
    })

    ์˜์กด์„ฑ ์ œ๊ฑฐ

    const User = {
      props: ['id'],
      template: '<div>User {{ id }}</div>'
    }
    const router = new VueRouter({
      routes: [
        { path: '/user/:id', component: User, props: true },
      ]
    })
  • URL์— ๋Œ€ํ•œ ์˜์กด์„ฑ์ด ์ œ๊ฑฐ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ์ด๋‚˜ ํ…Œ์ŠคํŠธ๊ฐ€ ์‰ฝ์Šต๋‹ˆ๋‹ค.

  • ์ด๋ฆ„์„ ๊ฐ€์ง€๋Š” ๋ทฐ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๊ฐ๊ฐ์˜ ๋ทฐ์— props ์˜ต์…˜์„ ์„ค์ •ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

    const router = new VueRouter({
      routes: [
        {
          path: '/main',
          components: {
            default: Main,
            header: Header,
            footer: Footer
          },
    		props: { default: true, header: false, 					footer: false }
        }
      ]
    })

Boolean mode

  • props์†์„ฑ์— true๋ฅผ ๋„ฃ์œผ๋ฉด route.params๊ฐ€ ์ปดํฌ๋„ŒํŠธย props๋กœ ์„ค์ •๋ฉ๋‹ˆ๋‹ค.

Object mode

  • props๊ฐ€ ๊ฐ์ฒด์ผ ๋•Œ ์ปดํฌ๋„ŒํŠธ props๊ฐ€ ์žˆ๋Š” ๊ทธ๋Œ€๋กœ ์„ค์ •๋ฉ๋‹ˆ๋‹ค.
  • props๊ฐ€ ์ •์ ์ผ ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.
const router = new VueRouter({
  routes: [
    {
      path: '/main',
      components: {
        default: Main,
        header: Header,
        footer: Footer
      },
			props: {
	      footer: { backgroundColor: 'black' }
	    }
    }
  ]
})

function mode

  • props๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์ „๋‹ฌ์ธ์ž๋ฅผ ๋‹ค๋ฅธ ํƒ€์ž…์œผ๋กœ ์บ์ŠคํŒ…ํ•˜๊ณ  ์ •์ ์ธ ๊ฐ’์„ ๋ผ์šฐํŠธ ๊ธฐ๋ฐ˜ ๊ฐ’๊ณผ ๊ฒฐํ•ฉ๋ฉ๋‹ˆ๋‹ค.
const router = new VueRouter({
  routes: [
    { path: '/search', component: SearchUser, props: (route) => ({ user: route.query.id }) }
  
})
  • /search?id=testid ๋ผ๋Š” URL๋กœ ์ ‘์†ํ•˜๊ฒŒ ๋˜๋ฉด SearchUser ์ปดํฌ๋„ŒํŠธ props๋Š” {user:'testid'} ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.
  • props ๋ฅผ ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค ๋•Œ ์œ ์˜ ์‚ฌํ•ญ ์ค‘ ํ•œ๊ฐ€์ง€๋Š” route๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ ํ˜ธ์ถœ ๋˜๋Š” ํ•จ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด ํ•จ์ˆ˜์—์„œ ์ƒํƒœ๋ฅผ ์ €์žฅํ•˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค.
  • wrapper component๋ฅผ ์ด์šฉํ•˜๋ฉด ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ ์‘๋‹ตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ถ”๊ฐ€์ ์œผ๋กœ

history mode - URL ํ•ด์‹œ๊ฐ’ ์ง€์šฐ๊ธฐ

  • ๋ผ์šฐํ„ฐ URL์— ํ•ด์‹œ๊ฐ’(#)์ด ๋ถ™๊ฒŒ๋˜๋Š”๋ฐ ๋ทฐ ๋ผ์šฐํ„ฐ์˜ mode์†์„ฑ์— 'history'๋ฅผ ์ฃผ๋ฉด #์ด ์—†์–ด์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
const router = new VueRouter({
  mode: 'history',
  routes: [...]
})

๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ

  • ๋ณดํ†ต created()์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
  • ๋ผ์šฐํ„ฐ ๋งํฌ๋ฅผ ํ†ตํ•ด ๊ฒฝ๋กœ๊ฐ€ ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒฝ์šฐ /post/:id ๊ฒฝ๋กœ์— ๋งค์นญ๋˜๋Š” ์ปดํฌ๋„ŒํŠธ๋Š” ํ™”๋ฉด์ด ๋ฆฌํ”„๋ž˜์‹œ ๋  ๊ฒฝ์šฐ์—๋งŒ created() ํ›…์ด ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.
  • ๋‹จ์ˆœํžˆ :id ๊ฐ’๋งŒ ๋ณ€๊ฒฝ๋˜๋Š” ๋ผ์šฐํŒ…์ผ ๊ฒฝ์šฐ Post ์ปดํฌ๋„ŒํŠธ๋Š” ๊ต์ฒด๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • this.$route๋ฅผ watch๋ฅผ ํ†ตํ•ด ๊ฐ์‹œํ•˜๋ฉด ๋ผ์šฐํŒ…์ด ๋™์ž‘ํ• ๋•Œ๋งˆ๋‹ค ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ์ˆ˜ ์žˆ๊ณ  ํ™”๋ฉด๋„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    watch: {
      // ๋ผ์šฐํ„ฐ ๊ฐ์ฒด๋ฅผ ๊ฐ์‹œํ•˜๊ณ  ์žˆ๋‹ค๊ฐ€ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.
      '$route': 'function ์ด๋ฆ„'
    },

404 Not Found ๋ผ์šฐํŒ…

{ 
	// will match everything 
	path: '*' 
} 
{ // will match anything starting with `/user-` 
	path: '/user-*' 
}
  • /icelatte๋Š” * ์— ๋งค์นญ๋ฉ๋‹ˆ๋‹ค.
  • /user-admin์€ user-* ์— ๋งค์นญ๋ฉ๋‹ˆ๋‹ค.

Vue router๋ฅผ ์ด์šฉํ•  ๋•Œ ์ƒˆ๋กœ๊ณ ์นจํ•˜๋ฉด ํ™”๋ฉด์ด ์•ˆ๋œจ๋Š” ์ด์œ 

Vue ๋ผ์šฐํ„ฐ์˜ ๋ชจ๋“œ๋ฅผ 'history'๋ชจ๋“œ๋กœ ํ•  ๊ฒฝ์šฐ jsํŒŒ์ผ์— ์ •์˜ํ•œ Vue Routing์˜ path๋กœ ํด๋ผ์ด์–ธํŠธ๋ฅผ ํ†ต์ œํ•ฉ๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ, ์ด๊ฒƒ์€ ์‹ค์ œ๋กœ ํ•˜๋‚˜์˜ ํŽ˜์ด์ง€์ด๊ณ  ๋‚˜๋จธ์ง€๋Š” ๊ฐ€์ƒ์ผ ๋ฟ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ผ์šฐํŒ… ๋œ ์ปดํฌ๋„ŒํŠธ๋“ค์ด ์„œ๋ฒ„์— ์‹ค์ œ๋กœ ๊ทธ ์ฃผ์†Œ์— ์กด์žฌํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ƒˆ๋กœ๊ณ ์นจ์„ ํ•  ๊ฒฝ์šฐ HTTPํ†ต์‹ ์—์„œ ์‘๋‹ต์„ ๋ฐ›์ง€ ๋ชปํ•˜๋ฏ€๋กœ '404 Not Found'๊ฐ€ ๋œจ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

ํ•ด๊ฒฐ๋ฒ•์€?

  1. Hash ๋ผ์šฐํ„ฐ ๋ชจ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
    • ํ•˜์ง€๋งŒ ๊ตฌ๊ธ€์ด ์•„๋‹Œ ๊ฒ€์ƒ‰์—”์ง„์—์„œ๋Š” SEO์— ๋ถˆ๋ฆฌํ•˜์—ฌ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋‹™๋‹ˆ๋‹ค.
  2. ์„œ๋ฒ„์ชฝ์— ์ถ”๊ฐ€ ์„ธํŒ…์„ ํ•ฉ๋‹ˆ๋‹ค.
    • ํ•ด๋‹น ์ฃผ์†Œ๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” html/css/js ๋ฌธ์„œ๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์„ธํŒ…ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
post-custom-banner

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