如何使用NPM安装的Toastify.js?我得到“无法解决模块指定符”。

发布于 2025-02-09 07:50:01 字数 367 浏览 2 评论 0 原文

我试图使用Toastify-js,这是用于吐司类型消息的库,使用NPM安装。 在文档中,运行安装COMAND后,它说:

-Import toastify-js进入模块以开始使用它。 从“ toastify-js”

导入吐司

但是当我在代码中插入该导入行时,我会收到此消息:

unturew typeError:无法解析模块指定符“ toastify-js”。 相对引用必须以“/”,“ ./”或“ ../” ../ p>开始

我认为我可能没有理解的东西...我应该把那条线放在哪里?或我在做错什么。文档是否假设使用我不知道的NPM安装的库,也许?

Im trying to use Toastify-js, a library for toast type messages, installing it using npm.
In the documentation, after running the installing comand it says:

-Import toastify-js into your module to start using it.
import Toastify from 'toastify-js'

but when i inlude that import line in my code i get this message:

Uncaught TypeError: Failed to resolve module specifier "toastify-js".
Relative references must start with either "/", "./", or "../".

I think theres probably something im not understanding... where should i put that line? or what im doing wrong. Is the documentation assuming something about using npm-installed libraries that I don't know about, maybe?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往事随风而去 2025-02-16 07:50:01

我知道回答有点晚了,但我自己现在就发现了。为了通过在Laravel Blade文件中通过NPM安装toastify-js,首先必须编译一个由导入烤面包的JavaScript文件。

首先,安装toastify-js。

npm install --save toastify-js

并在JavaScript文件中导入烤面包和CSS。对我来说,我只是使用默认的app.js

resources/js/app.js

import Toastify from 'toastify-js'
import "toastify-js/src/toastify.css"

window.Toastify = Toastify;

,然后我们必须在vite.config.js中编译“ app.js”文件。

vite.config.js

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';

export default defineConfig({
    plugins: [
        laravel({
            input: [
                'resources/css/app.css',
                'resources/js/app.js'
            ],
            refresh: true,
        }),
    ],
});

汇编后,我们可以通过刀片文件中的Vite Blade指令链接App.js文件。您可以将指令放在关闭的主体标签之前或您想要的任何地方。

 @vite('resources/js/app.js')

然后,刀片文件中的任何地方都可以写脚本以显示烤面包。

<script type="module">
        Toastify({
            text: "Hello, I am toasted!!",
            duration: 3000,
            destination: "",
            newWindow: true,
            close: true,
            gravity: "bottom", // `top` or `bottom`
            position: "right", // `left`, `center` or `right`
            stopOnFocus: true, // Prevents dismissing of toast on hover
            style: {
                background: "linear-gradient(to right, #00b09b, #96c93d)",
            },
            onClick: function() {} // Callback after click
        }).showToast();
    </script>

还要确保脚本标签具有“模块”的类型。否则,烤面包不会出现。

希望您可以按照这些步骤以相同的方式显示烤面包。请告诉我,如果我有任何我应该知道的要修复或改善此方法的东西。

另外,要知道如何更好地自定义吐司,请确保阅读Toastify-JS文档。

I know it is a bit late for answering but I just found out about it myself right now. In order to use toastify-js by installing through npm in Laravel blade files firstly we must compile a JavaScript file which consists of importing Toastify.

Firstly of course, install the toastify-js.

npm install --save toastify-js

And in a JavaScript file import the Toastify and CSS of it. For me, I just went with the default app.js

resources/js/app.js

import Toastify from 'toastify-js'
import "toastify-js/src/toastify.css"

window.Toastify = Toastify;

And after that we must compile the "app.js" file in vite.config.js.

vite.config.js

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';

export default defineConfig({
    plugins: [
        laravel({
            input: [
                'resources/css/app.css',
                'resources/js/app.js'
            ],
            refresh: true,
        }),
    ],
});

After the compilation, we can just link the app.js file through the vite blade directive in our blade file. You can just put the directive before the closing body tag or anywhere you like if you want to.

 @vite('resources/js/app.js')

And then anywhere in your blade files you can just write the script to show the toast.

<script type="module">
        Toastify({
            text: "Hello, I am toasted!!",
            duration: 3000,
            destination: "",
            newWindow: true,
            close: true,
            gravity: "bottom", // `top` or `bottom`
            position: "right", // `left`, `center` or `right`
            stopOnFocus: true, // Prevents dismissing of toast on hover
            style: {
                background: "linear-gradient(to right, #00b09b, #96c93d)",
            },
            onClick: function() {} // Callback after click
        }).showToast();
    </script>

And also makes sure that the script tag has the type of "module". Or else, the toast will not show up.

I hope you can show toast the same way by following those steps. Please let me know if I have anything that I should know to fix or to improve this method.

Also, in order to know how to customize your toast better, make sure to read the toastify-js docs.
https://github.com/apvarun/toastify-js/blob/master/README.md

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文