菜鸟虚心请教微信小程序的问题,为什么在attached后面加了function()之后就不会执行?

最近在学微信小程序,跟着里面的老师走了一遍。

 lifetimes: {
    attached:function(){
      var that = this;
      console.log(that.properties.rate);
    }  
  }

按照这样的代码执行之后,控制台不会显示收到的rate变量。
然后我就比较了下面两段代码:

 lifetimes: {
    attached:function(){
      console.log("123");
    }      
  }
 lifetimes: {
    attached:
      console.log("123")
  }

发现下面一段执行,上面一段不执行。这是为啥。
帮帮我这个菜鸟吧!万分感谢!

个人理解 第一段的 attached是一个函数,你没有调用肯定不能执行, 第二段的 attached 相当于只是一个表达式,所以就相当于赋值

libVersion修改一下,如2.6.5

你好!我遇到和你一样的问题。我把现有的代码拷贝给你看看

// components/starts/starts.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    rate: {
      type: Number,
      value: 0
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {

  },

  attached: function () {
    console.log("======================里面执行啦!!!!");
      var that = this;
      console.log("22222222222222222")
      var rate = that.data.rate;
      console.log("==================rate="+this.properties.rate);
      var intRate = parseInt(rate);
      var light = parseInt(intRate / 2);
      var half = intRate % 2;
      var gray = 5 - light - half;
      console.log("==================");
      console.log(light);
      console.log(half);
      console.log(gray);
      console.log("==================");
  },
  lifetimes: {
    // 生命周期函数,可以为函数,或一个在methods段中定义的方法名
    attached: function () {
      onsole.log("======================222执行啦!!!!");
     },
    moved: function () { },
    detached: function () { },
  }
})

和官方文档说明的有点不一样,lifetimes字段里面的attached不会出现覆盖的情况,我直接在外部写attached就能拿到我们想要的效果的