charleyw/wechat-weapp-redux

自定义组件Component()能通过connect绑定数据和动作吗?

Opened this issue · 2 comments

自定义组件Component()能通过connect绑定数据和动作吗?

看了下源码,貌似不能

可以修改的
修改源码 connect.js对Component支持

修改内容

    const {
      onLoad: _onLoad,
      onUnload: _onUnload,
      ready: _ready,
      detached: _detached,
    } = pageConfig

    function onLoad(options) {
      this.store = app.store;
      if (!this.store) {
        warning("Store对象不存在!")
      }
      if(shouldSubscribe){
        this.unsubscribe = this.store.subscribe(handleChange.bind(this, options));
        handleChange.call(this, options)
      }
      if (typeof _onLoad === 'function') {
        _onLoad.call(this, options)
      }
      if (typeof _ready === 'function') {
        _ready.call(this, options)
      }
    }

    function onUnload() {
      if (typeof _onUnload === 'function') {
        _onUnload.call(this)
      }
      if (typeof _detached === 'function') {
        _detached.call(this)
      }
      typeof this.unsubscribe === 'function' && this.unsubscribe()
    }

    /**
     * 兼容Component情况
     */
    const ready = onLoad
    const detached = onUnload

    return assign({}, pageConfig, mapDispatch(app.store.dispatch), {onLoad, onUnload, ready, detached})