普通模版

在微信中使用模版temalate的话, 给模版自定义名称即可

  1. <template name="moban2">
  2. <view>
  3. <text>{{username}}</text>
  4. <text>{{age}}</text>
  5. </view>
  6. </template>

调用使用 is=”模版的名称” 并绑定数据 … 代表解构的意思

  1. <template is="moban2" data="{{...shuju}}"></template>

Js文件中的 data:{ }

  1. shuju:{
  2. username:"xiaohong",age:18,type:"帅",
  3. },

高级模版 单文件

可专门建立文件夹专门放置模版 wxss样式 wxml视图 都可以拆分成单独的文件,然后进行导入导出 引用等

wxml 视图

在别的文件夹下书写了模版, 在调用的时候首先要引入进来

模版层:

  1. <template name= 'meagss'>
  2. <text>{{title }} </text>
  3. <text>{{content }} </text>
  4. </templast>

引用模版

  1. <templast is=”meagss” data=”{{...shuju}}”></templast>

image.png

动态的传入数据

可以动态的传入数据到模版中. 在引入的这里传 使用data=”{{ 参:””,参2:”” }}”

注意引入外部文件的模版需要加后叠 wxml

  1. <template is="navMoban" data="{{title:'我是index2的标题',content:'我是index2的内容'}}"></template>
  2. <template is="navMoban" data="{{title:'我是index3的标题',content:'我是index3的内容'}}"></template>

image.png

服务器数据data进行渲染

可用服务器返回来的数据data进行渲染 注意数据要数组的形式, 才能for渲染

  1. <template is="navMoban" wx:for="{{message}}" data="{{...item}}" wx:key="index"></template>
  1. data: {
  2. message:[
  3. {
  4. title:"周杰伦",
  5. content:"我们一起去唱歌吧"
  6. },
  7. {
  8. title:"老陈",
  9. content:"好好学习 "
  10. }
  11. ]
  12. },

image.pngimage.png

include快捷引入

在引入非常多的时候,使用
注意: 这个引入要使用 include 标签 并引入的文件需要带后缀 wxml , 然后就可以正常使用了,自动渲染到本页面

模版

  1. <view>
  2. <text>我是 include文件的</text>
  3. </view>

image.png

引入

使用include 标签 src=”路径” 引入,包含后叠名

  1. <include src="include01.wxml" />

image.png

整体效果

image.png