自定义组件

创建组件

如下图,在文件夹中创建自定义组件:


自定义组件也是由json、wxml、wxss、js 4个文件组成;与页面的区别如下:

1.0> json 文件中的 component 为 true

{
  "component": true
}

2.0> js 文件中使用 Component() 来注册组件,并提供组件的属性定义、内部数据和自定义方法

Component({
  properties: {
    // 组件的属性列表
  },
  data: {
    // 组件的初始数据
  },
  methods: {
    // 这里是一个自定义方法
    customMethod: function(){}
  }
})


使用自定义组件

1.0> 在 json 文件中引入

{
  "usingComponents": {
    "component-tag-name": "path/to/the/custom/component"
  }
}

2.0> 在页面中使用

<!-- 以下是对一个自定义组件的引用 -->
<component-tag-name inner-text="Some text"></component-tag-name>





举报

© 著作权归作者所有


1