Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

实现了 componentWillPreload 的页面 $componentType 的值错误 #4123

Closed
kong-dev opened this issue Aug 9, 2019 · 1 comment
Closed
Assignees

Comments

@kong-dev
Copy link
Contributor

kong-dev commented Aug 9, 2019

问题描述
页面中实现了 componentWillPreload(),则第二次及之后进入该页面时打印 this.$componentType 得到的值是 'COMPONENT'

复现步骤

/* xx.tsx */

export default class Page extends Taro.PureComponent<> {

  componentDidMount () {
      console.log('componentType: ', this.$componentType)
  }

  componentWillPreload (params: { [p: string]: any }): any {}

  render () {
    return (
      <View />
    )
  }
}

期望行为
正确得到的结果:'PAGE'

报错信息

系统信息
👽 Taro v1.3.10

Taro CLI 1.3.10 environment info:
System:
OS: macOS 10.14.5
Shell: 5.3 - /bin/zsh
Binaries:
Node: 10.16.0 - ~/.nvm/versions/node/v10.16.0/bin/node
Yarn: 1.16.0 - ~/.nvm/versions/node/v10.16.0/bin/yarn
npm: 6.9.0 - ~/.nvm/versions/node/v10.16.0/bin/npm
npmPackages:
@tarojs/async-await: 1.3.9 => 1.3.9
@tarojs/cli: 1.3.9 => 1.3.9
@tarojs/components: 1.3.9 => 1.3.9
@tarojs/plugin-babel: 1.3.9 => 1.3.9
@tarojs/plugin-csso: 1.3.9 => 1.3.9
@tarojs/plugin-sass: 1.3.9 => 1.3.9
@tarojs/plugin-uglifyjs: 1.3.9 => 1.3.9
@tarojs/redux: 1.3.9 => 1.3.9
@tarojs/redux-h5: 1.3.9 => 1.3.9
@tarojs/router: 1.3.9 => 1.3.9
@tarojs/taro: 1.3.9 => 1.3.9
@tarojs/taro-alipay: 1.3.9 => 1.3.9
@tarojs/taro-h5: 1.3.9 => 1.3.9
@tarojs/taro-swan: 1.3.9 => 1.3.9
@tarojs/taro-tt: 1.3.9 => 1.3.9
@tarojs/taro-weapp: 1.3.9 => 1.3.9
@tarojs/webpack-runner: 1.3.9 => 1.3.9
eslint-config-taro: 1.3.9 => 1.3.9
eslint-plugin-taro: 1.3.9 => 1.3.9
nerv-devtools: ^1.4.0 => 1.4.0
nervjs: ^1.4.0 => 1.4.0
stylelint-config-taro-rn: 1.3.9 => 1.3.9
stylelint-taro-rn: 1.3.9 => 1.3.9

补充信息

@tarojs/taro-weapp/src/native-api.js 中:

function processApis (taro) {
  // ...
        if (key === 'navigateTo' || key === 'redirectTo' || key === 'switchTab') {
          let url = obj['url'] ? obj['url'].replace(/^\//, '') : ''
          if (url.indexOf('?') > -1) url = url.split('?')[0]

          const Component = cacheDataGet(url)
          if (Component) {
            const component = new Component()
            if (component.componentWillPreload) {
              // ...
              cacheDataSet(preloadInitedComponent, component)
            }
          }
        }
 // ...
}

new Component() 没有传入 isPage 参数

@tarojs/taro-weapp/src/creat-component.js 中:

function createComponent (ComponentClass, isPage) {
 // ...
  const weappComponentConf = {
    data: initData,
    created (options = {}) {
      if (isPage && cacheDataHas(preloadInitedComponent)) {
        this.$component = cacheDataGet(preloadInitedComponent, true)
      } else {
        this.$component = new ComponentClass({}, isPage)
      }
      // ...
    },
    // ...
  }
  // ...
  return weappComponentConf
}

如在 cache 中找到 preload 已经初始化的组件则直接取用缓存中的实例,所以此时 this.$component 的 isPage 为 false

@taro-bot
Copy link

taro-bot bot commented Aug 9, 2019

欢迎提交 Issue~

如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏

如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。

Good luck and happy coding~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants