首页 > 编程语言 >微信小程序---自定义组件和传参

微信小程序---自定义组件和传参

时间:2022-08-25 11:14:50浏览次数:56  
标签:传参 文件 自定义 wxml 微信 component 文件夹 组件

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'}}"/>

标签:传参,文件,自定义,wxml,微信,component,文件夹,组件
From: https://www.cnblogs.com/ljh980217/p/16623574.html

相关文章