当使用Pug模板时出现“indent”意外标记

5

我有一个简单的应用程序,应输出少量内容。我使用 NodeJs、Express 和 Pug

const pug = require('pug');
const express = require('express');
const app = express();

const indexFile = 'index'; // the file to load

app.set('view engine', 'pug'); // use pug

app.get('/', function (req, res) {
  res.render(indexFile, {content: 7}); // load the file and set the variable to 7
});

app.listen(8888, function () {
  console.log('Server running on port 8888');
});

And my Pug file / HTML

doctype html
    link(rel='stylesheet', href='../CSS/requirements.css')
    script(src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js')
    body
      p = content

启动服务器时,我收到了这个错误消息。
Error: C:\Users\mah\Desktop\Test\views\index.pug:2:1
    1| doctype html

  > 2|     link(rel='stylesheet', href='../CSS/requirements.css')

-------^
    3|     script(src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js')

    4|     body

    5|       p = content


unexpected token "indent"
    at makeError (C:\Users\mah\Desktop\Test\node_modules\pug-error\index.js:32:13)
    at Parser.error (C:\Users\mah\Desktop\Test\node_modules\pug-parser\index.js:53:15)
    at Parser.parseExpr (C:\Users\mah\Desktop\Test\node_modules\pug-parser\index.js:264:14)
    at Parser.parse (C:\Users\mah\Desktop\Test\node_modules\pug-parser\index.js:112:25)
    at parse (C:\Users\mah\Desktop\Test\node_modules\pug-parser\index.js:12:20)
    at Object.parse (C:\Users\mah\Desktop\Test\node_modules\pug\lib\index.js:126:22)
    at Function.loadString [as string] (C:\Users\mah\Desktop\Test\node_modules\pug-load\index.js:45:21)
    at compileBody (C:\Users\mah\Desktop\Test\node_modules\pug\lib\index.js:86:18)
    at Object.exports.compile (C:\Users\mah\Desktop\Test\node_modules\pug\lib\index.js:243:16)
    at handleTemplateCache (C:\Users\mah\Desktop\Test\node_modules\pug\lib\index.js:216:25)

有人能帮我解决这个问题吗?

1个回答

5

我认为您忘记在doctype html之后加上html了。

doctype html
html
  link(rel='stylesheet', href='../CSS/requirements.css')
  script(src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js')
  body
    p=content

doctype htmlhtml应该处于同一层次。顺便提一下,将p = content更改为p=content以打印传递的值。


当使用以下doctype html时,它可以工作。但是这不包括CSS链接(rel='stylesheet', href='../CSS/requirements.css')。只要使用正确的路径。 - Pankaj

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接