express-handlebars模板不在静态路由上加载CSS样式

问题描述 投票:0回答:1

我有一组不同的路线都使用相同的模板main,但任何路线只有一个/远离基线路线应用CSS但超出该范围的任何东西将不包括它。

所以app.get('/profile)将使用CSS渲染,但app.get('/profile/edit')不会。

我不确定为什么这不起作用,据我所知,这条线应该适用于它下面设置的所有路线。

在server.js中

  app.use(express.static(__dirname + "/public"));
  //Many different routes
  //...
  //...

    //renders with CSS successfully
    app.get("/profile", (req,res) => {
      res.render('profile', {
        layout: "main",
        cause: {
          title: title
        },
        csrfToken: req.csrfToken
      });
    });

 //this is the route that renders without CSS
    app.get("/profile/edit", (req,res) => {
      res.render('edit', {
        navItems: [
          {name: 'See your fellow supporters',
          link: "/signatures"}
        ],
        layout: "main",
        cause: '',
        csrfToken: req.csrfToken
      });
    });

把手模板

   //edit.handlebars
    <h2>Change your details</h2>
    <form method="POST">
    <input type="text" name="firstName" placeholder="first name">
    <label for="firstName">First Name</label>

    <input type="text" name="lastName" placeholder="last name">
    <label for="lastName">Last Name</label>

    <input type="email" name="email" placeholder="e-mail">
    <label for="email">E-mail</label>

    <input type="password" name="password" placeholder="password">
    <label for="password">Password</label>

    <input type="text" name="age" placeholder="age">
    <label for="age">Age</label>

    <input type="text" name="city" placeholder="city/town">
    <label for="city">City or Town</label>

    <input type="text" name="homepage" placeholder="homepage or social media">
    <label for="homepage">Homepage</label>

    <input type="hidden" name="_csrf" value="{{csrfToken}}">
    <input  id="formButton" type="submit" name="submit" value="Update">
  </form>


//profile.handlebars
          <h2>Let people know that you support: {{>cause}}</h2>
      <form method="POST">
        <input type="text" name="age" placeholder="age">
        <input type="text" name="city" placeholder="city/town">
        <input type="text" name="homepage" placeholder="homepage or social media">
        <input type="hidden" name="_csrf" value="{{csrfToken}}"
        <input  id="formButton" type="submit" name="submit" value="continue">
      </form>
node.js express express-handlebars
1个回答
0
投票

server.js你可以使用:

app.engine('hbs', expressHandlebars({
 defaultLayout: 'main',
 extname: '.hbs',
 layoutsDir: path.join(__dirname, 'layouts')
}))

那将需要你在main.hbs文件夹中有你的layouts

这应该可以解决你的问题。

© www.soinside.com 2019 - 2024. All rights reserved.