workshop - Vue CLI
์ค๋์ Node.js๋ฅผ ์ค์นํ๊ณ Vue CLI๋ฅผ ์ด์ฉํด Vue Router์ฑ์ ์ค์นํ๊ณ ์ด๋ฅผ ํตํด ๋ก๋๋ฒํธ ์ถ์ฒจ๊ธฐ(?)์ ์ ์ฌ๋ฉ๋ด๋ฅผ ๊ณ ๋ฅด๋ ํ์ด์ง๋ฅผ ๊ตฌ์ฑํ๋ ๊ณผ์ ๋ฅผ ํ๋ค. javascript๋ง์ ์ด์ฉํด ์ด ํ์ด์ง๋ฅผ ๊ตฌํํ ๋๋ ์ฌ์ค ์ข ํ๋ค์๋๋ฐ, lodash๋ฅผ importํด์ ์ฐ๋ ์ซ์๋ฅผ ๊ฐ์ ธ์ค๋๊ฒ๋ ํจ์ฌ ์ ๋ฒ๋ณด๋ค ํธํ๋ค!๋ํ ์ ์ฌ๋ฉ๋ด ๊ณ ๋ฅด๋ ๊ฒ๋ lodash๋ฅผ ์ฌ์ฉํ๋ ์ฝ๋๊ฐ ํ๊ฒฐ ๊ฐ๋ตํด์ง๊ฑฐ ๊ฐ๋ค. ๋ํ, Vue CLI๋ฅผ ํตํด ํ๋ฉด์ ๊ตฌํํ๋ ์ ๋ณด๋ค ํจ์ฌ ๊น๋ํ๊ณ ๋น๋๊ธฐํ ์ํค๋ ๊ฒ๋ ์์์ ๋๋๊น ์ข์๊ฑฐ ๊ฐ๋ค. ์ง๋์ฃผ์ ๊ทธ์ ์ฃผ์๋ javascript์ ์ ์ํ๋๋ผ ํ๋ค์์ง๋ง ํ๋ ์์ํฌ๋ฅผ ์ฌ์ฉํด ๊ตฌํํ๋๊ฑด ์์ง ์ด๋ ค์๋ ์ฌ๋ฐ๋๊ฑฐ ๊ฐ๋ค.


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>
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
lodash๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด bash์์ lodash ์ค์นํ๊ธฐ
npm i lodash
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>
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>
'๐๐จ๐๐๐ฒ ๐ ๐๐๐๐ซ๐ง' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐๐จ๐๐๐ฒ ๐ ๐๐๐๐ซ๐ง 2022.05.11.์ (0) | 2022.05.11 |
---|---|
๐๐จ๐๐๐ฒ ๐ ๐๐๐๐ซ๐ง 2022.05.10.ํ (0) | 2022.05.10 |
๐๐จ๐๐๐ฒ ๐ ๐๐๐๐ซ๐ง 2022.05.06.๊ธ (0) | 2022.05.06 |
๐๐จ๐๐๐ฒ ๐ ๐๐๐๐ซ๐ง 2022.05.03.ํ (0) | 2022.05.03 |
๐๐จ๐๐๐ฒ ๐ ๐๐๐๐ซ๐ง 2022.05.02.์ (0) | 2022.05.02 |
๋๊ธ