Slim Top-Level API
Slim是Slim库的入口点。 如果从Script标签加载Slim,则Slim全局可以使用这些顶级API。
如果你使用ES6和npm,import Slim from 'slim-store'。
如果你使用ES5和npm,var Slim = require('slim-store')。
Overview
创建Store实例.
- Slim.createStore
- Spec:createStore时的可选参数
注入插件.
事件中心相关.
Store 实例API.
Reference
Slim.createStore
Slim.createStore(Spec) 创建并返回一个Store实例。
const store = Slim.createStore(spec)
Spec
Spec 是Slim.createStore传入的一个配置对象
const store = Slim.createStore(spec)
Slim.use
Slim.use(plugin) 是一种在Store创建前的更灵活的插件注入方式
import Slim from 'slim-store'
let plugin = {
    init(store) {
      console.log('init')
    }
}
Slim.use(plugin)
Store.dispatch
Store.dispatch(actionType, ...args) 触发 Action. 更多关于Action.
import Slim from 'slim-store'
const actions = {
  increment: (context, ...args) => {
    console.log('action increment fire')
  }
}
const store = Slim.createStore({
  actions
})
store.dispatch('increment')
// output: action increment fire
Store.commit
Store.commit(reducerKey, ...args) 触发 Reducer. 更多关于Reducer.
import Slim from 'slim-store'
const reducers = {
  increment: (state, ...args) => {
    console.log('action increment fire')
  }
}
const store = Slim.createStore({
  reducers
})
store.commit('increment')
// output: action increment fire
Store.getGetter
Store.getGetter(getterKey) 是state的组合方法。更多关于getGetter.
import Slim from 'slim-store'
const state = {
  firstName: 'slim',
  lastName: 'store'
}
const getters = {
  fullName: ({firstName, lastName}) => firstName + ' ' + lastName 
}
const store = Slim.createStore({
  state,
  getters
})                  
const username = store.getGetter('username')
console.log(username)   
// output: slim store
Spec.state
Spec.state 是一个单对象数据源
const state = {
  level1: {
    key1OfLevel1: 11
  },
  level2: {
    key1OfLevel2: 12
  }
}
Spec.actions
Spec.actions 是方法的键值对。回调接收多个参数,第一个参数为当前Store上下文
const actions = {
  actionType: (context, ...args) => {
    store.commit('actionType', ...args)
  }
}
Spec.reducers
Spec.reducers 是方法的键值对。回调接收多个参数,第一个参数为当前Store下注册的State
const reducers = {
  reducerKey: (state, arg1, arg2) => {
    state.someKey = arg1 + arg2
  }
}
Spec.getters
Spec.getters 是方法的键值对。回调接收一个参数,为Store下注册的State
const getters = {
  fullName: ({firstName, lastName}) => firstName + ' ' + lastName
}
Spec.mode
Spec.mode 是数据限制模式,可选值:"strict"(默认)和"loose",更多关于Mode
Spec.plugin
Spec.plugin 是一个注入插件集合,类型为数组,更多关于Plugin.