๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • What would life be If we had no courage to attemp anything?
๐“๐จ๐๐š๐ฒ ๐ˆ ๐‹๐ž๐š๐ซ๐ง

๐“๐จ๐๐š๐ฒ ๐ˆ ๐‹๐ž๐š๐ซ๐ง 2022.05.10.ํ™”

by DevIseo 2022. 5. 10.

Today I Learn 220510

์–ด์ œ ๋งŒ๋“ค์—ˆ๋˜ ๊ณผ์ œ์—์„œ ์‚ฌ์‹ค /lotto/6์œผ๋กœ ๋„˜์–ด๊ฐ€์ง€ ์•Š๋Š” ์˜ค๋ฅ˜๊ฐ€ ์žˆ์–ด์„œ path๋ฅผ '/lotto/6'์œผ๋กœ ์ง€์ •ํ•ด์„œ ํ•ด๊ฒฐํ–ˆ๋Š”๋ฐ, ์˜ค๋Š˜ ์™œ ๋„˜์–ด๊ฐ€์ง€ ์•Š์•˜๋Š”์ง€ ๋ฐœ๊ฒฌํ•˜์˜€๋‹ค..!

์—ญ์‹œ๋‚˜ ํœด๋จผ์—๋Ÿฌ์˜€๊ณ  params๋ฅผ parmas๋ผ๊ณ  ์ ์–ด์„œ ์ƒ๊ธด ์˜ค๋ฅ˜์˜€๋‹ค....!!

 

๋˜ ๋กœ๋˜ ํŽ˜์ด์ง€์—์„œ ์ˆซ์ž 6๊ฐœ๋ฅผ ๋ฝ‘๋Š” ํ•จ์ˆ˜์—์„œ this.$router.params.lottoNum์ด๋ผ ์ ์–ด์„œ ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ฒผ์—ˆ๋‹ค!!! this.$route.params.lottoNum์œผ๋กœ ํ•ด์•ผ ํ•˜๋Š”๋ฐ,

lunchํŽ˜์ด์ง€์—์„œ lottoํŽ˜์ด์ง€๋กœ ๋„˜์–ด๊ฐˆ ๋•Œ this.$router.push๋ผ๊ณ  ์ ์–ด์„œ route์™€ router๊ฐ€ ํ—ท๊ฐˆ๋ ธ๋‹ค!

 

 

์™œ lunchํŽ˜์ด์ง€์—์„œ๋Š” ๋‹ค๋ฅธ URL๋กœ ์ด๋™ํ•˜๋ ค๋ฉด this.$router.push๋ผ ์“ฐ๋Š” ๊ฑธ๊นŒ?

  • ์ด ๋ฉ”์„œ๋“œ๋Š” ๋ฅผ ํด๋ฆญํ•  ๋•Œ ๋‚ด๋ถ€์ ์œผ๋กœ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ์™€ ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์— this.$router.push()๋กœ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค. --์ „์ฒด 
  • ๋ฐ˜๋ฉด lottoํŽ˜์ด์ง€์—์„œ๋Š” route์ด๊ธฐ ๋•Œ๋ฌธ์— this.$route.params๋กœ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์ด๋‹ค!! -- ํ˜„์žฌ ํŽ˜์ด์ง€์˜ params

๊ทธ๋ž˜์„œ ์–ด์ œ์˜ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€ํ™”๋˜์—ˆ๋‹ค.

/router/index.js

import Vue from 'vue'
import VueRouter from 'vue-router'
import LunchView from '../views/LunchView.vue'
import LottoView from '../views/LottoView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/lunch',
    name: 'lunch',
    component: LunchView
  },
  {
    path: '/lotto/:lottoNum',
    name: 'lotto',
    component:LottoView

  }
]

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

export default router

App.vue

<template>
  <div id="app">
    <nav>
      <router-link :to="{name: 'lunch'}">Lunch</router-link> |
      <router-link :to="{name: 'lotto',params:{ lottoNum:6 } }">Lotto</router-link>
    </nav>
    <router-view/>
  </div>
</template>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

nav {
  padding: 30px;
}

nav a {
  font-weight: bold;
  color: #2c3e50;
}

nav a.router-link-exact-active {
  color: #42b983;
}
</style>

/views/LottoView.vue

<template>
  <div>
    <h1>๋กœ๋˜</h1>
    <button @click="getLuckyNums">Get Lucky Numbers</button>
    <p>{{ selectedLuckyNums }}</p>
  </div>
</template>

<script>
import _ from 'lodash'

export default {
  name:'LottoView',
  data: function() {
    return {
      selectedLuckyNums: [],
    }
  },
  methods: {
    getLuckyNums: function(){
      const numbers = _.range(1,46)
      this.selectedLuckyNums = _.sampleSize(
        numbers,
        this.$route.params.lottoNum,
      )
    }
  }
}
</script>

<style>

</style>

/views/LunchView.vue

<template>
  <div>
    <h1>์ ์‹ฌ๋ฉ”๋‰ด</h1>
    <button @click="pickLunch">Pick Lunch</button>
    <p>{{ selectedLunchMenu }}</p>
    <br>
    <hr>
    <h1>๋กœ๋˜๋ฅผ ๋ฝ‘์•„๋ณด์ž</h1>
    <button @click="moveToLotto">Pick Lotto</button>
  </div>
</template>

<script>
import _ from 'lodash'

export default {
  name: 'LunchView',
  data: function(){
    return { selectedLunchMenu:''} 
  },
  methods: {
    moveToLotto() {
      this.$router.push({ name: 'lotto', params:{lottoNum:6}})
    },
    pickLunch() {
      this.selectedLunchMenu= _.sample(['ํ”ผ์ž','์น˜ํ‚จ','์‚ผ๊ฒน์‚ด','๊ฟ”๋ฐ”๋กœ์šฐ','ํƒ„ํƒ„๋ฉด','๋ผ๋ฉ˜','์—ฐ์–ด์ดˆ๋ฐฅ','๋ง‰๊ตญ์ˆ˜'])
    }
  }
}
</script>

<style>

</style>

๋Œ“๊ธ€