当前位置:网站首页>微信小程序中自定义模板
微信小程序中自定义模板
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"
}
}
})
相关链接
边栏推荐
- The form select in antd is received before it is selected
- leetcode-190.颠倒二进制位
- MySQL 实践篇 —— 主从复制
- Chapter 6 提升
- 如何配置adb环境变量(环境变量在哪打开)
- 验证码暴力破解测试[通俗易懂]
- How much do you know about JVM memory management
- Jenkins -- continuous integration server
- Beyond Istio OSS——Istio服务网格的现状与未来
- [FPGA]: ise and Modelsim joint simulation
猜你喜欢
随机推荐
Basic exercises of DQL in MySQL
Smart touch screen LCD bathroom mirror light touch chip-dlt8t02s-jericho
I copied the bottom of the liquidated NFT, but was locked by opensea
Leetcode 笔记 566. 重塑矩阵
Margin calculation
为什么说Crypto游戏正在改变游戏产业?
Use and source code of livedata in jetpack family bucket
Getderivedstatefromprops lifecycle
Go language - Application of stack - expression evaluation
FFT海浪模拟
LyScript 获取上一条与下一条指令
Jenkins -- continuous integration server
[FPGA] FIR filter - half band filter
Databinding+livedata can easily realize skin changing without restart
With 433 remote control UV lamp touch chip-dlt8sa20a-jericho
Android engineers, how to use kotlin to provide productivity?
Why neural networks are ineffective?
合并表格行---三层for循环遍历数据
Beyond Istio OSS——Istio服务网格的现状与未来
Leetcdoe-342. Power of 4









