简体   繁体   English

无法在打字稿上设置 Sweetalert 选项

[英]Can't set sweetalert options on typescript

Installed packages:安装的软件包:

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

How I'm using sweetalert:我如何使用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) => { });
}

I'm getting these errors:我收到这些错误:

  • VSC IntelliSense: VSC 智能感知:

'Argument of type '{ title: string; '类型参数'{标题:字符串; text: string;文本:字符串; type: string;类型:字符串; showCancelButton: boolean;显示取消按钮:布尔值; showConfirmButton: boolea...' is not assignable to parameter of type 'Settings & PromtModalSettings'. showConfirmButton: boolea...' 不能分配给类型为 'Settings & PromtModalSettings' 的参数。
Type '{ title: string;输入 '{ 标题:字符串; text: string;文本:字符串; type: string;类型:字符串; showCancelButton: boolean;显示取消按钮:布尔值; showConfirmButton: boolea...' is not assignable to type 'PromtModalSettings'. showConfirmButton: boolea...' 不可分配到类型 'PromtModalSettings'。 Types of property 'type' are incompatible.属性“类型”的类型不兼容。 Type 'string' is not assignable to type 'PromtType'.'类型 'string' 不能分配给类型 'PromtType'。 at: '101,14' source: 'ts'在:'101,14' 来源:'ts'

  • TypeScript compiles, but on browser console, I get this: TypeScript 编译,但在浏览器控制台上,我得到这个:

ERROR Error: SweetAlert: Unexpected 2nd argument (function (remove) {...错误错误:SweetAlert:意外的第二个参数(函数(删除){...

I tried setting the options type as SweetAlert.Settings & SweetAlert.AlertModalSettings , but I can't access the namespace SweerAlert , and also tried any .我尝试将options类型设置为SweetAlert.Settings & SweetAlert.AlertModalSettings ,但我无法访问命名空间SweerAlert ,并且还尝试了any . None of them worked.他们都没有工作。

How can I set these options to sweetalert?如何将这些选项设置为 sweetalert?

TS type file. TS 类型文件。

Sweetalert does not need to install types. Sweetalert 不需要安装类型。 It already has it implemented.它已经实施了。 When installing types, I was getting a conflict between the orignal types, then getting errors.安装类型时,我在原始类型之间发生冲突,然后出现错误。 I just removed @types/sweetalert and everything worked just like the examples in their site .我刚刚删除了@types/sweetalert ,一切都像他们网站上的例子一样。

A simple fix is :一个简单的修复是:

Directly go to the project folder then \\node_modules\\sweetalert\\typings\\sweetalert.d.ts In this you will find this line直接进入项目文件夹然后 \\node_modules\\sweetalert\\typings\\sweetalert.d.ts 在这你会发现这一行

const swal: SweetAlert;

just delete or comment it.. The error is sorted..只需删除或评论它.. 错误已排序..

Rather than messing with the node_modules why not simply cast the options as SweetAlertOptions:而不是搞乱 node_modules 为什么不简单地将选项转换为 SweetAlertOptions:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM