带有Angular 7的SweetAlert

时间:2019-02-26 15:32:00

标签: angular sweetalert

我正在尝试在我的角度项目中使用甜蜜警报。

这就是我使用甜蜜警报的方式:

import swal from 'sweetalert';

swal({
    title: "Problem",
    text: "Try again later!",
    icon: "error"
  })

我收到以下错误:

  

node_modules / sweetalert / typings / sweetalert.d.ts(4,9)中的错误:错误TS2403:后续变量声明必须具有相同的类型。变量'swal'必须为'typeof import(“ C:/ Users / user / Desktop / University / Thesis / workspace / web / myProject / project / node_modules / sweetalert / typings / sweetalert”)'类型,但此处的类型为' SweetAlert”。

有人可以帮我吗?

7 个答案:

答案 0 :(得分:2)

我有同样的问题,我的解决方法是这个。

import * as _swal from 'sweetalert';
import { SweetAlert } from 'sweetalert/typings/core';

const swal: SweetAlert = _swal as any;

由于某种原因,名称“ swal”显示错误,如果您通过“ _swal”更改别名,则应该可以使用

答案 1 :(得分:0)

编译Angular项目的简单解决方案是 转到您的项目文件夹 \ node_modules \ sweetalert \ typings \ sweetalert.d.ts

在此文件中,只需注释该行 // const swal:SweetAlert;

最终文件如下:

import swal, { SweetAlert } from "./core";

declare global {
  // const swal: SweetAlert;
  const sweetAlert: SweetAlert;
}

export default swal;
export as namespace swal;

答案 2 :(得分:0)

对于SweetAlert2,您只应使用此行将其导入:

import * as Swal from 'sweetalert2';

然后以这种方式使用它:

Swal.fire('Hello world!');

答案 3 :(得分:0)

我不知道我刚刚做了什么,但是显然,这是编译器要我执行的,因为当我遇到相同的问题时,它可以工作:

在文件“ /node_modules/sweetalert/typings/sweetalert.d.ts”中,替换行中swal的类型
“ swal:SweetAlert”和“ swal:typeof swal”

因此(之前):

import swal, { SweetAlert } from "./core";

declare global {
const swal: SweetAlert;
const sweetAlert: SweetAlert;
}

export default swal;
export as namespace swal;

成为(之后):

import swal, { SweetAlert } from "./core";

declare global {
const swal: typeof swal;
const sweetAlert: SweetAlert;
}

export default swal;
export as namespace swal;

答案 4 :(得分:0)

当我删除现有的简单代码行时,问题就解决了:

node_modules/sweetalert/src/sweetalert.d.ts 

代码将如下所示:

import swal, { SweetAlert } from "./core";

declare global {
  const swal: SweetAlert;
  const sweetAlert: SweetAlert;
}

export default swal;
export as namespace swal;

只需删除以下行:const swal: SweetAlert;

您可以在gitgub上检查:https://github.com/AlbinoDrought/sweetalert-sans-ts-namespace/commit/699f10b8546a524000dd3e3b41bf7a7e599a2666

答案 5 :(得分:0)

您可以尝试

import Swal from 'sweetalert2'; 

Swal.fire('Error', 'Please Enter Username', 'error');

答案 6 :(得分:-1)

首先,..我最近正在更新node_modules ...,并且出现了一些问题。最后一个是这个!

我尝试过npm install sweetalert2 .... 但是仍然给我带来问题 所以最终的解决方案是... 消除那条线,工作精细,没有错误..!

相关问题