【问题标题】:Cannot invoke an expression whose type lacks a call signature. Type 'typeof import("sweetalert2")' has no compatible call signatures无法调用其类型缺少调用签名的表达式。类型 'typeof import("sweetalert2")' 没有兼容的调用签名
【发布时间】:2019-10-08 14:02:00
【问题描述】:

我收到“无法调用类型缺少调用签名的表达式。类型 'typeof import("sweetalert2")' 没有兼容的调用签名。”在角度使用 SWAL 时出现此错误。

我的 package-cli.json 看起来像

    "ng2-sweetalert2": {
    "version": "0.0.8",
    "resolved": "https://registry.npmjs.org/ng2-sweetalert2/-/ng2-sweetalert2-0.0.8.tgz",
    "integrity": "sha1-+VkDV2dqeLIMf3eFTLVt/cJda0Q=",
    "requires": {
        "lodash.assign": "^4.0.8",
        "sweetalert2": "^5.2.0"
    },
    "dependencies": {
        "es6-promise": {
            "version": "4.2.6",
            "resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-4.2.6.tgz",
            "integrity": "sha512-aRVgGdnmW2OiySVPUC9e6m+plolMAJKjZnQlCwNSuK5yQ0JN61DZSO1X1Ufd1foqWRAlig0rhduTCHe7sVtK5Q=="
        },
        "sweetalert2": {
            "version": "5.3.8",
            "resolved": "https://registry.npmjs.org/sweetalert2/-/sweetalert2-5.3.8.tgz",
            "integrity": "sha1-YALBhFPQYMQLnMVL+DoBDHHjlik=",
            "requires": {
                "es6-promise": "^4.2.6"
            }
        }
    }
}

"sweetalert2": {
      "version": "8.9.0",
      "resolved": "https://registry.npmjs.org/sweetalert2/-/sweetalert2-8.9.0.tgz",
      "integrity": "sha512-gI+wL44QRbfd6FvRVB8KKLQPhD/8jP3XUNrvYGcSlU7IA09dQtQw3rrLCbM3c+HY0L3O8VTz1Gvu29n8ryIgag=="
}

我在 package.json 依赖项中有以下内容

"dependencies":{
"ng2-sweetalert2": "0.0.8",
"sweetalert2": "^8.9.0",}

在一个组件中,我的代码如下所示

import * as swal from 'sweetalert2';

        swal({
          text: 'Login Successful!',
          timer: 1000,
          onOpen: () => {
            swal.showLoading()
          }
        })then((result) => {
          if (result.dismiss === swal.DismissReason.timer) {
              // calling few required functions here
          }
        })

在组件中,我收到了上述错误。搜索了很多,但无法找出问题所在。请帮帮我。

【问题讨论】:

  • 您在拨打then 之前肯定会错过.。但是我不认为这是你的问题。你有发生错误的那一行吗?此外,您安装了 ng2-sweetalert2,但您只使用了 sweetalert2。可以试试this way吗?
  • 是的,我错过了那个点。即使在使用它之后,我也会收到同样的错误。我使用了 npm install --save sweetlaert2。

标签: angular sweetalert2


【解决方案1】:

我也有同样的问题。使用

swal.fire() 

而不是

swal()

它会解决你的问题。

【讨论】:

    【解决方案2】:

    您通过 package.json 安装了两个依赖项:

    • sweetalert2,这是一个纯(!)javascript 库。它与角度无关。
    • ng2-sweetalert2,这是一个 Angular 服务,它将原生的 sweetalert2 库包装到一个 Angular 服务中。根据GitHub page,这已被弃用。

    我建议从您的 package.json 中删除 sweetalert2,并添加 ngx-sweetalert2:

    npm install --save sweetalert2 @sweetalert2/ngx-sweetalert2
    

    设置ngx-sweetalert2 中描述的模块,从那里,您有多种方法可以使其工作,通过指令或组件。

    【讨论】:

      猜你喜欢
      • 2017-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-26
      • 1970-01-01
      • 2020-05-17
      • 1970-01-01
      相关资源
      最近更新 更多