在 .pug 文件中安装聊天小部件

Install chat widget in .pug file

我需要在我们的网站上安装聊天机器人小部件。该小部件需要调用 javascript 和 HTML 标记。我在HTML环境下安装没问题

但是我在使用 .pug 作为前端的系统之一上安装它时遇到问题。

奇怪的是它在其中一页上取得了成功。但是当我在另一个页面上尝试时失败了。

这是 html 中的示例安装:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Chatbot</title>
     <script src="https://unchatpkg.com/vue"></script>
     <script src="https://chatbot-prod.firebaseapp.com/plus-chatbotcomponent.min.js"></script> 
</head>
<body>
    <plus-chatbot-component></plus-chatbot-component> 
</body>
</html>

但在 .pug 中有点不同,javascript 和组件必须在 HEAD 中,无需将其放入 BODY

这是第 1 页成功的示例安装(将其从 html 更改为遵循 .pub 格式后):

doctype html
head
  title= title
  meta(name='viewport', content='width=device-width, initial-scale=1.0')
  script(src='https://opnge.com/vue')
  script(src='https://my-chatbot-prod.firebaseapp.com/my-chatbot-component.min.js')
  plus-chatbot-component

body

所以我尝试在另一页(第2页)上使用相同的方法。但是这次是错误的。在这个页面上,代码有点不同。我找不到头部区域。我刚找到 body 区域。所以我将相同的方法粘贴到 body 上方,但出现错误。代码如下:

extends ../layout
append styles
  //-link(rel='stylesheet' type='text/css' href='/stylesheets/customer/customer.css')
  link(rel='stylesheet' type='text/css' href='/stylesheets/customer/login.css')
  style.

script(src='https://opnge.com/vue')
  script(src='https://my-chatbot-prod.firebaseapp.com/my-chatbot-component.min.js')
  my-chatbot-component

block body
  .bg-login.segment(style='background-image: url('+background_url+')')
    include ../header_customer

报错是这样的:

Error: /home/crmroot/app-roro-jonggrang-v/views/customer/login.pug:7:1

Only named blocks and mixins can appear at the top level of an extending template
    at makeError (/home/crmroot/app-roro-jonggrang-v/node_modules/pug-error/index.js:32:13)
    at error (/home/crmroot/app-roro-jonggrang-v/node_modules/pug-linker/index.js:7:30)
    at addNode (/home/crmroot/app-roro-jonggrang-v/node_modules/pug-linker/index.js:34:9)

请指教。谢谢

为了使用该小部件,javascript 必须包含在 head 中,并且组件元素必须在 body 中使用。

要将脚本添加到 head 并将组件添加到 body,您可以像这样修改示例 Pug 文件:

doctype html
head
  title= title
  meta(name='viewport', content='width=device-width, initial-scale=1.0')
  script(src='https://opnge.com/vue')
  script(src='https://my-chatbot-prod.firebaseapp.com/my-chatbot-component.min.js')

body
  my-chatbot-component

在您的另一个示例中,因为该文件正在扩展另一个文件,所以您在文件的基本缩进级别可以拥有的唯一语句是 extendsblock(或 appendprepend) 语句。您不能将脚本元素放在基本级别。

您应该在 layout 文件中包含 script 标记,然后修改此其他文件以将组件放入 body,如下所示:

extends ../layout

append styles
  //-link(rel='stylesheet' type='text/css' href='/stylesheets/customer/customer.css')
  link(rel='stylesheet' type='text/css' href='/stylesheets/customer/login.css')

block body
  my-chatbot-component
  .bg-login.segment(style='background-image: url('+background_url+')')
    include ../header_customer