微信小程序?qū)崿F(xiàn)給嵌套template模板傳遞數(shù)據(jù)的方式總結(jié)
本文實例總結(jié)了微信小程序?qū)崿F(xiàn)給嵌套template模板傳遞數(shù)據(jù)的方式。分享給大家供大家參考,具體如下:
一、template模板調(diào)用的數(shù)據(jù)是單一形態(tài)時:
indexTemplate模板:
<import src="../lookAndCollect-template/lookAndCollect-template.wxml" /> <template name="indexTemplate"> <view class="user-info"> <image class="avatar" src="{{avatar}}"></image> <text class="name">{{name}}</text> <text class="date">{{date}}</text> </view> <view class="news"> <text class="news-title">{{title}}</text> <image class="news-img" src="{{newsImg}}"></image> <text class="news-content">{{content}}</text> </view> <template is="reviewAndCollect" data="{{review,look}}"></template> </template>
lookAndCollect模板:
<template name="lookAndCollect-template"> <view class="lookAndCollect-template"> <view class="lookAndCollect-template-review"> <image src="/smallApp/images/icon/view.png"></image> <text>{{look}}</text> </view> <view class="lookAndCollect-template-look"> <image src="/smallApp/images/icon/chat.png"></image> <text>{{collect}}</text> </view> </view> </template>
indexTemplate模板在index.wxml中的引用:
<block wx:for="{{newsData}}" wx:for-item="newsItem"> <view class="item"> <template is="indexTemplate" data="{{...newsItem}}" /> </view> </block>
index.wxml對應(yīng)的index.js寫法:
var newsDataList = require("../index-data.js"); Page({ data: { }, onLoad: function (option) { this.setData({ newsData: newsDataList.dataList }); } })
模板中使用單一形式的數(shù)據(jù):
var news_data = [ { listId: "0", avatar: "/smallApp/images/avatar/1.png", name: "我是大貓貓", date: "16分鐘前", title: "搞事情?法國招聘新特工 會漢語成必備條件", newsImg: "/smallApp/images/post/crab.png", content: "是的,你沒看錯,據(jù)法國《費加羅報》報道,法國境外安全總局(DGSE)欲在2019年前招募600名新特工,而且新的特工必須年輕、有高等文憑,會多國語言,并且熟悉電腦與互聯(lián)網(wǎng)。", review: "0", look: "30" }, { listId: "1", avatar: "/smallApp/images/avatar/2.png", name: "風(fēng)口上的豬", date: "1天前", title: "順豐控股上市次日盤中漲停 離首富差4個漲停", newsImg: "/smallApp/images/post/bl.png", content: "根據(jù)之前借殼方鼎泰新材發(fā)布的公告,該公司定增完成后,第一大股東將變更為深圳明德控股發(fā)展有限公司(簡稱“明德控股”),持股比例為64.58%,后4名分別為寧波順達豐潤投資管理合伙企業(yè)(有限合伙)…", review: "100", look: "380" } ]; module.exports = { dataList: news_data }
如果需要在嵌套的模板中傳入多個數(shù)據(jù),可以將每個數(shù)據(jù)用逗號隔開。
二、嵌套模板調(diào)用包括object對象時的調(diào)用方法:
模板中使用的數(shù)據(jù)review和look以對象的形式呈現(xiàn)時:
var news_data = [ { listId: "0", avatar: "/smallApp/images/avatar/1.png", name: "我是大貓貓", date: "16分鐘前", title: "搞事情?法國招聘新特工 會漢語成必備條件", newsImg: "/smallApp/images/post/crab.png", content: "是的,你沒看錯,據(jù)法國《費加羅報》報道,法國境外安全總局(DGSE)欲在2019年前招募600名新特工,而且新的特工必須年輕、有高等文憑,會多國語言,并且熟悉電腦與互聯(lián)網(wǎng)。", reviewAndCollect { review: "0", look: "30" } }, { listId: "1", avatar: "/smallApp/images/avatar/2.png", name: "風(fēng)口上的豬", date: "1天前", title: "順豐控股上市次日盤中漲停 離首富差4個漲停", newsImg: "/smallApp/images/post/bl.png", content: "根據(jù)之前借殼方鼎泰新材發(fā)布的公告,該公司定增完成后,第一大股東將變更為深圳明德控股發(fā)展有限公司(簡稱“明德控股”),持股比例為64.58%,后4名分別為寧波順達豐潤投資管理合伙企業(yè)(有限合伙)…", reviewAndCollect { review: "120", look: "300" } } ]; module.exports = { dataList: news_data }
indexTemplate模板
<import src="../lookAndCollect-template/lookAndCollect-template.wxml" /> <template name="indexTemplate"> <view class="user-info"> <image class="avatar" src="{{avatar}}"></image> <text class="name">{{name}}</text> <text class="date">{{date}}</text> </view> <view class="news"> <text class="news-title">{{title}}</text> <image class="news-img" src="{{newsImg}}"></image> <text class="news-content">{{content}}</text> </view> <template is="reviewAndCollect" data="{{reviewAndCollect}}"></template> </template>
lookAndCollect模板:
<template name="lookAndCollect-template"> <view class="lookAndCollect-template"> <view class="lookAndCollect-template-review"> <image src="/smallApp/images/icon/view.png"></image> <text>{{reviewAndCollect.look}}</text> </view> <view class="lookAndCollect-template-look"> <image src="/smallApp/images/icon/chat.png"></image> <text>{{reviewAndCollect.collect}}</text> </view> </view> </template>
ps: indexTemplate模板在index.wxml中的引用,以及index.wxml對應(yīng)的index.js的寫法,同第一種。
希望本文所述對大家微信小程序開發(fā)有所幫助。
相關(guān)文章
ToolTip 通過Js實現(xiàn)代替超鏈接中的title效果
ToolTip 通過Js實現(xiàn)代替超鏈接中的title效果,需要的朋友可以參考下。2011-04-04javascript用函數(shù)實現(xiàn)對象的方法
這篇文章主要介紹了javascript用函數(shù)實現(xiàn)對象的方法,涉及javascript函數(shù)使用技巧,需要的朋友可以參考下2015-05-05JS+HTML實現(xiàn)自定義上傳圖片按鈕并顯示圖片功能的方法分析
這篇文章主要介紹了JS+HTML實現(xiàn)自定義上傳圖片按鈕并顯示圖片功能的方法,結(jié)合實例形式分析了JavaScript圖片上傳、編碼轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2020-02-02利用JS實現(xiàn)獲取當(dāng)前系統(tǒng)電量情況
在前端瀏覽器中我們可以通過使用JavaScript的navigator.getBattery()方法來獲取當(dāng)前系統(tǒng)的電池情況,本文將介紹如何使用這個API以及它在實際應(yīng)用中的使用,需要的可以參考下2023-12-12js canvas實現(xiàn)適用于移動端的百分比儀表盤dashboard
這篇文章主要為大家詳細介紹了js canvas實現(xiàn)適用于移動端的百分比儀表盤dashboard,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07