【问题标题】:Using different stylesheets in express/ node/ ejs application - style not defined在 express/node/ejs 应用程序中使用不同的样式表 - 未定义样式
【发布时间】:2021-09-01 09:48:54
【问题描述】:

我想在我的 express/node/ejs 项目中使用不同的样式表。 我想渲染具有特定样式的视图,所以在 app.js 中我这样做:

app.get('/mySite', (req, res) => {
res.render('mySite', { style: mySite });

假设我创建了这个 mySite.css 并在样板.ejs 中创建了样式链接:

<link rel="stylesheet" type="text/css" href="/stylesheets/<%=style%>.css">

我应该如何定义'style'才不会得到错误“style is not defined”?

对不起,如果这是我应该具备的基本表达知识,但我仍在学习

【问题讨论】:

    标签: css node.js express ejs


    【解决方案1】:

    我也在学习,通过在我的 controller.js 文件中定义样式表名称的对象,同样的方法对我很有效。

    const styles = {
      index: "styles",
      register: "register",
      login: "login",
    };
    

    然后只是在每个页面的渲染中添加了style属性:

    const mainController = {
      index: (req, res) => {
        res.render("./products/index", {
          showRoom: productCollection,
          style: styles.index,
        });
      },
      register: (req, res) => {
        res.render("./users/register", {
          style: styles.register,
        });
      },
      login: (req, res) => {
        res.render("./users/login", {
          style: styles.login,
        });
      },
    };
    

    这样,每当索引、寄存器等被渲染时,它都会调用样式表的名称来完成 HTML 中的 href:

      <!-- APPLIES EACH SPECIFIC STYLESHEET TO EACH SITE -->
      <link rel="stylesheet" href="/styles/<%= style %>.css" />
      <!-- ------------------------------------------------- -->
    

    【讨论】:

      【解决方案2】:

      好的,我了解到可以通过在路由中适当引用样式来完成,而在样板中必须有一个用于样式引用的 ejs 语法

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-23
        • 2017-07-01
        • 2015-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多