# vuex-persistedstate
在页面重新加载之间坚持并重新补充您的[Vuex](http://vuex.vuejs.org/)状态。
## 安装
`npm i -S vuex-persistedstate`
## 示例
```
import Vue from 'vue'
import Vuex from 'vuex';
import createPersistedState from 'vuex-persistedstate';
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
count: 0
},
plugins: [createPersistedState()],
mutations: {
increment: state => state.count++,
decrement: state => state.count--
}
});
new Vue({
el: '#app',
computed: {
count() {
return store.state.count;
}
},
methods: {
increment() {
store.commit('increment');
},
decrement() {
store.commit('decrement');
}
}
});
```
## 自定义存储方式
`vuex-persistedstate`默认使用`localStorage`来固化数据,一些特殊情况要如何应对呢?(如:safari的无痕浏览模式)
> 需要使用`sessionStorage`的情况
~~~javascript
plugins: [
persistedState({ storage: window.sessionStorage })
]
~~~
> 使用`cookie`的情况
~~~javascript
import persistedState from 'vuex-persistedstate'
import * as Cookies from 'js-cookie'
export default new Vuex.Store({
// ...
plugins: [
persistedState({
storage: {
getItem: key => Cookies.get(key),
setItem: (key, value) => Cookies.set(key, value, { expires: 7 }),
removeItem: key => Cookies.remove(key)
}
})
]
})
~~~