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

利用get实现computed出现问题 #4790

Closed
Daisy1213 opened this issue Nov 8, 2019 · 1 comment
Closed

利用get实现computed出现问题 #4790

Daisy1213 opened this issue Nov 8, 2019 · 1 comment
Labels
enhancement New feature or request

Comments

@Daisy1213
Copy link

想通过react类的写法中get方法实现computed的思想,测试代码如下,测试输出为6,理想输出为8

import {
  Block,
  View,
} from '@tarojs/components'
import Taro from '@tarojs/taro'
import withWeapp from '@tarojs/with-weapp'

@withWeapp({
  data: {
    a: 1,
    b: 2,
    c: 3
  },
  onReady () {
    setTimeout(() => {
      this.setData({
        b : 4
      })
    }, 1000)
  }

})
class _C extends Taro.Component {
  get sum () {
    const {a, b, c} = this.data
    return a + b + c
  }


  render() {
    console.log(this.data.b)

    return (
      <Block>
        <View>{this.sum}</View>
      </Block>
    )
  }
}

export default _C

期望行为
希望当b更新后,重新渲染时会再次调用this.sum重新计算和,输出应该为8

系统信息
image

尝试
尝试使用react中写法,把数据挂在了state上,在willMount里更新b的值,发现结果和期望的一致,但是由于我的代码是从原生小程序转换过来的,如果改成这种写法成本很大,看到文档上描述上面的代码应该是可行的,不知道是哪里出了问题,求解!!!
image

@taro-bot
Copy link

taro-bot bot commented Nov 8, 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
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants