Unable to import toastr module with Angular 2

时光怂恿深爱的人放手 提交于 2019-12-19 09:48:05

问题


I am creating an Angular 2 application with version 2.0.0-alpha.46 and I trying to import the toastr module.

I have downloaded the library and also downloaded the definitely typed file as well.

In my html page, I add a <script> tag for the .js file:

<script src="./src/libs/toastr/toastr.min.js"></script>

In Visual Studio Code, I try to import the module like so:

import * as toastr from 'toastr';

I am then able to call toastr.info('message') in my component - I even get intellisense; However, when i actually try to load the webpage I get a 404 /src/toastr not found error.

If I try to get explicit about finding the toastr definitely typed file:

import * as toastr from '../../definitions/toastr/toastr';

I will get "[full path to ts module] is not a module."

I have also tried adding ///<reference path="../../toastr.d.ts" /> to the top of the file I am using to import the module to no avail.

Does anyone have an idea of what may be happening? I am drawing a blank on what I may have wrong. Thanks in advance.


回答1:


I think that the toastr.min.js file doesn't contain a module at all. My understanding is that TypeScript definition files help you to define the methods and structures used by the toastr library. This is helpful within your IDE for code completion. This definition file doesn't actually create a module that can be use at runtime...

This means that you can use directly the toastr object directly without having to import it like this: import * as toastr from '...';.

Here is the corresponding plunkr: https://plnkr.co/edit/wzdoisKBrZYTeSX8r7Nd?p=preview.

Hope that makes sense. Thierry



来源:https://stackoverflow.com/questions/34870980/unable-to-import-toastr-module-with-angular-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!