组件数据

在小程序的组件中,用于组件模版渲染的私有数据,需要定义到data对象中。

  1. // components/test1/test1.js
  2. Component({
  3. /**
  4. * 组件的初始数据
  5. */
  6. data: {
  7. count: 0
  8. },
  9. })

组件方法

在小程序的组件中,事件处理函数饿自定义方法,需要定义到methods对象中。

  1. <!--components/test1/test1.wxml-->
  2. <button bind:tap="addCount">添加</button>
  1. // components/test1/test1.js
  2. Component({
  3. /**
  4. * 组件的初始数据
  5. */
  6. data: {
  7. count: 0
  8. },
  9. /**
  10. * 组件的方法列表
  11. */
  12. methods: {
  13. addCount() {
  14. this.setData({
  15. count: this.data.count + 1
  16. })
  17. }
  18. }
  19. })

组件属性

properties是组件对外的属性,用来接收外界传递到组件内的数据。

  1. <!--pages/home/home.wxml-->
  2. <view>
  3. <my-test1 max="10"></my-test1>
  4. </view>
  1. // components/test1/test1.js
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. max: {
  8. type: Number, // 数据类型
  9. value: 10 // 默认值
  10. },
  11. min: Number
  12. }
  13. })

dataproperties的区别?
1、在小程序中dataproperties都是可读可写的,这和Vue不同。
data倾向于存储组件的私有数据,properties倾向于存储外界传递到组件中的数据。
image.png