微信小程序里 template 模块的具体使用方式解析

why 263 2024-08-02

本篇文章给大家带来的内容是关于微信小程序中template模块的使用方法,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

WXML提供模板(template),可以在模板中定义代码片段,然后在不同的地方调用。
首先在pages目录下新建一个template目录用来存放模板
模板怎么建呢?
模板只需要新建xxx.wxss和xxx.wxml这两个文件就可以了,其他两个.js和.json不需要
在template目录中新建list-template.wxml和list-template.wxss

list-template.wxml:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<template name=&#39;listTmp&#39;><view>

  <view>

    <image src=&#39;/images/avatar/0.png&#39;></image>

    <text>may 9 2018</text>

  </view>

  <text>火影村</text>

  <image src=&#39;/images/detail/carousel/6.jpg&#39;></image>

  <text>火火火火火火火火火火火火火火火火火火火火火火火火火火火火火火火火火火火火火火火火火火火火火火火火火火火火火火火火</text>

  <view>

    <image src=&#39;/images/icon/view.png&#39;></image>

    <text>88</text>

    <image src=&#39;/images/icon/star.png&#39;></image>

    <text>88</text>

  </view></view></template>

list-template.wxss:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

.tmpContainer{

  display: flex; 

  flex-direction: column;

  }

.avatar_date image{

  width: 60rpx; 

  height: 60rpx; 

/* 把此元素放置在父元素的中部。就可以让图片与文字都在中间了 */

  vertical-align: middle; 

  margin-right: 10rpx;

  }

.avatar_date {

  padding: 10rpx;

  }

.avatar_date text{

  font-size: 32rpx;

  }

.company{

  margin-left: 10rpx; 

  font-size: 36rpx; 

  font-weight: 700; 

  margin: 10rpx;

  }

.contentImg{

  width: 100%; 

  height: 460rpx;

  }

.content{

  font-size: 32rpx; 

  /* 段落首字母缩进两个字 */

  text-indent: 64rpx;

  }

.collection_love image{

  width: 32rpx; 

  height: 32rpx; 

  vertical-align: middle; 

  margin-right: 10rpx;

  }

.collection_love text{

  font-size: 28rpx; 

  margin-right: 10rpx;

  }

在list.wxml中引入模板:

1

2

<!-- 在头部引入,注意结尾别忘了/ -->

<import src=&#39;/pages/template/list-template.wxml&#39;/>

然后在你想用模板的地方引用
image.png

还要在list.wxss中像以下样子引入模板样式

image.png


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微信小程序发送多条服务消息的代码实现方式探讨
下一篇:微信小程序中实现假数据评论功能的完整代码探究
相关文章

 发表评论

暂时没有评论,来抢沙发吧~