网站首页 > 文章精选 正文
样式导入
在根目录下(与pages目录同级)创建common目录,并创建common.wxss:
.red_big_text {
color: red;
font-size: xx-large;
}
在具体页面的wxss文件中引用:
@import "../../common/common.wxss";
就可以在页面的wxml中使用common的样式了。就当是css就行。
脚本导入
遵循CommonJS规范。
在根目录下(与pages目录同级)创建utils目录,并创建utils.js:
const formatDateTime = function (date) {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
return `${[year, month, day].map(formatNumber).join('-')} ${[hour, minute, second].map(formatNumber).join(':')}`
}
const formatNumber = n => {
n = n.toString()
return n[1] ? n : `0${n}`
}
function unixTimeStampToDate(timestamp) {
const date = new Date(timestamp);
return date.toLocaleDateString();
}
const unixTimeStampToDateTime = function (timestamp) {
const date = new Date(timestamp);
return date.toLocaleDateString() + ' ' + date.toLocaleTimeString();
}
const unixTimeStampToDefaultDateTime = timestamp => {
const date = new Date(timestamp);
const year = date.getFullYear();
const month = ('0' + (date.getMonth() + 1)).slice(-2);
const day = ('0' + date.getDate()).slice(-2);
const hours = ('0' + date.getHours()).slice(-2);
const minutes = ('0' + date.getMinutes()).slice(-2);
const seconds = ('0' + date.getSeconds()).slice(-2);
return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
}
module.exports = {
formatDateTime: formatDateTime,
unixTimeStampToDate: unixTimeStampToDate,
unixTimeStampToDateTime: unixTimeStampToDateTime,
unixTimeStampToDefaultDateTime: unixTimeStampToDefaultDateTime
}
在具体页面的js中导入并使用:
const util=require('../../utils/util.js')
Page({
onLoad() {
this.setData({
nowDateTime:util.formatDateTime(new Date()),
unixDate:util.unixTimeStampToDate(-639075600000),
unixDateTime:util.unixTimeStampToDateTime(-639075600000),
unixTimeToDefaultDateTime:util.unixTimeStampToDefaultDateTime(-639075600000)
})
}
})
<view>
<label>Date格式化:</label><text>{{nowDateTime}}</text>
<label>Unix时间戳转日期:</label><text>{{unixDate}}</text>
<label>Unix时间戳转日期时间:</label><text>{{unixDateTime}}</text>
<label>Unix时间戳自定义格式化:</label><text>{{unixTimeToDefaultDateTime}}</text>
</view>
模板导入
在根目录下(与pages目录同级)创建template目录--productlist目录--productlist的page。
productlist.wxss
.productList{
display: flex;
margin-top: 10rpx;
}
.productList .productImg{
width: 200rpx;
height: 200rpx;
background-color: seagreen;
}
.productList .productInfo{
flex: 1;
background-color: sienna;
}
productlist.wxml
<template name="productList">
<view class="productList">
<view class="productImg">{{productImg}}</view>
<view class="productInfo">{{productId}} {{productName}} {{price/100}}元</view>
</view>
</template>
就是普通的wxml页面的写法,关键点是template标签及name属性。
在新的page里引入模板。
在wxss导入样式:
@import "../../template/productlist/productlist.wxss";
.scrollarea {
flex: 1;
overflow-y: hidden;
}
在js里加一些数据:
data:{
productList:[
{"productImg":"产品图片1","productId":"1","productName":"商品名称1","price":10000},
{"productImg":"产品图片2","productId":"2","productName":"商品名称2","price":22200}
]
}
在wxml导入模板:
<navigation-bar title="模板导入" back="{{false}}"></navigation-bar>
<import src="../../template/productlist/productlist" />
<scroll-view class="scrollarea" scroll-y="true" type="list" style="height:{{windowHeight}}px">
<view wx:for="{{productList}}">
<template is="productList" data="{{...item}}" />
</view>
</scroll-view>
关键点就是import标签导入模板文件,然后用template标签和is属性指向要使用的模板name。
要传递数据到模板文件,如果用{{...数据变量}},则模板里面直接用属性名即可{{productName}}。
如果直接用{{数据变量}},则模板文件里面要用{{item.productName}}。
猜你喜欢
- 2025-01-04 「炫丽」从0开始做一个WPF+Blazor对话小程序
- 2025-01-04 adonisjs的模板以及路由
- 2025-01-04 5、谈谈你对BFC的理解?
- 2025-01-04 前端 BFC、IFC、GFC 和 FFC,这些你都知道吗?
- 2025-01-04 Wijmo5 Flexgrid基础教程:自定义编辑器
- 2025-01-04 H5小游戏开发教程之页面基础布局的开发
- 2025-01-04 web前端:CSS的常用属性速查表
- 2025-01-04 用网页做个ppt- 定时全屏切换图片
- 2025-01-04 HTML翻牌器:用CSS和HTML元素创造动态数字展示
- 2025-01-04 「网络安全」安全设备篇(漏洞扫描器-流量监控-安全审计产品)
- 最近发表
- 标签列表
-
- newcoder (56)
- 字符串的长度是指 (45)
- drawcontours()参数说明 (60)
- unsignedshortint (59)
- postman并发请求 (47)
- python列表删除 (50)
- 左程云什么水平 (56)
- 计算机网络的拓扑结构是指() (45)
- 稳压管的稳压区是工作在什么区 (45)
- 编程题 (64)
- postgresql默认端口 (66)
- 数据库的概念模型独立于 (48)
- 产生系统死锁的原因可能是由于 (51)
- 数据库中只存放视图的 (62)
- 在vi中退出不保存的命令是 (53)
- 哪个命令可以将普通用户转换成超级用户 (49)
- noscript标签的作用 (48)
- 联合利华网申 (49)
- swagger和postman (46)
- 结构化程序设计主要强调 (53)
- 172.1 (57)
- apipostwebsocket (47)
- 唯品会后台 (61)
- 简历助手 (56)
- offshow (61)