2 回答

TA貢獻(xiàn)1752條經(jīng)驗(yàn) 獲得超4個(gè)贊
要渲染視圖,您只需在expressjs中指定一個(gè)端點(diǎn)并僅調(diào)用該端點(diǎn)
您不應(yīng)該嘗試調(diào)用.ejs文件位置
示例:在這段代碼中l(wèi)ogin.ejs會(huì)自動(dòng)在后臺(tái)渲染,服務(wù)器庫會(huì)做這件事,res.render('login')會(huì)自動(dòng)調(diào)用login.ejs
router.get('/home', function(req, res, next) {
res.render('login');
});
您的超鏈接應(yīng)該單獨(dú)調(diào)用路由器映射端點(diǎn)
<a href="/home"><li> LOGIN </li></a>
一旦你決定使用EJS,出于UI設(shè)計(jì)的目的,如果單獨(dú)給你UI設(shè)計(jì)部分,那么你可以做一個(gè)虛擬代碼來渲染一個(gè)帶有固定數(shù)據(jù)的EJS,你不必用DB運(yùn)行整個(gè)網(wǎng)站
例如:假設(shè)有一個(gè)數(shù)據(jù)將渲染 EJS,要使用虛擬數(shù)據(jù)渲染,您可以這樣做
從正在盡自己職責(zé)的開發(fā)人員那里獲取數(shù)據(jù)并執(zhí)行此代碼
router.get('/home', function(req, res, next) {
var data = {"name": "Emmanuel"}
res.render('login', data);
});
正如你所看到的,數(shù)據(jù)是直接編碼的temporary

TA貢獻(xiàn)1776條經(jīng)驗(yàn) 獲得超12個(gè)贊
這是一個(gè)相當(dāng)老的問題,但我在為類似問題尋找自己的解決方案時(shí)發(fā)現(xiàn)了OPs帖子。
// this link should take me to the 'new post' page which is rendered only from an ejs template (no HTML) and has a matching endpoint on my express router obj as 'new'
<a href="new" class="new-post-link">new post</a>
// clicking this link would move the user to the /new endpoint and would render the 'new post' page upon clicking the link
<a href="new" class="new-post-link">new post</a>
// for OP's specific problem:
// original
<button id="loginBtn">
<a href="views/login.ejs"><LI> LOGIN </LI></a>
</button>
// solution
<button id="loginBtn">
<a href="login"><LI> LOGIN </LI></a>
</button>
我希望它能幫助某人。
- 2 回答
- 0 關(guān)注
- 159 瀏覽
添加回答
舉報(bào)