如何使用脚本。在JADE模板中

Cat*_*azz 3 express pug

我已经使用JADE模板使用express框架创建了一个简单的节点应用程序。

在学习过程中一切都很好,直到我开始尝试运行一些客户端JS,但我不知道该怎么做。

我需要在app / index.js中做些事情来告诉节点有关它们的信息吗?任何帮助将非常感激。

谢谢

玉器

 extends layout

 block content

 h1 Title

 script.
   console.log("I am running on the client");
Run Code Online (Sandbox Code Playgroud)

app.js

 extends layout

 block content

 h1 Title

 script.
   console.log("I am running on the client");
Run Code Online (Sandbox Code Playgroud)

layout.jade

 doctype html
 html
   head
     title= title
     link(rel='stylesheet', href='/stylesheets/style.css')
     link(rel='stylesheet', href='/stylesheets/bootstrap.css')
     link(rel='stylesheet', href='/stylesheets/style.css')
     script(src='/javascripts/jquery-2.1.3.js') 
     script(src='/javascripts/bootstrap.js') 

   body
     div(class="navbar navbar-inverse navbar-fixed-top")
       .container
         .navbar-header
           button.navbar-toggle(type="button", data-toggle="collapse", data-target=".navbar-collapse")
             span.icon-bar
             span.icon-bar
             span.icon-bar
           a.navbar-brand(href="/") Twitter
         div(class="collapse navbar-collapse")
           ul(class="nav navbar-nav")
             li.active
               a(href="#") Home
             li
               a(href="#about") About
             li
               a(href="#contact") Contact
     block content     
Run Code Online (Sandbox Code Playgroud)

Bre*_*nan 5

任何内联脚本都可以像这样运行

script.
  if (usingJade)
   console.log('you are awesome')
 else
   console.log('use jade')
Run Code Online (Sandbox Code Playgroud)

来自Docs。

任何外部JS文件都可以像这样加载:

script(src="/path/to/script.js")
Run Code Online (Sandbox Code Playgroud)

另外,您可能要确保您确实在使用布局文件。Jade建议执行以下操作:

extends ./layout.jade
Run Code Online (Sandbox Code Playgroud)

您拥有文件路径并附加了扩展名的位置。尽管扩展名是可选的,因为您在中指定了jade引擎app.js。

让我知道这会有所帮助!

  • 啊,我很高兴能帮上忙。我正要评论说它在我的环境中有效,所以它一定是别的东西! (2认同)