【问题标题】:Importing sweetalert2 module returns 404 in angular2导入 sweetalert2 模块在 angular2 中返回 404
【发布时间】:2017-02-15 13:02:24
【问题描述】:

一直在关注在线文档并尝试在我的 angular2 应用程序中使用 sweetalert2 插件。

不幸的是,我包含了这个

import { default as swal } from 'sweetalert2';

我收到一个 404 错误,提示找不到 sweetalert

【问题讨论】:

  • 文件在 node_modules 中??

标签: javascript angular typescript sweetalert2


【解决方案1】:

您的导入应该是import swal from 'sweetalert2';import * as swal from 'sweetalert2;

【讨论】:

    【解决方案2】:
    1. 通过引用必要的文件来初始化插件:
    <script src="sweetalert2.all.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/core-s/2.4.1/core.js"></script>
    

    如果需要,您还可以单独包含样式表:

    > <script src="sweetalert2.min.js"></script> <link rel="stylesheet"
    > href="sweetalert2.min.css">
    

    或者

    // ES6 模块或 TypeScript

    从“sweetalert2”导入 swal

    // CommonJS

    const swal = require('sweetalert2')

    1. 页面加载后调用 sweetAlert2 函数
    swal({
      title: 'Error!',
      text: 'Do you want to continue',
      type: 'error',
      confirmButtonText: 'Cool'
    })
    

    【讨论】:

      【解决方案3】:

      根据SweetAlert2 documentation,对于 ES6 模块或 TypeScript,您应该像这样导入 SweetAlert2:

      import swal from 'sweetalert2' 
      

      【讨论】:

      • 当我尝试使用 swal 触发器时,我再次得到同样的错误 swal({ title: 'Error!', text: 'Do you want to continue', type: 'error', confirmButtonText: '酷'})
      • 你添加了 import 'sweetalert2';在您的应用或共享模块中?
      • 是的,我做到了,导入工作,但是当我包含 swal() 函数时,它会抛出错误
      • 从 'sweetalert2/src/sweetalert2.all.js' 导入 swal;但它打破了 angular2 构建
      猜你喜欢
      • 2017-03-31
      • 1970-01-01
      • 1970-01-01
      • 2017-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-26
      • 2016-06-05
      相关资源
      最近更新 更多