1.自定义组件

1.在项目根目录中新建components文件夹
2.在components文件夹下新建组件的文件夹,如zujian
3.鼠标右键点击zujian文件夹,选择新建component,就会生成wxml,wxss,js,json四个文件
4.在组件的json文件中进行自定义组件声明

{
  "component": true
}

5.在需要使用组件的页面,json文件中声明

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

6.在wxml中使用

<component-tag-name></component-tag-name>

2.传参

1.在使用组件的wxml中传参

<component-tag-name title="作品"></component-tag-name>

2.在自定义组件的js文件中,properties节点接收该属性

 properties: {
    title:{
      type:String,
      value:""
    }
  }

3.在自定义组件的wxml文件中使用

<view>
  {{title}}
</view>

3.wxml提供的文件引用方式

1.在被引入的item.wxml文件中定义

<template name="item">
  <text>{{title}}</text>
</template>

2.在需要使用的页面,wxml中使用

方式一:
<import src="item.wxml"/>
方式二:
<template is="item" data="{{title: 'forbar'}}"/>
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐