8000 expose replaceState() · vuejs/v2.vuejs.org@49c17e1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 49c17e1

Browse files
committed
expose replaceState()
1 parent f97d106 commit 49c17e1

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

docs/en/api.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ const store = new Vuex.Store({ ...options })
8888
})
8989
```
9090
91+
- **replaceState(state: Object)**
92+
93+
Replace the store's root state. Use this only for state restoration / time-travel purposes.
94+
9195
- **watch(getter: Function, cb: Function, [options: Object])**
9296

9397
Reactively watch a getter function's return value, and call the callback when the value changes. The getter receives the store's state as the only argument. Accepts an optional options object that takes the same options as Vue's `vm.$watch` method.

src/index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,19 @@ class Store {
7575
}
7676

7777
set state (v) {
78-
throw new Error('[vuex] Vuex root state is read only.')
78+
throw new Error('[vuex] Use store.replaceState() to explicit replace store state.')
79+
}
80+
81+
/**
82+
* Replace root state.
83+
*
84+
* @param {Object} state
85+
*/
86+
87+
replaceState (state) {
88+
this._dispatching = true
89+
this._vm.state = state
90+
this._dispatching = false
7991
}
8092

8193
/**

src/plugins/devtool.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ export default function devtoolPlugin (store) {
88
hook.emit('vuex:init', store)
99

1010
hook.on('vuex:travel-to-state', targetState => {
11-
store._dispatching = true
12-
store._vm.state = targetState
13-
store._dispatching = false
11+
store.replaceState(targetState)
1412
})
1513

1614
store.on('mutation', (mutation, state) => {

0 commit comments

Comments
 (0)
0