You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pug模板如何传递参数?href拼接URL变量失效怎么解决

问题原因

Pug 中 #{} 插值语法仅适用于文本内容的变量注入,在属性的表达式场景下,不需要用 #{} 包裹变量,直接写变量名即可调用,你之前的写法属于语法误用所以无法正常解析。

正确写法示例

doctype html
html
  head
    block head
      meta(charset="utf-8")
      meta(name="viewport", content="width=device-width")
      meta(http-equiv="X-UA-Compatible", content="IE=edge")
      meta(name="x-apple-disable-message-reformatting")
      link(rel="stylesheet", href="style.css", data-inline)
  body
    p Приветствую #{name},
    p  #{url} 

    p 
      // 写法1:直接拼接字符串和变量
      a(href='/' + url) Google
      // 写法2:使用ES6模板字符串,可读性更高
      a(href=`/${url}`) Google

内容的提问来源于stack exchange,提问作者Mediator

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 17:45:01