【问题标题】:Can't set sweetalert options on typescript无法在打字稿上设置 sweetalert 选项
【发布时间】:2017-11-28 15:38:56
【问题描述】:

已安装的包:

"@angular/core": "^4.0.0"
"typescript": "~2.3.3"
"sweetalert": "^2.0.8"
"@types/sweetalert": "^1.1.28"

我如何使用 sweetalert:

import * as swal from "sweetalert";
//...
doSomething() {
    const options = {
        title: "Are you sure?",
        text: "Some text here!",
        type: "warning",
        showCancelButton: true,
        showConfirmButton: true
    };
    swal(options, (remove) => { });
}

我收到以下错误:

  • VSC 智能感知:

'类型参数'{ title: string;文本:字符串;类型:字符串; 显示取消按钮:布尔值; showConfirmButton: boolea...' 不是 可分配给“Settings & PromtModalSettings”类型的参数。
输入'{标题:字符串;文本:字符串;类型:字符串;显示取消按钮: 布尔值; showConfirmButton: boolea...' 不可分配给类型 'PromtModalSettings'。 属性“类型”的类型不兼容。 类型“字符串”不可分配给类型“PromtType”。在:'101,14' 来源:'ts'

  • TypeScript 编译,但在浏览器控制台上,我得到了这个:

错误错误:SweetAlert:意外的第二个参数(函数(删除){...

我尝试将options 类型设置为SweetAlert.Settings & SweetAlert.AlertModalSettings,但我无法访问命名空间SweerAlert,也尝试了any。他们都没有工作。

如何将这些选项设置为 sweetalert?

TS type file.

【问题讨论】:

  • 你试过import swal from 'sweetalert'而不是import * as swal吗?
  • 我明白了:Module '"sweetalert"' has no default export
  • 转到sweetalert.d.ts 并更改export = swal -> export default swal;。最近在 SweetAlert github 上报告了此问题,并已提交修复。我只是不知道它不在最新的 npm 包中。然后你可以做import swal from 'sweetalert'。但是,我的建议是使用 SweetAlert2
  • 抱歉,import swal from 'sweetalert'import * as swal 之间有什么区别?
  • 它不接受相同的属性,嵌套其他属性的按钮会出错。

标签: angular typescript sweetalert


【解决方案1】:

Sweetalert 不需要安装类型。它已经实现了。安装类型时,我遇到了原始类型之间的冲突,然后出现错误。我刚刚删除了@types/sweetalert,一切都和site 中的示例一样。

【讨论】:

  • 我从几个小时就陷入了这个错误,所以你说删除@types/sweetalert,请你能指定更多我应该从哪里删除这个@types/sweetalert,因为我用这个命令安装了swal @987654325 @ 然后像这样在我的组件中导入它 import swalfrom 'sweetalert';@types/sweetalert
  • 我的 package.json 中也没有 @types/sweetalert 并且仍然出现此错误。
【解决方案2】:

一个简单的解决方法是:

直接进入项目文件夹然后\node_modules\sweetalert\typings\sweetalert.d.ts 在这你会发现这一行

const swal: SweetAlert;

只需删除或评论它.. 错误已排序..

【讨论】:

  • 这可能是一个快速而肮脏的修复,但绝对不是一个好的修复。更新依赖后更改将消失。
【解决方案3】:

与其搞乱 node_modules 为什么不简单地将选项转换为 SweetAlertOptions:

 const options = {
        title: "Are you sure?",
        text: "Some text here!",
        type: "warning",
        showCancelButton: true,
        showConfirmButton: true
    } as SweetAlertOptions;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-28
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    • 1970-01-01
    • 2019-12-17
    • 2019-10-22
    • 1970-01-01
    相关资源
    最近更新 更多