当前位置:网站首页>Custom components in applets
Custom components in applets
2022-07-01 04:04:00 【richest_ qi】
List of articles
Applet project
The main files involved in the code are :
- app.json
- pages/index/index.wxml
- pages/index/index.wxss
- pages/index/index.js

app.json
{
"pages": [
"pages/index/index"
],
"window": {
"navigationBarBackgroundColor": "#971a22",
"navigationBarTitleText": " home page ",
"navigationBarTextStyle": "white"
},
"style": "v2",
"sitemapLocation": "sitemap.json"
}
pages/index/index.wxml
<view class="indexContainer">
<view class="recommendSongContainer">
<view class="header">
<view class="title"> Recommended song list </view>
<view>
<text class="desc"> Meet your true love song list </text>
<text class="btn">></text>
</view>
</view>
<scroll-view class="recommendSongList" enable-flex scroll-x>
<view class="recommendItem" wx:for="{
{list}}" wx:key="id">
<image src="{
{item.picUrl}}"></image>
<view class="name">{
{item.title}}</view>
</view>
</scroll-view>
</view>
<view class="newSongContainer">
<view class="header">
<view class="title"> New song express </view>
<view>
<text class="desc"> More fresh music, more quick fall </text>
<text class="btn">></text>
</view>
</view>
</view>
<view class="newDiscContainer">
<view class="header">
<view class="title"> New dishes are on the shelves </view>
<view>
<text class="desc"> The new magic disc sounds so popular </text>
<text class="btn">></text>
</view>
</view>
</view>
<view class="screamSongContainer">
<view class="header">
<view class="title"> Scream new song list </view>
<view>
<text class="desc"> Trendsetters must listen to the hot new list </text>
<text class="btn">></text>
</view>
</view>
</view>
</view>
pages/index/index.wxss
.indexContainer{
padding: 0 20rpx;
}
.header{
display: flex;
justify-content: space-between;
align-items: center;
margin: 20rpx 0;
}
.header .title{
font-weight: bold;
}
.header .desc{
font-size: 24rpx;
color: #333;
padding: 0 20rpx;
}
.header .btn{
font-size: 24rpx;
padding: 0 10rpx;
background: #eee;
color: #888;
border-radius: 6rpx;
}
.recommendSongList{
display: flex;
height: 280rpx;
}
.recommendSongList image{
width: 200rpx;
height: 200rpx;
border-radius: 10rpx;
margin-right: 10rpx;
}
.recommendSongList .name{
font-size: 26rpx;
display: -webkit-box;
overflow: hidden;
text-overflow:ellipsis;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
}
pages/index/index.js
Page({
data:{
list:[]
},
onLoad(){
this.getDataFromServer();
},
getDataFromServer(){
const result = [
{
id: 494479726, picUrl: "https://p2.music.126.net/58ox3zVEmosSrdLaKj6x5w==/109951162827155600.jpg", title: " Back shake MV | It turns out that back shaking can also be visualized "},
{
id: 135595185, picUrl: "https://p2.music.126.net/11ZHf0G9FQzWNi-8sFzCmw==/109951164541050373.jpg", title: "Acid Jazz Acid jazz — In Jazz “ Acidic ” The fusion "},
{
id: 114846926, picUrl: "https://p2.music.126.net/GTf1b1G2dAs1SleurIfcJg==/3399689953594431.jpg", title: " The suffixes of song titles are popular science "},
{
id: 113780871, picUrl: "https://p2.music.126.net/qxUWSKhrd1UWME8oAYTMFQ==/109951164497486331.jpg", title: "「Cool Jazz」 Cold Jazz makes you relax "},
{
id: 2962407224, picUrl: "https://p2.music.126.net/NW1GEN3sruiLpT4AZrdDFw==/109951165669533032.jpg", title: "『 British female voice 』 The surviving and independent British rose "},
{
id: 2107922801, picUrl: "https://p2.music.126.net/b--QkIcFfdxz_DryW55ZfA==/109951163165101034.jpg", title: " Acoustooptic aesthetics I Classical soundtrack in classic movies "},
{
id: 78669437, picUrl: "https://p2.music.126.net/AfN_yyi-fQe8POTMKJFjAA==/7957165650297535.jpg", title: " Divine episode in American drama "},
{
id: 3116763088, picUrl: "https://p2.music.126.net/MSom1XSXqt5K9ArZlJ29CQ==/109951164608648583.jpg", title: " The power of music I Secular love songs of the Renaissance "},
{
id: 101354498, picUrl: "https://p2.music.126.net/dmj9iqz8MsD_sCd1xBF0WA==/7918682744429845.jpg", title: " Fashion sports • Essential rhythm "}
]
this.setData({
list:result})
}
})
Custom components

Create a new folder under the project root directory :components,components I'm gonna go ahead and create a new folder :Header,Header Under the new Component, Automatic file generation : Header.wxml、Header.wxss、Header.js and Header.json.
The documents involved in the code change are :
- components/Header/Header.wxml
- components/Header/Header.wxss
- components/Header/Header.js
- pages/index/index.json
- pages/index/index.wxml
- pages/index/index.wxss

components/Header/Header.wxml
<view class="header">
<view class="title">{
{title}}</view>
<view>
<text class="desc">{
{desc}}</text>
<text class="btn">></text>
</view>
</view>
components/Header/Header.wxss
.header{
display: flex;
justify-content: space-between;
align-items: center;
margin: 20rpx 0;
}
.header .title{
font-weight: bold;
}
.header .desc{
font-size: 24rpx;
color: #333;
padding: 0 20rpx;
}
.header .btn{
font-size: 24rpx;
padding: 0 10rpx;
background: #eee;
color: #888;
border-radius: 6rpx;
}
components/Header/Header.js
Component({
properties: {
title:{
type:String,
value:" Default title "
},
desc:{
type:String,
value:" Default description "
}
},
data: {
},
methods: {
}
})
pages/index/index.json
{
"usingComponents": {
"Header":"/components/Header/Header"
}
}
pages/index/index.wxml
<view class="indexContainer">
<view class="recommendSongContainer">
<Header title=" Recommended song list " desc=" Meet your true love song list "/>
<scroll-view class="recommendSongList" enable-flex scroll-x>
<view class="recommendItem" wx:for="{
{list}}" wx:key="id">
<image src="{
{item.picUrl}}"></image>
<view class="name">{
{item.title}}</view>
</view>
</scroll-view>
</view>
<view class="newSongContainer">
<Header title=" New song express " desc=" More fresh music, more quick fall "/>
</view>
<view class="newDiscContainer">
<Header title=" New dishes are on the shelves " desc=" The new magic disc sounds so popular "/>
</view>
<view class="screamSongContainer">
<Header title=" Scream new song list " desc=" Trendsetters must listen to the hot new list "/>
</view>
</view>
pages/index/index.wxss
.indexContainer{
padding: 0 20rpx;
}
.recommendSongList{
display: flex;
height: 280rpx;
}
.recommendSongList image{
width: 200rpx;
height: 200rpx;
border-radius: 10rpx;
margin-right: 10rpx;
}
.recommendSongList .name{
font-size: 26rpx;
display: -webkit-box;
overflow: hidden;
text-overflow:ellipsis;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
}
Summary
Create custom components
Use... In wechat applet Component() Implement custom components , It takes an object as a parameter , The object contains multiple attributes , among , Commonly used :
properties, External properties of components , amount to vue or react Medium props. The type isObject Map, Not required . Such as
properties: {
title:{
type:String, // Type of attribute , Required
value:" Default title " // The initial value of the property , Not required
}
}
data, Data inside the component itself . The type isObject, Not required .methods, Component approach .
Use custom components
Before using custom components , First, on the page json Make a reference statement in the document , As shown below , such , Page wxml You can use custom components just like basic components .
{
"usingComponents": {
"Header":"/components/Header/Header"
}
}
Related links
Use scroll-view Possible problems and solutions in implementing slider view
Introduction to custom components
Component Constructors
边栏推荐
- Note de développement du système embarqué 80: application du concepteur Qt à la conception de l'interface principale
- 分账技术赋能农贸市场,重塑交易管理服务效能
- 熊市下的Coinbase:亏损、裁员、股价暴跌
- 高并发下接口幂等性如何保证?
- Quickly filter data such as clock in time and date: Excel filter to find whether a certain time point is within a certain time period
- [today in history] June 30: von Neumann published the first draft; The semiconductor war in the late 1990s; CBS acquires CNET
- 318. Maximum word length product
- [JPCs publication] the Third International Conference on control theory and application in 2022 (icocta 2022)
- 【TA-霜狼_may-《百人计划》】2.3 常用函数介绍
- Unity之三维空间多点箭头导航
猜你喜欢

【TA-霜狼_may-《百人计划》】2.1 色彩空间

LeetCode 1828. Count the number of points in a circle

【TA-霜狼_may-《百人计划》】1.3纹理的秘密

Loop filtering based on Unet

【TA-霜狼_may-《百人计划》】2.3 常用函数介绍

25.k sets of flipped linked lists
![[TA frost wolf \u may- hundred people plan] 2.4 traditional empirical lighting model](/img/05/85c004e4fbfc8d4984ac04ddb1190b.png)
[TA frost wolf \u may- hundred people plan] 2.4 traditional empirical lighting model

Embedded System Development Notes 79: why should I get the IP address of the local network card

DO280管理应用部署--RC

431. 将 N 叉树编码为二叉树 DFS
随机推荐
8. string conversion integer (ATOI)
Qt development experience tips 226-230
389. find a difference
10. regular expression matching
Class and object finalization
线程常用方法与守护线程
This may be your last chance to join Tencent
Inventory the six second level capabilities of Huawei cloud gaussdb (for redis)
Spock单元测试框架介绍及在美团优选的实践___第一章
LeetCode 1827. Increment array with minimal operation
[EI conference] 2022 international joint civil and Offshore Engineering Conference (jccme 2022)
[ta - Frost Wolf May - 100 people plan] 2.3 Introduction aux fonctions communes
318. 最大单词长度乘积
LetCode 1829. Maximum XOR value per query
JMeter学习笔记2-图形界面简单介绍
Promql select time series
10. 正则表达式匹配
674. longest continuous increasing sequence force buckle JS
【TA-霜狼_may-《百人计划》】1.1 渲染流水线
How keil displays Chinese annotations (simple with pictures)