symfony - 使用 webpack 和 encore 在 symfony 4.2.9 中使用 toastr
问题描述
我正在尝试使用 Webpack/Encore 将 toastr 库添加到我的项目中,但我无法使其工作。
我正在使用 Yarn 来管理 node_modules/ 中的库
在我的 app.js 中,我有:
$(() =>
{
toastr.info("test");
});
我试过了
在我的代码之前在 app.js 中简单导入
import "toastr";
未捕获的 ReferenceError:未定义 toastr
我在谷歌搜索我的问题时发现的另一个导入
import * as toastr from 'toastr'; import '../../node_modules/toastr/build/toastr.min.css';
这一项工作,但看起来 CSS 不是:弹出窗口是透明的,只有边框
在 webpack.config.js 中添加提供变量
.autoProvideVariables({ "window.toastr": require.resolve("toastr") })
未捕获的 ReferenceError:未定义 toastr
在 webpack.config.js 中添加条目
.addEntry('toastr', [ './node_modules/toastr/build/toastr.min.js', './node_modules/toastr/build/toastr.min.css' ])
未捕获的 ReferenceError:未定义 toastr
解决方案
我设法让它以这种奇怪的方式工作
在 webpack.config.js 中,CSS :
.addEntry('toastr', [
'./node_modules/toastr/build/toastr.min.css'
])
在 app.js 中,导入行:
import * as toastrlib from 'toastr';
$(() =>
{
toastrlib.info("test");
});
我可能错过了一些东西,并希望以正确的方式去做。即使它正在工作,这个也可能是错误的,所以我仍在阅读好的解决方案。
推荐阅读
- image-processing - 如何构造一个过滤器来检测对角线边缘
- android - Kotlin 无法点击空的 EditText
- sql - 有没有办法将空值等同于 SQL 分区中的非空值?
- python - 我们如何使用python获取dist的数据列表
- node.js - 如何在 docker-compose 中制作 React 应用程序?构建步骤完成后容器正在退出
- python - pip 安装错误 - 由于 EnvironmentError 无法安装软件包
- kubernetes - Prometheus 未从 GKE 中的 cadvisor 接收指标
- python - 我无法将一个(熊猫)DataFrame 正确附加到另一个 DataFrame
- hash - Teradata:哈希字符串以返回一定长度的字符串
- python - 找到最短距离,同时只访问每个节点一次