wechat-miniprogram / computed

小程序自定义组件 computed / watch 扩展
MIT License
653 stars 60 forks source link

能不能支持在一个computed里访问另一个computed变量? #50

Closed ljjsimon closed 3 years ago

ljjsimon commented 3 years ago

比如 computed:{ a(data){ return data.b+'bar' }, b(data){ return 'foo' } }

LastLeaf commented 3 years ago

试了下版本 >=2.0.0 是有支持的。怀疑有问题的话就给个代码片段?

ljjsimon commented 3 years ago

@LastLeaf 就上面这个代码啊。。。我console.log(this.data.a)输出是undefinedbar

ljjsimon commented 3 years ago

@LastLeaf 我是2.2.0

LastLeaf commented 3 years ago

我用你的代码 + v2.2.0 试了下,在 wxml 数据绑定里面显示完全正常。

有问题的话请提供完整代码片段。

ljjsimon commented 3 years ago

难道是我见鬼了不成? package.json

{
  "name": "weapp",
  "version": "1.0.0",
  "description": "这是云开发的快速启动指引,其中演示了如何上手使用云开发的三大基础能力:",
  "main": "index.js",
  "dependencies": {
    "@vant/weapp": "^1.6.6",
    "miniprogram-computed": "^4.0.1"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

index.js

const computedBehavior = require('miniprogram-computed')
Page({
  behaviors: [computedBehavior],

  /**
   * 页面的初始数据
   */
  data: {
    url:''
  },
  computed:{
    a(data){
      return data.b+'bar'
    },
    b(data){
      return 'foo'
    }
  }
})

index.wxml

      <view style="margin-top:100px;">{{a}}</view>
截屏2021-01-22 上午9 48 36
LastLeaf commented 3 years ago

这段代码里面写的是 "miniprogram-computed": "^4.0.1" ,然而 v4 的接口变了,下面的 const computedBehavior = require('miniprogram-computed') 调用会失效。

麻烦看下更改版本之后有没有 npm install 和点击开发者工具中的“工具 > 构建 npm ”。

ljjsimon commented 3 years ago

@LastLeaf 可以了,谢谢。之前是2.2.0,版本和代码都是从小程序文档复制过来的https://developers.weixin.qq.com/miniprogram/dev/extended/utils/computed.html。 今天早上升级后没有构建,刚构建完就好了

LastLeaf commented 3 years ago

有可能是以前也忘了构建。我试过 v2.2.0 也是 ok 的。但是 v1 不行。