当前位置:网站首页>微信小程序中自定义模板
微信小程序中自定义模板
2022-07-28 12:20:00 【richest_qi】
文章目录
小程序项目
1. 定义模板。
项目根目录下新建文件夹:templates,用于存放模板。
文件夹templates下新建文件夹:temp,temp下新建文件:temp.wxml和temp.wxss。
temp.wxml中,通过 <template name=""></template> 定义模板。
2. 新建页面。
小程序根目录下新建文件夹:test,在文件夹test下新建Page:test。
3. 目标页面中使用模板。
目标页面的wxml文件通过<import src=""/>引入模板的wxml文件。
目标页面的wxml文件通过 <template is=""></template> 使用模板。
目标页面的wxss文件得通过@import ""引入模板的wxss文件。
代码涉及的主要文件有:
- templates/temp/temp.wxml
- templates/temp/temp.wxss
- pages/test/test.json
- pages/test/test.wxml
- pages/test/test.wxss
- pages/test/test.js

templates/temp/temp.wxml
<!-- 定义模板 -->
<template name="my-template">
<view class="user-box">
<image class="avatar" src="{
{avatarUrl}}"/>
<view class="username">{
{username}}</view>
</view>
</template>
templates/temp/temp.wxss
.user-box{
text-align: center;
}
.user-box .avatar{
width: 300rpx;
height: 300rpx;
border-radius: 50%;
}
.user-box .username{
font-size: 48rpx;
font-family:fantasy;
color: #1761d8;
height: 80rpx;
line-height: 80rpx;
}
pages/test/test.json
{
"usingComponents": {
},
"navigationBarBackgroundColor": "#eee",
"navigationBarTextStyle": "black",
"navigationBarTitleText": "我的"
}
pages/test/test.wxml
<import src="/templates/temp/temp.wxml"/>
<view class="test-container">
<template is="my-template" data="{
{...user}}"></template>
</view>
pages/test/test.wxss
@import "/templates/temp/temp.wxss";
.test-container{
height: 100%;
background: #eee;
}
pages/test/test.js
Page({
data:{
user:{
username:"Mr Duck",
avatarUrl:"/static/images/avatar.png"
}
}
})
相关链接
边栏推荐
- Resolve browser password echo
- PCP parity principle arbitrage
- Bear market spread portfolio
- 倒计时 2 天!2022 中国算力大会:移动云邀您共见算力网络,创新发展
- How to design a second kill system?
- Form for real-time custom verification
- Mysql中DQL基本练习
- Is jetpack compose completely out of view?
- [FPGA]: ISE generates MCS file and burning process
- Using auto.js to realize the function of fifaol3 mobile terminal card interceptor
猜你喜欢

ES6 null merge operator (?)

IP电话系统和VoIP系统使用指南
![[error] after logging in to another machine using SSH, you find that the hostname is still yourself | unable to access yarn8088](/img/81/641a5b3445534fc3b8c87ee6deaa64.png)
[error] after logging in to another machine using SSH, you find that the hostname is still yourself | unable to access yarn8088

Intrinsic value and time value of options

One track education, PHP training, unity of knowledge and practice, popular

一根筋教育PHP培训 知行合一收热捧
![[FPGA] FIR filter - half band filter](/img/6e/d97b3842f80e37aa41b888384a14cb.png)
[FPGA] FIR filter - half band filter

Realize the mutual value transfer between main window and sub window in WPF

Jenkins--持续集成服务器

How to design a second kill system?
随机推荐
The form select in antd is received before it is selected
为什么说Crypto游戏正在改变游戏产业?
2020jenkins study notes
Change password, confirm password verification antd
Stepless dimming colorful RGB mirror light touch chip-dlt8s12a-jericho
I copied the bottom of the liquidated NFT, but was locked by opensea
Using auto.js to realize the function of fifaol3 mobile terminal card interceptor
合并表格行---三层for循环遍历数据
How does the vditor renderer achieve server-side rendering (SSR)?
Chapter 6 promotion
《如何打一场数据挖掘赛事》入门版
Definition of option basis
[matlab]: FFT related problems
Deployment之滚动更新策略。
倒计时 2 天!2022 中国算力大会:移动云邀您共见算力网络,创新发展
leetcode-190.颠倒二进制位
夜神模拟器抓包微信小程序
Beyond Istio OSS——Istio服务网格的现状与未来
一根筋教育PHP培训 知行合一收热捧
Array, string de duplication