NervJS/taro

鸿蒙平台taroGlobalData初始化不正确, 通过Taro.getApp()无法获取taroGlobalData中的属性

hpsoar opened this issue · 2 comments

相关平台

鸿蒙

复现仓库

https://github.com/hpsoar/taro_harmony_app_init.git

使用框架: React

复现步骤

运行后Taro.getApp()获取到的app对象,没有携带taroGlobalData中的数据。

期望结果

App中配置了taroGlobalData属性

class App extends React.Component {
  taroGlobalData = {
    'hello': 'world'
  }
  render() {
    return this.props.children;
  }
}

预期

const app = Taro.getApp();
app.hello有值

实际结果

实际app.helloundefined

环境信息

👽 Taro v4.0.0-beta.75


  Taro CLI 4.0.0-beta.75 environment info:
    System:
      OS: macOS 14.5
      Shell: 5.9 - /bin/zsh
    Binaries:
      Node: 20.5.0 - ~/.nvm/versions/node/v20.5.0/bin/node
      Yarn: 1.22.10 - /usr/local/bin/yarn
      npm: 9.8.0 - ~/.nvm/versions/node/v20.5.0/bin/npm
    npmPackages:
      @tarojs/cli: 4.0.0-beta.75 => 4.0.0-beta.75
      @tarojs/components: 4.0.0-beta.75 => 4.0.0-beta.75
      @tarojs/helper: 4.0.0-beta.75 => 4.0.0-beta.75
      @tarojs/plugin-framework-react: 4.0.0-beta.75 => 4.0.0-beta.75
      @tarojs/plugin-platform-alipay: 4.0.0-beta.75 => 4.0.0-beta.75
      @tarojs/plugin-platform-h5: 4.0.0-beta.75 => 4.0.0-beta.75
      @tarojs/plugin-platform-jd: 4.0.0-beta.75 => 4.0.0-beta.75
      @tarojs/plugin-platform-qq: 4.0.0-beta.75 => 4.0.0-beta.75
      @tarojs/plugin-platform-swan: 4.0.0-beta.75 => 4.0.0-beta.75
      @tarojs/plugin-platform-tt: 4.0.0-beta.75 => 4.0.0-beta.75
      @tarojs/plugin-platform-weapp: 4.0.0-beta.75 => 4.0.0-beta.75
      @tarojs/react: 4.0.0-beta.75 => 4.0.0-beta.75
      @tarojs/runtime: 4.0.0-beta.75 => 4.0.0-beta.75
      @tarojs/shared: 4.0.0-beta.75 => 4.0.0-beta.75
      @tarojs/taro: 4.0.0-beta.75 => 4.0.0-beta.75
      babel-preset-taro: 4.0.0-beta.75 => 4.0.0-beta.75
      eslint-config-taro: 4.0.0-beta.75 => 4.0.0-beta.75
      react: ^18.0.0 => 18.3.1
    npmGlobalPackages:
      typescript: 5.4.5

补充信息

entry/src/main/ets/app.ets

onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
    AppStorage.setOrCreate('__TARO_ENTRY_PAGE_PATH', 'pages/index/index')
    AppStorage.setOrCreate('__TARO_PAGE_STACK', [])
    // 引入
    initHarmonyElement()
    this.app = createComponent()
    callFn(this.app?.onLaunch, this, ObjectAssign(want, launchParam))
  }

传到onLaunch中的thisEntryAbility

因此,在以下代码中,把taroGlobalData中定义的属性加到EntryAbility上了,而不是全局app上。

// entry/src/main/ets/npm/@tarojs/plugin-framework-react/dist/runtime/app.ts

  onLaunch (launchParam?: any) {
      waitAppWrapper(() => {
        // 用户编写的入口组件实例
        const app = getAppInstance()
        this.$app = app

        if (app) {
          // 把 App Class 上挂载的额外属性同步到全局 app 对象中
          if (app.taroGlobalData) {
            const globalData = app.taroGlobalData
            const keys = Object.keys(globalData)
            const descriptors = Object.getOwnPropertyDescriptors(globalData)
            keys.forEach(key => {
              Object.defineProperty(this, key, {
                configurable: true,
                enumerable: true,
                get () {
                  return globalData[key]
                },
                set (value) {
                  globalData[key] = value
                }
              })
            })
            Object.defineProperties(this, descriptors)
          }

          app.onCreate?.()
        }

        eventCenter.trigger('__taroRouterLaunch', launchParam)
      })
    },

H5中是正常的。

复现代码是harmony的代码,只有一个页面。

这里确实是一个 bug,本周发版修复

已修复至 4.0.0@beta.81