new Vue({
// state
data () {
return {
count: 0
}
},
// view
template: `
<div>{{ count }}</div>
`,
// actions
methods: {
increment () {
this.count++
}
}
})
npm install vuex --save
├─src
│ ├─components
│ ├─router
│ ├─store (new)
│ │ └─index.js (必備預設檔名)
│ └─main.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const state = {}
const getters = {}
const actions = {}
const mutations = {}
export default new Vuex.Store({
state,
getters,
actions,
mutations
})
...
import store from './store';
new Vue({
el: '#app',
router,
// 加入 store
store,
template: '<App/>',
components: { App }
})
腳色 | 職權 |
---|---|
state | 存放 APP 所有 state |
action | 調用 mutation 函式及處理 ajax (非同步) |
getter | 負責取 state 資料 |
mutation | 負責改變 state 資料 (同步) |
import { mapActions } from 'vuex'
export default {
// ...
methods: {
// 方法一 直接取用
...mapActions([
'increment'
]),
// 方法二 另存別名取用
...mapActions({
add: 'increment' // 使用 add 為元件內別名
}),
// 方法三 直接呼叫使用
actionIncrement(){
this.$store.dispatch('increment')
}
}
}
import { mapGetters } from 'vuex'
export default {
data () {
return {
// 方法三 直接呼叫使用
count: this.$store.state.count
}
},
computed: {
// 方法一 直接取用
...mapGetters([
'getCount'
]),
// 方法二 另存別名取用
...mapGetters({
count: 'getCount' // 使用 add 為元件內別名
})
}
}
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Syncing