Vue.JS๋ฅผ ์คํฐ๋ํ๋ ๋ ํ์งํ ๋ฆฌ ์
๋๋ค.
๋จ๋ฐฉํฅ data binding
์๋ฐฉํฅ data binding
๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ DOM์๋ฆฌ๋จผํธ๊ฐ ๋ฌด์ธ๊ฐ๋ฅผ ์ํํ๋๋ก ์ง์ํ๋ ํน์ํ ํ ํฐ
์กฐ๊ฑด์ ๋ฐ๋ผ ์๋ฆฌ๋จผํธ๋ฅผ ํ๋ฉด์ ํ์
v-if์ ํ๊ฐ ๊ฒฐ๊ณผ๊ฐ ๊ฑฐ์ง์ด๋ฉด ์๋ฆฌ๋จผํธ๋ฅผ ํ์
<li v-for =" (value, key, index) in story" class =" list-group-item" >
{{index}} : {{key}} : {{value}}
</li >
(3) v-on (์ด๋ฒคํธ ๋ฆฌ์ค๋๋ฅผ ์๋ฆฌ๋จผํธ์ ๋ถํ)
4๊ฐ์ง ์ด๋ฒคํธ ํ์ ์
.prevent
.stop
.capture
.self
<!-- prevent๋ฅผ ์ด์ฉํ ์ ์ถ๋ฒํผ -->
<button type =" submit" @click =" calculate" >Calculate</button >
<!-- submit ์ด๋ฒคํธ๋ ๋๋ ํ์ด์ง๋ฅผ ์๋ก๊ณ ์นจํ์ง ์์ต๋๋ค. -->
<button type =" submit" @click .prevent =" calculate" >Calculate</button >
:click or @
ํค ํ์ ์ :keyup.enter = :keyup.13
enter, tab, delete, esc, space, up, down, left, right
-------------------
<!-- ์ ์ฒด ๋ฌธ๋ฒ -->
<a v-on :click =" doSomething" > ... </a >
<!-- ์ฝ์ด -->
<a @click =" doSomething" > ... </a >
ํ๋ ์ด์์ ์์ฑ ๋๋ ์ปดํฌ๋ํธ ํ๋กํผํฐ ๋ฅผ ํํ์์ ๋์ ์ผ๋ก ๋ฐ์ธ๋ฉ ํ๋๋ฐ ์ฌ์ฉ(๋์ ์ผ๋ก ๋ฐ๊พธ๊ธฐ)
<!-- ์ ์ฒด ๋ฌธ๋ฒ -->
<a v-bind :href =" url" > ... </a >
<!-- ์ฝ์ด -->
<a :href =" url" > ... </a >
v-model.number (์ฌ์ฉ์๊ฐ ์
๋ ฅํ๋ ๊ฒ์ ์ซ์๋ก ์ ์ฅ)
<body >
<div class =" container" >
a={{ a }}, b={{ b }}
<input v-model .numbr =" a" >
<pre > {{data}} </pre >
</div >
</body >
<script >
export default {
// ๋ฉ์๋ ๊ฐ์ฒด ์๋ ๋ฉ์๋ ์ ์
methods: {
clickEvent : function () {
// ๋ฉ์๋ ๋ด this๋ Vue ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํด
this .data ++ ;
}
}
};
</script >
๊ณ์ฐ๋ ํ๋กํผํฐ
๋ทฐ์ ์ธ๋ผ์ธ ํํ์ ํธํ์ง๋ง ๋ก์ง์ด ๋ณต์กํด์ง๋ฉด computed๋ฅผ ์ฌ์ฉํด์ผํจ
computed properties๋ ๋ค๋ฅธ ์์ธ์ ๋ฐ๋ผ ๊ฐ์ด ๋ฐ๋๋ ๋ณ์. ๊ฐ์ฒด ํ๋กํผํฐ๋ก ์ฌ์ฉํ ์ ์๋ ํจ์์ฒ๋ผ ๋์.
<script >
export default {
computed: {
showList () {
// this๋ Vue์ ์ธ์คํด์ค
return this .models ;
}
}
};
์๋ฅผ ๋ค๋ฉด, ์ฐ์ฐ๋์ด ๋ง๊ฑฐ๋ data๋ฅผ ์ ์งํด์ผํ ๋ ์ฌ์ฉํ๋ฉด ์ข์
๊ณ์ฐ๋ ์บ์ฑ(Computed Caching) vs ๋ฉ์๋(Methods)
๊ณ์ฐ๋ ์์ฑ ๋์ methods๋ก ์ ์ ๊ฐ๋ฅ, ๋๊ฐ์ง์ ์ ๊ทผ ๋ฐฉ์์ ์๋ก ๋์ผํจ
์ด๋ ์ฐจ์ด์ ์ ๊ณ์ฐ๋ ์์ฑ์ ์ข
์์ฑ์ ๋ฐ๋ผ ์บ์๋๋ค
๋ฌด์จ๋ง์ธ์ง ์์๋ณด๋ฉด
๊ณ์ฐ๋ ์์ฑ(computed)์ ์ข
์์ฑ ์ค ์ผ๋ถ๊ฐ ๋ณ๊ฒฝ๋ ๊ฒฝ์ฐ์๋ง ๋ค์ ๊ณ์ฐ
์์ ์์์ message ๊ฐ ๋ณ๊ฒฝ๋์ง ์๋ ํ, reversedMessage์ ๋ํ ๋ค์ค ์ ๊ทผ์
ํจ์๋ฅผ ๋ค์ ์ํํ ํ์ ์์ด ์ด์ ์ ๊ณ์ฐ๋ ๊ฒฐ๊ณผ๋ฅผ ์ฆ์ ๋ฐํํจ
๋ง์ฝ computed ์์ ์ค์๊ฐ์ผ๋ก ๋ ์ง๋ฅผ ๋ํ๋ด๋ ํจ์ Date.now() ๋ฅผ ๋ฃ๋๋ค๋ฉด ์
๋ฐ์ดํธ ๋์ง ์์!
ํ์ง๋ง methods ํธ์ถ์ ๋ ๋๋ง ํ ๋๋ง๋ค ํญ์ ํธ์ถ
์บ์ฑ์ด ํ์ํ ์ด์ ๋?
์บ์ฑํ์ง ์์ผ๋ฉด ์๊ฐ์ด ๋ง์ด ์์๋๋ A ์์ฑ์ด ํ์ํ ๊ฒ๋ณด๋ค getter๋ฅผ ๋ ์คํํ๊ฒ ๋์ด ๋ถํ์ํ ์์
์คํ
๋ฐ๋ผ์ ์บ์ฑ์ด ํ์ํ์ง ์์๋๋ methods๋ฅผ ์ฌ์ฉ
์ถ์ฒ: https://takeuu.tistory.com/25 [์๋๋น์คํ์
]
๋ฐฐ์ด์ ํญ๋ชฉ ํํฐ๋ง
<script >
export default {
methods: {
// ์๊ฐ์ ๋ฐ๋ผ ์ด์ผ๊ธฐ๋ฅผ ํํฐ๋งํ๋ ๋ฉ์๋
storiesBy : function (writer ) {
return this .stories .filter (function (story ) {
return story .writer === writer
}
}
};
< / script>
<script >
export default {
computed: {
orderedStories : function () {
// JS ๋ด์ฅํจ์ sort์ฌ์ฉ
// ๋ฐฐ์ด์ ์์๋ฅผ ์ ๋ ฌ
return this .stories .sort (function (a , b ){
// ๋ฐฐ์ด์ ๋ฐํ
return a .upvotes - b .upvotes ;
})
}
}
};
</script >
compareFunction(a, b) = 0๋ณด๋ค ์์ผ๋ฉด, a๋ฅผ b๋ณด๋ค ๋ฎ์ ์ธ๋ฑ์ค๋ก ์ ๋ ฌ
compareFunction(a, b) = 0์ด๋ฉด, a์ b์ ์์น๋ฅผ ๋ณ๊ฒฝํ์ง ์์ต๋๋ค.
compareFunction(a, b) = 0๋ณด๋ค ํฌ๋ฉด, b๋ฅผ a๋ณด๋ค ๋ฎ์ ์ธ๋ฑ์ค๋ก ์ ๋ ฌ
์ฌ์ฉ์ ์ ์ ํํฐ ์ ์ฉ
์ ํธ๋ฆฌํฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํ์ฉ : CDN์ ํตํด html ํ์ผ์ ์ถ๊ฐ
Lodash
Underscore
sugar
// ๋ฌธ๋ฒ
// ์ฒซ๋ฒ์งธprop : ์ ๋ ฌํ ๋ฐฐ์ด
// ๋๋ฒ์งธprop : ์ ๋ ฌ ๊ธฐ์ค์ด ๋ ํค์ ๋ฐฐ์ด
// ์ธ๋ฒ์งธprop : ๊ฐ ํค์ ์์๋ก ๊ตฌ์ฑ๋ ๋ฐฐ์ด
_ . orderBy ( collection , [ iteratees = [ _ . identity ] ] , [ orders ] )
(1) Creation : ์ปดํฌ๋ํธ ์ด๊ธฐํ (์ด๋ฒคํธ, ์๋ช
์ฃผ๊ธฐ ๋ฉ์๋ ์ด๊ธฐํ, ๋ฉ์๋ ๋ฐ ๋ฐ์ดํฐ ์ต์
์ ํ๊ณ ๋ฐ์ํ์ผ๋ก ์ค์ )
before create : data, events ์ธํ
์
created : data, events ํ์ฑํ( template, ๊ฐ์๋ -> mount, rendering X )
(2) Mounting : ๋(DOM) ์ฝ์
๋จ๊ณ (ํ
ํ๋ฆฟ์ ์ฝ์ด์ ๋ฌธ๋ฒ ์ ์ฉ, ํ๋ฉด ๋ ๋๋ง)
before Mount : template, ๋ ๋ํจ์๊ฐ ๋ ๋๋ง ๋ ํ ์ฒซ ๋ ๋๋ง ์ง์ ์ ์คํ ( ๋น๊ถ์ฅ, server-side-rendering์ ํธ์ถ X )
Mounted : component, template, ๋ ๋๋ง๋ DOM ์ ๊ทผ ๊ฐ๋ฅ (๋ชจ๋ ํ์ component๊ฐ ๋ง์ดํ
์ํ ๋ณด์ฅ์ X, s-s-r ํธ์ถ X )
(3) Updating : Diff. ์ฌ๋ ๋๋ง ๋จ๊ณ (๋ฐ์ดํฐ ์์ฑ์ ๋ณํ๋ฅผ ๊ฐ์งํ๋ฉด ํ๋ฉด์ ๋ค์ ๋ ๋๋ง)
before Update : ๋ ์ฌ๋ ๋๋ง -> Before0Update -> ํจ์น ( ์ฌ๋ ๋๋ง ์ ์ ์ ์ํ์ data ์ป์ ์ O )
updated : ๋์ด ์
๋ฐ์ดํธ๋ ์ํ, ๋ ์ข
์ ์ฐ์ฐ ๊ฐ๋ฅ
The lifecycle hooks around update respond to changes in the DOM . Watchers respond to changes in the data . DOM changes are generally in response to data changes, but they might not be data owned by the component in question. ex
(4) Destruction : ํด์ ๋จ๊ณ (๋ฐ์ดํฐ์ ๋ฐ์ ํด์ ๋ฐ ์ด๋ฒคํธ๋ฅผ ์ญ์ )
before Destroy : ๋ทฐ์ธ์คํด์ค ์ ๊ฑฐ ์ง์ ์ ํธ์ถ๋จ
destroyed : ๋ทฐ์ธ์คํด์ค ์ ๊ฑฐ ํ ํธ์ถ ๋จ
life-cycle Hooks Tutorial
7. ์ฌ์ฉ์ ์ ์ ์ด๋ฒคํธ
1) $on(event)๋ฅผ ์ด์ฉํด ์ด๋ฒคํธ ์ฒญ์ทจ
2) $emit(event)๋ฅผ ์ด์ฉํด ์ด๋ฒคํธ ๋ฐ์
์์ > ๋ถ๋ชจ ๋ณ๊ฒฝ์ฌํญ ์ ๋ฌ : emit
<script >
Vue .component (' chariot' , {
template: ' #chariot-template' ,
// ๋ถ๋ชจ > ์์ ์ปดํฌ๋ํธ ํต์
props: [' chariot' ],
methods: {
rideChariot : function (chariot ) {
// ์ด๋ฒคํธ ์ด๋ฆ ์ธ์์ ํจ๊ป ๋ฆฌ์ค๋ ์ฝ๋ฐฑํจ์์ ์ถ๊ฐ๋ก ์ ๋ฌํฉ๋๋ค(arrowํจ์)
this .$emit (' select' , chariot)
}
}
:
$emit์์ ์ค์ ํ 'select' ๊ฐ ์๋์ ๋ถ๋ชจ์์ ํ๊ทธ ์์ @select ๋ก ๋ค์ด๊ฐ. ๊ทธ ๋, @select="๋ด๋ถํจ์"์คํ ์ํด
<body >
<div class =" container" >
<h1 >Chariot shopping</h1 >
<ul class =" list-group" >
<chariot v-for =" (chariot, index) in chariots" :key =" index" :chariot =" chariot" @select =" updateChariot" ></chariot >
</ul >
<pre >{{ $data }}</pre >
</div >
3) $once(event)๋ฅผ ์ด์ฉํด ์ด๋ฒคํธ๋ฅผ ํ ๋ฒ๋ง ์ฒญ์ทจ
4) $off()๋ฅผ ์ด์ฉํด ์ด๋ฒคํธ ๋ฆฌ์ค๋๋ฅผ ์ ๊ฑฐ
์ฌ๋ฌ ์ปดํฌ๋ํธ ๊ฐ ํต์ ๊ฐ์๋ ์ค์๊ด๋ฆฌ์ฒด๊ณ ๊ฐ ํ์ํจ
props, emit, eventbus ๋์ฒด
vuex๋ ? https://vuex.vuejs.org/kr/
vuex์ ํจ์, ์ํ ์ ์ฅ -> ๋ค๋ฅธ ์ปดํฌ๋ํธ์์ ์์ ๋กญ๊ฒ ์ ๊ทผ ๊ฐ๋ฅ
vuex์ ์ปดํฌ๋ํธ ๋น๊ต
state : Vue ์ธ์คํด์ค์ data๋ผ๊ณ ์๊ฐํ๋ฉด ๋จ
getters : computed
mutations : ๋ณ์ด (state๊ฐ์ ๋ณํ์ํด) (๋ชจ๋ ๊ธฐ๋ฅ์ด ๋๊ธฐ ๋ก ๋์ํจ)
actions : ๋น๋๊ธฐ ๋ก์ง ์ฒ๋ฆฌ(mutation์ ๋ณํ์ํค๋ business ๋ก์ง)
import Vue from 'vue'
import Vuex from 'vuex'
Vue . use ( Vuex )
export default new Vuex . Store ( {
state : { // data
allUsers :[
{ userId :'123' , password : '1234' , name : 'tonz' , address : 'Seoul' }
:
]
} ,
mutations : {
} ,
actions : {
}
} )
์ค์ํต์ ๊ด๋ฆฌ์ ๊ฒฉ์ธ main.js ํ์ผ์ Vuex์ค์ (๊ฐ ์ปดํฌ๋ํธ์์ import X)
:
import store from './store'
:
new Vue ( {
router,
store,
render : h => h ( App )
} ) . $mount ( '#app' )
AllUsers.vue ์ ๊ฐ์ ์ปดํฌ๋ํธ์์์ ์ ๊ทผ : $store.state.allUsers
<template >
<div >
<h1 >All Users ({{ $store .state .allUsers .length }})</h1 >
:
<v-list-tile
v-for =" (user, index) in $store .state .allUsers "
:key =" index"
avatar >
:
</template >
<script >
export default {
data () {
return {
}
},
mounted () {
EventBus .$on (' SignUp' . users => {
this .$store .state .allUsers .push (users)
})
}
}
</script >
๋ณต์กํ ๊ณ์ฐ์์ด ๋ ๋ ์ฉ์ดํจ
state ์์ฒด๋ฅผ ๊ฐ์ ธ์ค๊ธฐ ์ํ getter
store.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue . use ( Vuex )
export default new Vuex . Store ( {
state : { // data
allUsers :[
{ userId :'123' , password : '1234' , name : 'tonz' , address : 'Seoul' }
:
]
} ,
getters : { // computed
allUsersCount : function ( state ) { // state๋ก ๋ฐ๋์ ๋ค์ด๊ฐ์ผํจ
return state . allUsers . length
} ,
countOfSeoul : state => {
let count = 0
state . allUsers . forEach ( user => {
if ( user . address === 'Seoul' ) count ++
} )
return count
} ,
percentOfSeoul : ( state , getters ) => { // state๋ก ๋ฐ๋์ ๋ฐ์์ผ ๋๋ฒ์งธ ์ธ์ ์ธ์
return Math . round ( getters . countOfSeoul / getters . allUsersCount * 100 )
}
} ,
mutations : {
} ,
actions : {
}
} )
getters๋ก AllUsers.vue ์ ๊ฐ์ ์ปดํฌ๋ํธ์์์ ์ ๊ทผ : $store.getters.allUsersCount
<template >
<div >
<h1 >Seoul Users : ({{ $store .getters .countOfSeoul }}) ({{ $store .getters .percentOfSeoul }}%)</h1 >
:
<v-list-tile
v-for =" (user, index) in $store .getters .allUsersCount "
:key =" index"
avatar >
:
</template >
<script >
export default {
data () {
return {
}
},
mounted () {
EventBus .$on (' SignUp' . users => {
this .$store .state .allUsers .push (users)
})
}
}
</script >
getters์์ ์ฐ๊ณ ์๋ ๊ฐ๋ค์ ์ฌ์ฉํ๊ณ ์ํ๋ ์ปดํฌ๋ํธ์ ๊ฐ๋จํ๊ฒ ๋ถ๋ฌ๋ด๋ ๊ฒ
map getters๋ก AllUsers.vue ์ ๊ฐ์ ์ปดํฌ๋ํธ์์์ ์ ๊ทผ : ๋ฐฐ์ด ๋ก ๋ฐ๊ธฐ
<template >
<div >
<h1 >Seoul Users : ({{ countOfSeoul }}) ({{ percentOfSeoul }}%)</h1 >
:
</template >
<script >
import { mapGetters } from ' vuex' // vuex์์ ๋ฏธ๋ฆฌ ์ค์ ๋์ด ์์ผ๋ฏ๋ก vuex์์ ์ํฌํธ
export default {
data () {
return {
}
},
computed: {
// ๋ฐฐ์ด๋ก ๋ฐ์
... mapGetters ([' allUsersCount' , ' countOfSeoul' , ' percentOfSeoul' ]) // mapGetters์์ ๋ถ๋ฌ์์ผ ๊ฐ๋จํ ์ฌ์ฉ๊ฐ๋ฅ
},
mounted () {
EventBus .$on (' SignUp' . users => {
this .$store .state .allUsers .push (users)
})
}
}
</script >
AllUsers.vue ๋ค๋ฅธ ๊ฐ์ผ๋ก ๋ฐ์์ ์ธ ๋ mapGetters๋ฅผ ๊ฐ์ฒด ๋ก ๋ฐ๊ธฐ
mapState
<template >
<div >
<h1 >All Users : ({{ count }})</h1 >
<h1 >Seoul Users : ({{ seouls }}) ({{ percent }}%)</h1 >
:
<v-list-tile
v-for =" (user, index) in allUsers" // mapState ์์
:key =" index"
avatar >
:
</template >
<script >
import { mapState , mapGetters } from ' vuex'
export default {
computed: {
... mapGetters ({ // ๊ฐ์ฒด๋ก ๋ฐ์
count: ' allUsersCount' ,
seouls: ' countOfSeoul' ,
percent: ' percentOfSeoul'
}),
... mapState ([' allUsers' ])
},
:
< / script>
store.commit() : mutation ํธ์ถ
WHY ? ๊ฐ ์ปดํฌ๋ํธ์์ ๋์ผํ๊ฒ ์ฐ์ด๋ฉด ์ฝ๋์ค๋ณต -> state๋ฅผ ๋ณํ์ํค๋ mutation์ ๊ฐ ์ปดํฌ๋ํธ์์ ํ์ฉ!(commit )
state์ setter ํน์ updater์ ์ญํ
store.js
:
export default new Vuex . Store ( {
state : { // data
allUsers :[
{ userId :'123' , password : '1234' , name : 'tonz' , address : 'Seoul' }
:
]
} ,
mutations : {
addUsers : ( state , payload ) => {
state . allUsers . push ( payload )
}
} ,
actions : {
}
} )
SignUp.vue : mapMutations
<template >
:
</template >
<script >
import { mapMutations } from ' vuex'
export default {
:
computed: {
... mapGetters ({ // ๊ฐ์ฒด๋ก ๋ฐ์
count: ' allUsersCount' ,
seouls: ' countOfSeoul' ,
percent: ' percentOfSeoul'
}),
... mapState ([' allUsers' ])
},
methods: {
... mapMutations ([' addUsers' ]),
signUp () {
let userObj = {
userId : this .userId ,
:
}
this .addUsers (userObj) // userObj๋ store.js์ payload๋ก ๋ ์๊ฐ
// EventBus.$emit('signUp', userObj)
:
}
}
:
< / script>
SignUp.vue : mapMutation์ด ์๋ commit('mutation์ด๋ฆ', ๋๊ธธ payload)์ผ๋ก ๋ถ๋ฌ์ค๊ธฐ
<template >
:
</template >
<script >
// import { mapMutations } from 'vuex'
export default {
:
computed: {
// ...mapGetters({ // ๊ฐ์ฒด๋ก ๋ฐ์
count: ' allUsersCount' ,
seouls: ' countOfSeoul' ,
percent: ' percentOfSeoul'
}),
... mapState ([' allUsers' ])
},
methods: {
... mapMutations ([' addUsers' ]),
signUp () {
let userObj = {
userId : this .userId ,
:
}
this .$store .commit (' addUsers' , userObj)
// this.addUsers(userObj) // userObj๋ store.js์ payload๋ก ๋ ์๊ฐ
// EventBus.$emit('signUp', userObj)
:
}
}
:
</script >
store.dispatch() : action ํธ์ถ
๊ฐ ์ปดํฌ๋ํธ์์ dispatch๋ฐ์ ํ mutations์ผ๋ก commit๋ ๋ฆผ
state๋ฅผ ๋ณํ์ํค๊ธฐ์ํ business ๋ก์ง ํฌํจ(ex. ๋น๋๊ธฐ ๋ฐฉ์์ผ๋ก ์๋ฒ์ ํต์ )
๋น๋๊ธฐ ์์
ํ mutation ์คํ
store.js
:
export default new Vuex . Store ( {
state : { // data
allUsers :[
{ userId :'123' , password : '1234' , name : 'tonz' , address : 'Seoul' }
:
]
} ,
mutations : {
addUsers : ( state , payload ) => {
state . allUsers . push ( payload )
}
} ,
actions : { // ์ด๋ฆ ๊ฒน์ณ๋ ๋จ
addUsers : context => {
// mutation์ ๋ถ๋ฌ์ค๋ ๋ฐฉ๋ฒ
context . commit ( 'addUsers' )
}
}
} )
commit์ด ๋ง์์ง๋ฉด ์์ ๋ฐฉ๋ฒ์ ๋ถํธ
:
export default new Vuex . Store ( {
state : { // data
allUsers :[
{ userId :'123' , password : '1234' , name : 'tonz' , address : 'Seoul' }
:
]
} ,
mutations : {
addUsers : ( state , payload ) => {
state . allUsers . push ( payload )
}
} ,
actions : { // ์ด๋ฆ ๊ฒน์ณ๋ ๋จ
addUsers : ( { commit } , payload ) => { // function({commit})๊ณผ ๊ฐ์ ์๋ฏธ
// context, payload
// { commit }, payload
// ์ด๊ณณ์ ์๋ฒ์ ํต์ ํ๋ business๋ก์ง ๋์ด
:
:
commit ( 'addUsers' , payload ) // state๋ฅผ ๋ณํ์ํค๋ ํ ์ค!
}
}
} )
SignUp.vue : actions๋ฅผ ๋ถ๋ฌ์ค๊ธฐ dispatch
<template >
:
</template >
<script >
import { mapMutations } from ' vuex'
export default {
:
methods: {
... mapMutations ([' addUsers' ]),
signUp () {
let userObj = {
userId : this .userId ,
:
}
// this.addUsers(usrObj)
this .$store .dispatch (' addUsers' , userObj)
:
}
}
:
< / script>
SignUp.vue : mapActions ํ์ฉ
<template >
:
</template >
<script >
import { mapActions } from ' vuex'
export default {
:
methods: {
// ...mapMutations(['addUsers']),
... mapActions ([' addUsers' ]),
signUp () {
let userObj = {
userId : this .userId ,
:
}
// this.addUsers(usrObj)
// this.$store.dispatch('addUsers', userObj)
this .addUsers (userObj)
:
}
}
:
< / script>
1) Vue.js์์ nextTick ์ฌ์ฉํ๊ธฐ
nextTick ์ผ๋ก ๊ฐ์ผ๋ค callback์ ํตํด DOM์ ์กฐ์ํ๊ฒ ๋๋ฉด Vue.js์์ ๋ฐ์ดํฐ๊ฐฑ์ ํ UI๊น์ง ์๋ฃํ ๋ค์ nextTick์ ์๋ ํจ์๋ฅผ ์ต์ข
์ ์ผ๋ก ์ํํ๊ฒ ๋ฉ๋๋ค.
created: function() {
// ...
this.$nextTick(function() {
var dom = document.getElementById('item-0');
dom.style.backgroundColor = 'red';
});
}
2) Vue.js์์ nextTick ํ์ฉํ๊ธฐ
์ฝ๋์์ผ๋ก ์๋๋ select box ์์ pig ๋ฅผ ์ ํํ๋ ๊ฒฝ์ฐ
ํ๋ฉด์ pig ๋ animal ์ด๋ผ๊ณ ํํํ๋ ค๊ณ ํ๋ค. ํน์ apple ๋ฅผ ์ ํํ๋ฉด fruit ๋ก ํ๊ธฐํ๋ค.
ํ์ง๋ง ๋๋๊ฒ๋(!) ์ฝ๋๋ ์๋์ ๋ฐ๋๋ก ๋์ํ๋ค.
click ์ด๋ฒคํธ๋ v-model ๊ฐ์ด ๋ณ๊ฒฝ๋๊ธฐ ์ ์ ํธ์ถ๋๊ณ
ํธ์ถ์ด ๋๋๊ณ ๋์์ผ v-model ๊ฐ์ด ๋ณ๊ฒฝ๋๊ธฐ ๋๋ฌธ์ด๋ค.
๋ค์๋งํด select box ์ pig ๊ฐ ์ ํ๋์ด ์๋ ์ํ์์ apple ๋ก ๋ณ๊ฒฝํ๋ ค๊ณ ํ ๋
์์ง pig ์ธ ์ํ์์ click ์ด๋ฒคํธ๊ฐ ๋ค ๋๋๊ฒ ๋๊ณ
๊ทธ ํ์ v-model ์ apple ๋ก ๋ณ๊ฒฝ๋๋ค.
click ์ด๋ฒคํธ ํธ์ถ์์, ๋๋จ (pig์ธ ์ํ์์ ํด๋ฆญ์ด๋ฒคํธ ์ข
๋ฃ)
v-model ๊ฐ ๋ณ๊ฒฝ (์ด๋ฒคํธ ํธ์ถ ๋๋ ์ดํ apple๋ก ๊ฐ ๋ณ๊ฒฝ)
< b-form-select v-model = "selected" :options = "options" @change = "Changed" / >
{ { type } }
Changed() {
if ( this.selected == "pig" )
this.type = "animal";
if ( this.selectd == "apple" )
this.type = "fruit";
}
Changed() {
Vue.nextTick(() => {
if ( this.selected == "pig" )
this.type = "animal";
if ( this.selectd == "apple" )
this.type = "fruit";
});
}