2 回答

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

TA貢獻1776條經驗 獲得超12個贊
這是一個相當老的問題,但我在為類似問題尋找自己的解決方案時發現了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 關注
- 162 瀏覽
添加回答
舉報