状态的存储访问统一使用 get()
、set()
、sync()
、call()
方法
安装
npm i vuex-pathify
配置
scheme | path | state | getter | mutation | action | notes |
---|---|---|---|---|---|---|
standard | /foo | foo | foo | SET_FOO | setFoo | 默认 |
simple | /foo | foo | foo | foo | setFoo | 简洁 |
1 | // 默认配置 |
由于ES6导入的工作方式,配置必须保存在独立文件中,并且必须在导入任何存储文件之前导入
- 新建 store/pathify.js
1 | import pathify from 'vuex-pathify' |
- 项目集成
1 | import Vue from 'vue' |
参数说明
deep:
0:禁止访问子属性
1:启用对现有子属性的访问
2:允许创建新的子属性
自定义配置
1 | pathify.options.mapping = function (type, name, formatters) { |
formatters:
camel - camelCase
snake - snake_case
const - CONST_CASE
API
1 | import { make } from 'vuex-pathify' |
const mutations = make.mutations(state)
代码生成如下:
1 | mutations = { |
const actions = make.actions(state)
代码生成如下:
1 | const actions = { |
const getters = make.getters(state)
代码生成如下:
1 | const getters = { |
get 访问
1 | store.get('filters@sort.order') |
set 设置
1 | store.set('items', data).then(console.log) |
sync 设置双向绑定数据
1 | sync('products/status') |
call 创建函数 dispatch actions
1 | methods: { |
装饰器
1 | import { Get, Sync, Call } from 'vuex-pathify' |
等价于
1 | import { get, sync, call } from 'vuex-pathify' |