actions
ํจ์๋ฅผ ํธ์ถํ ๋ ํ๋ผ๋ฏธํฐ๋ฅผ ์ ๋ฌํ๋ ๋ฐฉ๋ฒ์ ์ฌ์ฉํด์ผ ํ๋ค.
actions
๋ฅผ ํธ์ถํ ๋๋ dispatch
๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ค. dispatch
๋ฉ์๋๋ฅผ ์ฌ์ฉํ ๋ ๋๋ฒ์งธ ์ธ์๋ก ์ ๋ฌ ํ ์ ์๋ค.ํํ
this.$store.dispatch("fetchData", parameter)
store.js
actions : {
//์ก์
(actions) ์ ์
fetchData(context, parameter){
console.log(parameter)
}
}
call.js(actions๋ฅผ ํธ์ถํ๋ jsํ์ผ)
<template>
<div>
<button @click="fetchDataWithParameter">์ก์
ํธ์ถ</button>
</div>
</template>
<script>
export default(){
methods : {
fetchDataWithParameter(){
const parameter = "์ ๋ฌํ ํ๋ผ๋ฏธํฐ";
this.$store.dispatch('fetchData',parameter);
//this.$store.dispatch๋ฅผ ์ฌ์ฉํ์ฌ action๋ฅผ ํธ์ถํ๊ณ ํ๋ผ๋ฏธํฐ๋ฅผ ์ ๋ฌํ๋ค.
}
}
}
</script>
์์ ์์ ์์ fetchDataWithParameter
๋ฉ์๋๋ฅผ ํธ์ถํ๋ฉด Vuex ์คํ ์ด์ fetchData
actions๊ฐ ์คํ๋๋ฉฐ, ํ๋ผ๋ฏธํฐ๋ก '์ ๋ฌํ ํ๋ผ๋ฏธํฐ'๋ฅผ ๋ฐ์ ์ ์๋ค. actions ํจ์ ๋ด์์ context
๋งค๊ฐ๋ณ์๋ฅผ ํตํด ์คํ ์ด ์ํ์ ๋ค๋ฅธ ์ก์
๋๋ ๋ฎคํ
์ด์
์ ํธ์ถํ ์ ์๋ค.
context
actions์ ์ถ๊ฐํ ํ๋ผ๋ฏธํฐ๋ ๋์ถฉ
$store
๋ฅผ ๋ปํ๋ค.
mutations์์ setMore์ธ ๋ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ ์ ์๋ค.
state ๋ณ๊ฒฝ์ ๋ฌด์กฐ๊ฑด mutations๊ฐ ํ๋ค.
mutations : { setMore(state, data){ state.more = data } }