jiangjiu / blog-md

前端/健身 思考与笔记~

Home Page:https://github.com/jiangjiu/blog-md/issues

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

san-store的connect问题

jiangjiu opened this issue · comments

commented

san-store的connect

引言

san-store中有一个connect对象,上面提供了常用的connect.san这个方法。

然而,链接的是组件自己new的store实例,这意味着我们没办法传入初始值,只能动态的添加actions来触发。

另外,当多个团队分别开发一个系统的不同模块时,有可能只维护自己的store,保持模块的独立性,这就需要使用connect.createConnector来手动操作了。

connect.createConnector

当实际业务中真的需要多个store实例时,可以通过这个函数自行创建方法连接store实例和San组件。步骤如下:

  1. 创建store实例
  2. 通过 connect.createConnector 方法创建一个 connect函数
  3. 调用这个函数对刚刚声明的store实例和组件进行connect
import {Store, connect} from 'san-store';

// 创建模块A中的store实例
const storeA = new Store({
    initData: {
        name:'erik'
    },
    actions:{
        changeUserName() {
            return builder().set('user.name', name);
        }
    }
});
 
// 调用connect.createConnector方法,传入store实例
const connectA = connect.createConnector(storeA);
 
const UserNameEditor = san.defineComponent({...});
 
// 调用手动创建的connectA方法进行storeA和组件连接
connectA(
    {name: 'user.name'},
    {change: 'changeUserName'}
)(UserNameEditor);

connect.createConnector 方法的签名为 {function(Class)}connect.createConnector({Store}store)