|
<p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px; line-height: 1.5; font-size: 18px;"><strong style="margin: 0px; padding: 0px;">1、不带参数</strong></span></p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">首先在pages文件夹中新建一个template文件夹,文件夹中新建一个template.wxml文件,代码如下</p><pre style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important;"><!--template.wxml--><template name="msgItem">
<view>
<text>This is template.wxml文件,我是一个模板</text>
</view></template></pre><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">然后我们书写我们所要调用template的页面index.wxml</p><pre style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important;"><!--index.wxml--><!-- 声明需要使用的模板文件 --><import src ="../template/template.wxml"/> <view>This is index.wxml</view><template is="msgItem"/></pre><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">注意:</p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">(1)index.wxml中template 标签的<strong style="margin: 0px; padding: 0px;">is属性</strong>与template.wxml中template 标签的<strong style="margin: 0px; padding: 0px;">name属性</strong>值<strong style="margin: 0px; padding: 0px;">相同</strong></p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">(2)index.wxml文件中要通过<strong style="margin: 0px; padding: 0px;">import标签</strong>声明需要使用的模板文件</p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">运行效果:</p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);"><img src="https://images2017.cnblogs.com/blog/1149706/201712/1149706-20171226163610385-1029414597.png" alt="" style="margin: 0px; padding: 0px; border: 0px; max-width: 900px; height: auto;"/></p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);"> </p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px; line-height: 1.5; font-size: 18px;"><strong style="margin: 0px; padding: 0px;">2、带参数</strong></span></p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">首先,修改template.wxml文件,我们给模板添加三个字段,修改后代码如下</p><p><span class="cnblogs_code_copy" style="margin: 0px; padding: 0px 5px 0px 0px; line-height: 1.5;"><a title="复制代码" style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="margin: 0px; padding: 0px; max-width: 900px; height: auto; border: none !important;"/></a></span></p><pre style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important;"><template name="msgItem">
<view>
<text>This is template.wxml文件,我是一个模板</text>
<view>
<text> {{index}}: {{msg1}} </text>
<text> {{msg2}} </text>
</view>
</view></template></pre><p><span class="cnblogs_code_copy" style="margin: 0px; padding: 0px 5px 0px 0px; line-height: 1.5;"><a title="复制代码" style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="margin: 0px; padding: 0px; max-width: 900px; height: auto; border: none !important;"/></a></span></p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">接下来我们在index.wxml中传递模板中所需要的三个参数,修改后的代码如下:</p><pre style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important;"><!--index.wxml--><!-- 声明需要使用的模板文件 --><import src ="../template/template.wxml"/><view>This is index.wxml</view><template is="msgItem" data="{{index:1,msg1:'msg1数据',msg2:'msg2数据'}}"/></pre><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">运行效果: </p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);"><img src="https://images2017.cnblogs.com/blog/1149706/201712/1149706-20171226163814823-254379739.png" alt="" style="margin: 0px; padding: 0px; border: 0px; max-width: 900px; height: auto;"/></p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);"> </p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px; line-height: 1.5; font-size: 18px;"><strong style="margin: 0px; padding: 0px;">3、列表item模板</strong></span></p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">接下来我们就通过一种常见的情况<strong style="margin: 0px; padding: 0px;">列表数据</strong>来使用模板,增加对模板的认知,直接上修改过的代码:</p><p><span class="cnblogs_code_copy" style="margin: 0px; padding: 0px 5px 0px 0px; line-height: 1.5;"><a title="复制代码" style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="margin: 0px; padding: 0px; max-width: 900px; height: auto; border: none !important;"/></a></span></p><pre style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important;">//index.jsPage({
data: {
list:[
{ name: '张三', age: 15 },
{ name: '李四', age: 25 },
{ name: '王五', age: 18 },
{ name: '赵六', age: 19 },
]
}
})</pre><p><span class="cnblogs_code_copy" style="margin: 0px; padding: 0px 5px 0px 0px; line-height: 1.5;"><a title="复制代码" style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="margin: 0px; padding: 0px; max-width: 900px; height: auto; border: none !important;"/></a></span></p><p><span class="cnblogs_code_copy" style="margin: 0px; padding: 0px 5px 0px 0px; line-height: 1.5;"><a title="复制代码" style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="margin: 0px; padding: 0px; max-width: 900px; height: auto; border: none !important;"/></a></span></p><pre style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important;"><!--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></pre><p><span class="cnblogs_code_copy" style="margin: 0px; padding: 0px 5px 0px 0px; line-height: 1.5;"><a title="复制代码" style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="margin: 0px; padding: 0px; max-width: 900px; height: auto; border: none !important;"/></a></span></p><p><span class="cnblogs_code_copy" style="margin: 0px; padding: 0px 5px 0px 0px; line-height: 1.5;"><a title="复制代码" style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="margin: 0px; padding: 0px; max-width: 900px; height: auto; border: none !important;"/></a></span></p><pre style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important;"><!--template.wxml--><template name="msgItem">
<view>
<text> name: {{name}} </text>
<text> age: {{age}}</text>
</view></template></pre><p><span class="cnblogs_code_copy" style="margin: 0px; padding: 0px 5px 0px 0px; line-height: 1.5;"><a title="复制代码" style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="margin: 0px; padding: 0px; max-width: 900px; height: auto; border: none !important;"/></a></span></p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">运行效果: </p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);"><img src="https://images2017.cnblogs.com/blog/1149706/201712/1149706-20171226163957495-732921833.png" alt="" style="margin: 0px; padding: 0px; border: 0px; max-width: 900px; height: auto;"/></p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);"> </p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px; line-height: 1.5; font-size: 18px;"><strong style="margin: 0px; padding: 0px;">4、使用模板样式</strong></span></p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">接下来我们就给模板增加样式文件,然后在需要调用模板的地方使用该样式。 </p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">首先在pages/template文件夹中新建一个template.wxss文件,然后对模板文件,添加一个简单样式。</p><p><span class="cnblogs_code_copy" style="margin: 0px; padding: 0px 5px 0px 0px; line-height: 1.5;"><a title="复制代码" style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="margin: 0px; padding: 0px; max-width: 900px; height: auto; border: none !important;"/></a></span></p><pre style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important;"><!--template.wxml--><template name="msgItem">
<view class="template_style">
<text> name: {{name}} </text>
<text class="template_age_style"> age: {{age}}</text>
</view></template></pre><p><span class="cnblogs_code_copy" style="margin: 0px; padding: 0px 5px 0px 0px; line-height: 1.5;"><a title="复制代码" style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="margin: 0px; padding: 0px; max-width: 900px; height: auto; border: none !important;"/></a></span></p><p><span class="cnblogs_code_copy" style="margin: 0px; padding: 0px 5px 0px 0px; line-height: 1.5;"><a title="复制代码" style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="margin: 0px; padding: 0px; max-width: 900px; height: auto; border: none !important;"/></a></span></p><pre style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important;">/* 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;
}</pre><p><span class="cnblogs_code_copy" style="margin: 0px; padding: 0px 5px 0px 0px; line-height: 1.5;"><a title="复制代码" style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="margin: 0px; padding: 0px; max-width: 900px; height: auto; border: none !important;"/></a></span></p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">我们的样式文件已经创建好了,接下来在index.wxml中引入该样式文件。</p><pre style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important;">/**index.wxss**/
@import "../template/template.wxss";</pre><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">运行效果: </p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);"><img src="https://images2017.cnblogs.com/blog/1149706/201712/1149706-20171226164218120-1774534762.png" alt="" style="margin: 0px; padding: 0px; border: 0px; max-width: 900px; height: auto;"/></p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);"><span style="margin: 0px; padding: 0px; line-height: 1.5; font-size: 18px;"><strong style="margin: 0px; padding: 0px;">5、调用不同的模板</strong></span></p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">有时候,我们有这样的需求,那就是同一个列表中,item数据不同,可能他的样式也是有很大的区别,所以我们使用的模板也会对应不相同,接下来我们就来实现这样需求的小Demo: <br style="margin: 0px; padding: 0px;"/>首先修改了一下template.wxml,原本该文件中只有一个template,现在我们创建了两个,新增的template仅仅多了一行代码,当然了实际开发中,需求会比这个难很多,在这里只是为了实现Demo。</p><p><span class="cnblogs_code_copy" style="margin: 0px; padding: 0px 5px 0px 0px; line-height: 1.5;"><a title="复制代码" style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="margin: 0px; padding: 0px; max-width: 900px; height: auto; border: none !important;"/></a></span></p><pre style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important;"><!--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></pre><p><span class="cnblogs_code_copy" style="margin: 0px; padding: 0px 5px 0px 0px; line-height: 1.5;"><a title="复制代码" style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="margin: 0px; padding: 0px; max-width: 900px; height: auto; border: none !important;"/></a></span></p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">接下来我们在index.wxml中通过age字段调用不同的模板:</p><p><span class="cnblogs_code_copy" style="margin: 0px; padding: 0px 5px 0px 0px; line-height: 1.5;"><a title="复制代码" style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="margin: 0px; padding: 0px; max-width: 900px; height: auto; border: none !important;"/></a></span></p><pre style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important;"><!--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></pre><p><span class="cnblogs_code_copy" style="margin: 0px; padding: 0px 5px 0px 0px; line-height: 1.5;"><a title="复制代码" style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="margin: 0px; padding: 0px; max-width: 900px; height: auto; border: none !important;"/></a></span></p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">运行效果: </p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, "ms song", 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);"><img src="https://images2017.cnblogs.com/blog/1149706/201712/1149706-20171226164349635-769072068.png" alt="" style="margin: 0px; padding: 0px; border: 0px; max-width: 900px; height: auto;"/></p><p><br/></p> |
|