如何在pug文件中选择特定的CSS,并在路径上包含'include'

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

我想制作动态页面,它会基于route.js文件来选择必须应用的CSS。我在node.js和pug中编码此页面。我尝试了与此类似的方法。

routes.js文件

router.get("/", (req,res) => {
const indexCss = "rel='stylesheet' href='index.css' "
res.render("index"), {css:indexCss};
});

index.pug文件

head
    link(#{css})
    title Index Page

很遗憾,它没有用。我想知道如何使用req和body-parser选择link,但我认为这样也不行。

我认为这将非常有用,它将有可能使头部部分化,并仅将其与指定的CSS一起包含在route.js文件中,以使代码更有条理,更短。

我没有其他想法了。有人可以指出我该怎么做,或者给我一个我可以弄乱的想法吗?

css node.js pug
1个回答
0
投票

Pug不会像这样处理属性插值。如果您想将包含样式表路径的变量从路由器传递到Pug,则可以这样操作:

routes.js

router.get('/', (req,res) => {
  const cssPath = 'index.css'
  res.render('index', { cssPath })
})

index.pug

head
  link(rel='stylesheet', href= cssPath)
  title Index Page

Pug attribute interpolation in the Pug documentation的更多信息。

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