微信小程序开发-自定义组件Components

此为练习项目,将要对图中数据进行自定义组件。

自定义组件好处:不需要每个页面都写重复的css和html的标签,提高效率。
在这里插入图片描述
在这里插入图片描述

1.在根目录新建conponents文件夹

新建组件PubTitle步骤:
新建文件夹PubTitle >> 鼠标右键PubTitle文件夹,新建Conponent
在这里插入图片描述

2.引用

在将要引用的页面的.json文件
加上 “PubTitle”:"/components/PubTitle/PubTitle"

{
  "usingComponents": {
    "PubTitle":"/components/PubTitle/PubTitle"
  }
}

wxml页面:
属性myTitle将要属性传值
引用位置

3.文件夹conponents下在对应的组件文件夹下的.js

属性传值:{{myTitle}}
定义myTitle类型type,默认值value

myTitle:{
      type: String,
      value:''
    }

组件属性
.wxml

<view class="pubTitle">
  <view class="txt">{{myTitle}}</view>
  <navigator open-type="switchTab" class="more" url="/pages/news/news">更多></navigator>
</view>

.wcss样式

.pubTitle{
  display: flex;
  justify-content: space-between;
  height: 60rpx;
  align-items: center;
  margin-bottom: 40rpx;
}
.pubTitle .txt{
  font-size: 40rpx;
  height: 100%;
  position: relative;
  padding-left: 15rpx;
}
.pubTitle .txt::before{
  display: block;
  width: 5rpx;
  height: 34rpx;
  background: #c8020b;
  content: "";
  position: absolute;
  left: 0;
  top: 50%;
  margin-top: -15rpx;
}
.pubTitle .more{
  font-size: 32rpx;
  color: #666;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值