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>
'๐๐จ๐๐๐ฒ ๐ ๐๐๐๐ซ๐ง' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐๐จ๐๐๐ฒ ๐ ๐๐๐๐ซ๐ง 2022.05.12.๋ชฉ (0) | 2022.05.12 |
---|---|
๐๐จ๐๐๐ฒ ๐ ๐๐๐๐ซ๐ง 2022.05.11.์ (0) | 2022.05.11 |
๐๐จ๐๐๐ฒ ๐ ๐๐๐๐ซ๐ง 2022.05.09.์ (0) | 2022.05.09 |
๐๐จ๐๐๐ฒ ๐ ๐๐๐๐ซ๐ง 2022.05.06.๊ธ (0) | 2022.05.06 |
๐๐จ๐๐๐ฒ ๐ ๐๐๐๐ซ๐ง 2022.05.03.ํ (0) | 2022.05.03 |
๋๊ธ