立即注册
 找回密码
 立即注册

梦幻岛

搜索
热搜: 活动 交友
返回列表
查看: 434|回复: 0

微信小程序 引入公共页面的几种情况

[复制链接]

35

主题

35

帖子

340

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
340
发表于 2019-6-19 09:40:57 | 显示全部楼层 |阅读模式
<p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, &quot;ms song&quot;, 宋体, 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, &quot;ms song&quot;, 宋体, 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: &quot;Courier New&quot; !important;">&lt;!--template.wxml--&gt;&lt;template&nbsp;name=&quot;msgItem&quot;&gt;
&nbsp;&nbsp;&lt;view&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;text&gt;This&nbsp;is&nbsp;template.wxml文件,我是一个模板&lt;/text&gt;
&nbsp;&nbsp;&lt;/view&gt;&lt;/template&gt;</pre><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, &quot;ms song&quot;, 宋体, 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: &quot;Courier New&quot; !important;">&lt;!--index.wxml--&gt;&lt;!--&nbsp;声明需要使用的模板文件&nbsp;--&gt;&lt;import&nbsp;src&nbsp;=&quot;../template/template.wxml&quot;/&gt;  &lt;view&gt;This&nbsp;is&nbsp;index.wxml&lt;/view&gt;&lt;template&nbsp;is=&quot;msgItem&quot;/&gt;</pre><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, &quot;ms song&quot;, 宋体, 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, &quot;ms song&quot;, 宋体, 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, &quot;ms song&quot;, 宋体, 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, &quot;ms song&quot;, 宋体, 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, &quot;ms song&quot;, 宋体, 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, &quot;ms song&quot;, 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">&nbsp;</p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, &quot;ms song&quot;, 宋体, 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, &quot;ms song&quot;, 宋体, 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: &quot;Courier New&quot; !important;">&lt;template&nbsp;name=&quot;msgItem&quot;&gt;
&nbsp;&nbsp;&lt;view&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;text&gt;This&nbsp;is&nbsp;template.wxml文件,我是一个模板&lt;/text&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;view&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;text&gt;&nbsp;{{index}}:&nbsp;{{msg1}}&nbsp;&lt;/text&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;text&gt;&nbsp;{{msg2}}&nbsp;&lt;/text&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/view&gt;
&nbsp;&nbsp;&lt;/view&gt;&lt;/template&gt;</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, &quot;ms song&quot;, 宋体, 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: &quot;Courier New&quot; !important;">&lt;!--index.wxml--&gt;&lt;!--&nbsp;声明需要使用的模板文件&nbsp;--&gt;&lt;import&nbsp;src&nbsp;=&quot;../template/template.wxml&quot;/&gt;&lt;view&gt;This&nbsp;is&nbsp;index.wxml&lt;/view&gt;&lt;template&nbsp;is=&quot;msgItem&quot;&nbsp;data=&quot;{{index:1,msg1:&#39;msg1数据&#39;,msg2:&#39;msg2数据&#39;}}&quot;/&gt;</pre><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, &quot;ms song&quot;, 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">运行效果:&nbsp;</p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, &quot;ms song&quot;, 宋体, 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, &quot;ms song&quot;, 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">&nbsp;</p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, &quot;ms song&quot;, 宋体, 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, &quot;ms song&quot;, 宋体, 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: &quot;Courier New&quot; !important;">//index.jsPage({
&nbsp;&nbsp;data:&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;list:[
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;name:&nbsp;&#39;张三&#39;,&nbsp;age:&nbsp;15&nbsp;},
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;name:&nbsp;&#39;李四&#39;,&nbsp;age:&nbsp;25&nbsp;},
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;name:&nbsp;&#39;王五&#39;,&nbsp;age:&nbsp;18&nbsp;},
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;name:&nbsp;&#39;赵六&#39;,&nbsp;age:&nbsp;19&nbsp;},
&nbsp;&nbsp;&nbsp;&nbsp;]
&nbsp;&nbsp;}
})</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: &quot;Courier New&quot; !important;">&lt;!--index.wxml--&gt;&lt;!--&nbsp;声明需要使用的模板文件&nbsp;--&gt;&lt;import&nbsp;src&nbsp;=&quot;../template/template.wxml&quot;/&gt;&lt;view&gt;This&nbsp;is&nbsp;index.wxml&lt;/view&gt;&lt;view&nbsp;wx:for=&quot;{{list}}&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;template&nbsp;is=&quot;msgItem&quot;&nbsp;data=&quot;{{name:item.name,age:item.age}}&quot;/&gt;&lt;/view&gt;</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: &quot;Courier New&quot; !important;">&lt;!--template.wxml--&gt;&lt;template&nbsp;name=&quot;msgItem&quot;&gt;
&nbsp;&nbsp;&lt;view&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;text&gt;&nbsp;name:&nbsp;{{name}}&nbsp;&lt;/text&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;text&gt;&nbsp;age:&nbsp;{{age}}&lt;/text&gt;
&nbsp;&nbsp;&lt;/view&gt;&lt;/template&gt;</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, &quot;ms song&quot;, 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">运行效果:&nbsp;</p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, &quot;ms song&quot;, 宋体, 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, &quot;ms song&quot;, 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">&nbsp;</p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, &quot;ms song&quot;, 宋体, 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, &quot;ms song&quot;, 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">接下来我们就给模板增加样式文件,然后在需要调用模板的地方使用该样式。&nbsp;</p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, &quot;ms song&quot;, 宋体, 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: &quot;Courier New&quot; !important;">&lt;!--template.wxml--&gt;&lt;template&nbsp;name=&quot;msgItem&quot;&gt;
&nbsp;&nbsp;&lt;view&nbsp;class=&quot;template_style&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;text&gt;&nbsp;name:&nbsp;{{name}}&nbsp;&lt;/text&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;text&nbsp;class=&quot;template_age_style&quot;&gt;&nbsp;age:&nbsp;{{age}}&lt;/text&gt;
&nbsp;&nbsp;&lt;/view&gt;&lt;/template&gt;</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: &quot;Courier New&quot; !important;">/*&nbsp;pages/template/template.wxss&nbsp;*/.template_style{
&nbsp;&nbsp;border-bottom:&nbsp;solid&nbsp;2px&nbsp;#999999;
&nbsp;&nbsp;padding:&nbsp;5px;
&nbsp;&nbsp;font-size:&nbsp;18px;
&nbsp;&nbsp;color:&nbsp;#000000;

}.template_age_style{
&nbsp;&nbsp;color:&nbsp;#666666;
&nbsp;&nbsp;font-size:&nbsp;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, &quot;ms song&quot;, 宋体, 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: &quot;Courier New&quot; !important;">/**index.wxss**/
@import&nbsp;&quot;../template/template.wxss&quot;;</pre><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, &quot;ms song&quot;, 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">运行效果:&nbsp;</p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, &quot;ms song&quot;, 宋体, 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, &quot;ms song&quot;, 宋体, 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, &quot;ms song&quot;, 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">有时候,我们有这样的需求,那就是同一个列表中,item数据不同,可能他的样式也是有很大的区别,所以我们使用的模板也会对应不相同,接下来我们就来实现这样需求的小Demo:&nbsp;<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: &quot;Courier New&quot; !important;">&lt;!--template.wxml--&gt;&lt;template&nbsp;name=&quot;msgItem&quot;&gt;
&nbsp;&nbsp;&lt;view&nbsp;class=&quot;template_style&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;text&gt;&nbsp;name:&nbsp;{{name}}&nbsp;&lt;/text&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;text&nbsp;class=&quot;template_age_style&quot;&gt;&nbsp;age:&nbsp;{{age}}&lt;/text&gt;
&nbsp;&nbsp;&lt;/view&gt;&lt;/template&gt;&lt;template&nbsp;name=&quot;msgItem2&quot;&gt;
&nbsp;&nbsp;&lt;view&nbsp;class=&quot;template_style&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;text&gt;&nbsp;name:&nbsp;{{name}}&nbsp;&lt;/text&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;text&nbsp;class=&quot;template_age_style&quot;&gt;&nbsp;age:&nbsp;{{age}}&lt;/text&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;text&gt;我是一个未成年&lt;/text&gt;&gt;&nbsp;&nbsp;&lt;/view&gt;&lt;/template&gt;</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, &quot;ms song&quot;, 宋体, 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: &quot;Courier New&quot; !important;">&lt;!--index.wxml--&gt;&lt;!--&nbsp;声明需要使用的模板文件&nbsp;--&gt;&lt;import&nbsp;src&nbsp;=&quot;../template/template.wxml&quot;/&gt;&lt;view&gt;This&nbsp;is&nbsp;index.wxml&lt;/view&gt;&lt;view&nbsp;wx:for=&quot;{{list}}&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;template&nbsp;is=&quot;{{item.age&nbsp;&gt;=&nbsp;18&nbsp;?&nbsp;&#39;msgItem&#39;&nbsp;:&nbsp;&#39;msgItem2&#39;}}&quot;&nbsp;data=&quot;{{name:item.name,age:item.age}}&quot;/&gt;&lt;/view&gt;</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, &quot;ms song&quot;, 宋体, Arial, 微软雅黑, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">运行效果:&nbsp;</p><p style="margin: 10px auto; padding: 0px; line-height: 1.5; font-size: 13px; font-family: verdana, &quot;ms song&quot;, 宋体, 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>
回复

使用道具 举报

返回列表
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表