微信小程序 本地數(shù)據(jù)讀取實例
微信小程序 本地數(shù)據(jù)讀取實例
一般情況下,小程序的utils這個文件夾下,我們可以把本地的數(shù)據(jù)寫進(jìn)去,封裝成.js文件,提供對外暴露的接口,然后讀取本地數(shù)據(jù)。如果涉及到一些針對這些數(shù)據(jù)的處理方法,也可以把方法寫好,封裝到.js文件里面,然后需要時調(diào)用。
module.exports = {
mtData: mtData,
searchmtdata: searchmtdata,
usedraw: usedraw
}
var mt_data = mtData()
function searchmtdata(id) {
var result
for (let i = 0; i < mt_data.list.length; i++) {
var mt = mt_data.list[i]
if (mt.id == id) {
result = mt
}
}
return result || {}
}
function mtData() {
var arr = {
list: [
{
id: '1',
MTId: 'MT001',
status: 'working',
Duration: 3,
Operator: 'tom',
IdleReason: 'lunch'
}, {
id: '2',
MTId: 'MT002',
status: 'Idle',
Duration: 7,
Operator: 'jerry',
IdleReason: 'reparied'
}, {
id: '3',
MTId: 'MT003',
status: 'Idle',
Duration: 6,
Operator: 'tom',
IdleReason: 'lunch'
}, {
id: '4',
MTId: 'MT004',
status: 'working',
Duration: 9,
Operator: 'jerry',
IdleReason: 'reparied'
}, {
id: '5',
MTId: 'MT005',
status: 'Idle',
Duration: 2,
Operator: 'tom',
IdleReason: 'lunch'
}, {
id: '6',
MTId: 'MT006',
status: 'working',
Duration: 6,
Operator: 'jerry',
IdleReason: 'reparied'
}, {
id: '7',
MTId: 'MT007',
status: 'Idle',
Duration: 1,
Operator: 'tom',
IdleReason: 'lunch'
}
]
}
return arr
}
如上面的代碼片段所示,function mtData寫好的本地數(shù)據(jù),其中包含對象List。另外還有一個function searchmtdata,這個方法是根據(jù)傳參的不同加載具體的數(shù)據(jù),接下來會用到,方法和數(shù)據(jù)都寫好后,需要寫一個對外暴露的接口:
module.exports = {
mtData: mtData,
searchmtdata: searchmtdata,
usedraw: usedraw
}
在其他頁面調(diào)用data文件(一般是在js文件里面)
var fileData = require('../../utils/data.js')
Page({
data: {
showData: fileData.mtData().list,
},
})
像上面的示例一樣,在data{}中賦值后,showData就可以直接在wxml中顯示了。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- 微信小程序本地緩存數(shù)據(jù)增刪改查實例詳解
- 微信小程序-詳解數(shù)據(jù)緩存
- 微信小程序之?dāng)?shù)據(jù)緩存的實例詳解
- 微信小程序開發(fā)之?dāng)?shù)據(jù)存儲 參數(shù)傳遞 數(shù)據(jù)緩存
- 微信小程序 本地數(shù)據(jù)存儲實例詳解
- 微信小程序 緩存(本地緩存、異步緩存、同步緩存)詳解
- 微信小程序基于本地緩存實現(xiàn)點贊功能的方法
- IOS中微信小程序播放緩存的音頻文件的方法
- 微信小程序?qū)崿F(xiàn)緩存根據(jù)不同的id來進(jìn)行設(shè)置和讀取緩存
- 微信小程序?qū)W習(xí)筆記之本地數(shù)據(jù)緩存功能詳解
相關(guān)文章
純js實現(xiàn)高度可擴展關(guān)鍵詞高亮方案詳解
這篇文章主要為大家介紹了純js實現(xiàn)高度可擴展關(guān)鍵詞高亮方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08

