這篇文章主要介紹在koa2中如何實(shí)現(xiàn)頁面渲染的全局?jǐn)?shù)據(jù),文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)主營麻江網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都App定制開發(fā),麻江h(huán)5微信平臺(tái)小程序開發(fā)搭建,麻江網(wǎng)站營銷推廣歡迎麻江等地區(qū)企業(yè)咨詢
該項(xiàng)目主要用的是傳統(tǒng)的服務(wù)端渲染的方式,所以會(huì)用 koa-views 去做頁面的渲染工作。實(shí)現(xiàn)方式就是 ctx.render('path',data),那么,有如下場景,每個(gè)頁面都需要去驗(yàn)證是否登錄,登錄了要返回頁面?zhèn)€人數(shù)據(jù),這個(gè)情況,怎么辦呢?我不想每次都去手動(dòng)的加入個(gè)人數(shù)據(jù)啊。例如這樣:
let data = {
"user":"12",
"questionList":questionList
};
await ctx.render("index",data);此處的user就是每個(gè)頁面都是要返回的數(shù)據(jù)。
很顯然,每個(gè)頁面都要獲得的數(shù)據(jù),用中間件去獲取,類似java的攔截器,過濾器之類的了。
import {UserClient} from "../manager/user/UserClient";
export const signInStatusMiddleware = async (ctx:any,next:any)=>{
console.log("signInStatusMiddleware")
let accessToken = ctx.cookies.get("ACCESS-TOKEN");
if(accessToken){
let userClient :UserClient = new UserClient;
let user = await userClient.getUserByToken(accessToken);
}
await next();
}OK,中間件中已經(jīng)拿到了用戶數(shù)據(jù)了,那么,問題來了。數(shù)據(jù)是可以拿,怎么放呢?
找到koa-views 源碼。有如下代碼:
return function views(ctx, next) {
if (ctx.render) return next()
ctx.render = function(relPath, locals = {}) {
return getPaths(path, relPath, extension).then(paths => {
const suffix = paths.ext
const state = Object.assign(locals, options, ctx.state || {})
// deep copy partials
state.partials = Object.assign({}, options.partials || {})
debug('render `%s` with %j', paths.rel, state)
ctx.type = 'text/html'
if (isHtml(suffix) && !map) {
return send(ctx, paths.rel, {
root: path
})
} else {
const engineName = map && map[suffix] ? map[suffix] : suffix
const render = engineSource[engineName]
if (!engineName || !render)
return Promise.reject(
new Error(`Engine not found for the ".${suffix}" file extension`)
)
return render(resolve(path, paths.rel), state).then(html => {
ctx.body = html
})
}
})
}
return next()
}關(guān)鍵是這一段
const state = Object.assign(locals, options, ctx.state || {})很顯然,state 是將傳入的數(shù)據(jù),合并了,中間件配置的options ,和ctx.state的。中間件顯式配置顯然部合適,所以,做法是,在攔截器中間件中,把user賦值給ctx.state.
export const signInStatusMiddleware = async (ctx:any,next:any)=>{
console.log("signInStatusMiddleware")
let accessToken = ctx.cookies.get("ACCESS-TOKEN");
if(accessToken){
let userClient :UserClient = new UserClient;
let user = await userClient.getUserByToken(accessToken);
ctx.state = Object.assign(ctx.state,{"user":user});
}
await next();
}ok。這樣一來,在頁面渲染的時(shí)候,就會(huì)帶上用戶信息了。而不需要再在各處去自己手動(dòng)添加。
以上是“在koa2中如何實(shí)現(xiàn)頁面渲染的全局?jǐn)?shù)據(jù)”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
文章題目:在koa2中如何實(shí)現(xiàn)頁面渲染的全局?jǐn)?shù)據(jù)
文章路徑:http://chinadenli.net/article8/ihppop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站維護(hù)、網(wǎng)站收錄、建站公司、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)