1、不带参数

首先在pages文件夹中新建一个template文件夹,文件夹中新建一个template.wxml文件,代码如下

<!--template.wxml-->
<template name="msgItem">
<view>
<text>This is template.wxml文件,我是一个模板</text>
</view>
</template>

然后我们书写我们所要调用template的页面index.wxml

<!--index.wxml-->
<!-- 声明需要使用的模板文件 -->
<import src ="../template/template.wxml"/>
  <view>This is index.wxml</view>
<template is="msgItem"/>

注意:

(1)index.wxml中template 标签的is属性与template.wxml中template 标签的name属性相同

(2)index.wxml文件中要通过import标签声明需要使用的模板文件

运行效果:

2、带参数

首先,修改template.wxml文件,我们给模板添加三个字段,修改后代码如下

<template name="msgItem">
<view>
<text>This is template.wxml文件,我是一个模板</text>
<view>
<text> {{index}}: {{msg1}} </text>
<text> {{msg2}} </text>
</view>
</view>
</template>

接下来我们在index.wxml中传递模板中所需要的三个参数,修改后的代码如下:

<!--index.wxml-->
<!-- 声明需要使用的模板文件 -->
<import src ="../template/template.wxml"/>
<view>This is index.wxml</view>
<template is="msgItem" data="{{index:1,msg1:'msg1数据',msg2:'msg2数据'}}"/>

运行效果:

3、列表item模板

接下来我们就通过一种常见的情况列表数据来使用模板,增加对模板的认知,直接上修改过的代码:

//index.js
Page({
data: {
list:[
{ name: '张三', age: 15 },
{ name: '李四', age: 25 },
{ name: '王五', age: 18 },
{ name: '赵六', age: 19 },
]
}
})
<!--index.wxml-->
<!-- 声明需要使用的模板文件 -->
<import src ="../template/template.wxml"/>
<view>This is index.wxml</view>
<view wx:for="{{list}}">
<template is="msgItem" data="{{name:item.name,age:item.age}}"/>
</view>
<!--template.wxml-->
<template name="msgItem">
<view>
<text> name: {{name}} </text>
<text> age: {{age}}</text>
</view>
</template>

运行效果:

4、使用模板样式

接下来我们就给模板增加样式文件,然后在需要调用模板的地方使用该样式。

首先在pages/template文件夹中新建一个template.wxss文件,然后对模板文件,添加一个简单样式。

<!--template.wxml-->
<template name="msgItem">
<view class="template_style">
<text> name: {{name}} </text>
<text class="template_age_style"> age: {{age}}</text>
</view>
</template>
/* pages/template/template.wxss */
.template_style{
border-bottom: solid 2px #999999;
padding: 5px;
font-size: 18px;
color: #000000; }
.template_age_style{
color: #666666;
font-size: 14px;
}

我们的样式文件已经创建好了,接下来在index.wxml中引入该样式文件。

/**index.wxss**/
@import "../template/template.wxss";

运行效果:

5、调用不同的模板

有时候,我们有这样的需求,那就是同一个列表中,item数据不同,可能他的样式也是有很大的区别,所以我们使用的模板也会对应不相同,接下来我们就来实现这样需求的小Demo: 
首先修改了一下template.wxml,原本该文件中只有一个template,现在我们创建了两个,新增的template仅仅多了一行代码,当然了实际开发中,需求会比这个难很多,在这里只是为了实现Demo。

<!--template.wxml-->
<template name="msgItem">
<view class="template_style">
<text> name: {{name}} </text>
<text class="template_age_style"> age: {{age}}</text>
</view>
</template>
<template name="msgItem2">
<view class="template_style">
<text> name: {{name}} </text>
<text class="template_age_style"> age: {{age}}</text>
<text>我是一个未成年</text>>
</view>
</template>

接下来我们在index.wxml中通过age字段调用不同的模板:

<!--index.wxml-->
<!-- 声明需要使用的模板文件 -->
<import src ="../template/template.wxml"/>
<view>This is index.wxml</view>
<view wx:for="{{list}}">
<template is="{{item.age >= 18 ? 'msgItem' : 'msgItem2'}}" data="{{name:item.name,age:item.age}}"/>
</view>

运行效果:

微信小程序 引入公共页面的几种情况的更多相关文章

  1. 微信小程序引入md5.js

    今天给大家安利一下微信小程序引入md5.js的方法,不多说 md5.js在下面 直接复制到项目的utils/md5.js即可 /* * A JavaScript implementation of t ...

  2. uni-app开发微信小程序引入UI组件库(Vant-weapp)步骤

    uni-app开发微信小程序引入UI组件库(Vant-weapp)步骤 这里以vant-weapp为例 uni-app官方文档介绍引入组件的方法 1. 新建相关目录 根目录下创建 wxcomponen ...

  3. Java Web项目,Android和微信小程序的初始页面配置

    Java Web项目 我们在Eclipse里开了Java Web项目之后,Run As Tomcat或者Apache服务器,本地运行,如果直接用http://localhost:8080访问项目,会发 ...

  4. (十二)微信小程序实现登陆页面+登陆逻辑

    微信小程序实现登陆页面 实现上面两个页面 第一个页面 <view> <!-- 上侧部分 --> <view class="top-view"> ...

  5. 完整微信小程序授权登录页面教程

    完整微信小程序授权登录页面教程 1.前言 微信官方对getUserInfo接口做了修改,授权窗口无法直接弹出,而取而代之是需要创建一个button,将其open-type属性绑定getUseInfo方 ...

  6. 微信小程序引入ECharts组件

    首先打开ECharts网页 https://echarts.apache.org/zh/tutorial.html#%E5%9C%A8%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8 ...

  7. 微信小程序引入外部组件 iView Weapp

    iview Weapp组件的使用方法: 1. 下载小程序组件库 (前提是你已经有了项目目录) 你可以直接去github把iView Weapp的代码下载下来,也可以用过npm来下载. github地址 ...

  8. 微信小程序之实现页面缩放式侧滑效果

    效果图: 实现原理:点击按钮,往需要动画的div中添加或移除拥有动画效果的class. 由于微信小程序中不能操作page这个根节点,所以,只有用一个div(view)来模仿page根节点. 1.结构 ...

  9. 微信小程序——引入背景图片【六】

    前言 之前写了一些小程序的博文只是看文档边看边写,了解下他,这次可是真枪真刀的做了! 框架使用的是美团的mpvue,我也是一边学习,一边写的,如有错误之处,还望大家指出. 在这里我有个问题,为什么微信 ...

随机推荐

  1. Linux巩固记录(5) hadoop 2.7.4下自己编译代码并运行MapReduce程序

    程序代码为 ~\hadoop-2.7.4\share\hadoop\mapreduce\sources\hadoop-mapreduce-examples-2.7.4-sources\org\apac ...

  2. Upgrade Win10

    Internal deployment of Windows 10 Enterprise is currently underway as a phased deployment. Watch you ...

  3. CSP攻略

    看完三篇文章应该就懂了csp是干嘛的. https://www.cnblogs.com/Wayou/p/intro_to_content_security_policy.html https://ww ...

  4. 题解 P2146 【[NOI2015]软件包管理器】

    题目大意 ​ 给你一棵树, 求一点到根的路径上有多少个未标记点并全标记, 和询问一个点的子树内有多少已标记点和撤销标记 解题方法 1: install 操作 ​ 这个操作是求一点到根的路径上有多少个未 ...

  5. odoo开发笔记--模型字段compute用法

    compute属性,实现的主要功能是,前端界面选择某个字段的时候,指定与该字段关联的其他字段可以关联,并联动的显示. 可以和inverse属性同时使用,不加inverse属性的话,前端界面的显示效果只 ...

  6. 使用makefile

    最近在学习一个处理二维相场问题的c++程序,遇到了makefile文件,之前没有接触过,这里做一个简单的整理. 什么是makefile? 大多程序员使用的windows操作系统,IED都完成了make ...

  7. Installation failed with message...It is possible that this issue is resolved by uninstalling an existing version of the apk if it is present, and then re-installing.

    错误弹窗如图: Installation failed with message Failed to finalize session: INSTALL_FAILED_TEST_ONLY:instal ...

  8. 使用ffmpeg实现对h264视频解码 -- (实现了一个易于使用的c++封装库)

    H264是当今流行的视频压缩格式:ffmpeg是一个开源库,实现了对h264视频文件的解压缩. 为了降低使用ffmpeg的复杂性,尽量隐藏实现细节,我写了一个封装库.c#也可以很方便的使用此库.解压后 ...

  9. spring boot实现ssm(2)功能

    spring 和 mybatis 整合的那篇: ssm(2) . 配置文件比ssm(1) 更多, 在做项目的时候, 配置文件是一个让人头大的事情. 那么在spring boot中, 实现相同功能, 需 ...

  10. screen 实战后台命令执行备份

    一.安装 [root@vmware ~]# yum install -y screen 二.直接在命令行键入 screen 命令 [root@vmware ~]# screen 三.暂时终端会话 那么 ...