【问题标题】:How to import boxicons in node js如何在节点js中导入boxicons
【发布时间】:2021-06-11 03:37:25
【问题描述】:

我有 node js express 应用程序,我已经安装了 boxicons 节点包,

$ npm install boxicons --save

boxicons 包安装在节点模块中,但我不知道如何正确导入它并在所有视图 ejs 文件中使用它, 我尝试在 app.js 中通过以下任一方式包含它,

import 'boxicons';

const boxicons = require('boxicons')

app.locals.boxicons = require('boxicons')

他们都没有工作,他们使 app.js 崩溃并出现错误。 请帮我解答

更新: 我在 head 标签中使用了他们的 css 链接,

 <link href='https://unpkg.com/boxicons@2.0.7/css/boxicons.min.css' rel='stylesheet'>

但它会减慢网站在生产中的加载速度,这就是为什么我避免链接他们的 unpkg css 或脚本,

【问题讨论】:

    标签: node.js import icons node-modules


    【解决方案1】:

    要在本地使用,这对我有用,在服务器上添加:

    app.use("/icons", express.static(path.join(__dirname, "../node_modules/boxicons")));
    

    在视图或 html 中使用:

    <link href = "/icons/css/boxicons.min.css" rel="stylesheet">
    <script src="/icons/dist/boxicons.js" type="text/javascript"></script>
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案2】:

    将此行添加到您的 .ejs 文件中

    <script src="https://unpkg.com/boxicons@latest/dist/boxicons.js"></script>
    

    现在可以在你想要的 ejs 文件中使用任何图标

    <box-icon type="solid" name="rocket"></box-icon>
    <box-icon type="logo" name="facebook-square"></box-icon>
    

    【讨论】:

      猜你喜欢
      • 2017-04-27
      • 2021-10-10
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-02
      • 1970-01-01
      • 2019-11-19
      相关资源
      最近更新 更多