首页 > 解决方案 > 未捕获的类型错误:sweetalert2_react_content_1.default 不是函数

问题描述

我有一个使用 TypeScript 的反应模块。

我想在 Swal 组件中使用 html,但这不起作用。

这是我的代码:

import Swal from "sweetalert2";
import withReactContent from "sweetalert2-react-content";

const MySwal = withReactContent(Swal);

MySwal.fire(<p>Holahola</p>);

当我将鼠标悬停在“Holahola”上时,我收到错误消息:“类型‘元素’没有与类型‘SweetAlertOptions’.ts(2769) 相同的属性”。

当我尝试在 Chrome 中运行我的代码时,我得到:“Uncaught TypeError: sweetalert2_react_content_1.default is not a function”

有人知道这里发生了什么吗?

我也试过:

MySwal.fire({
    title: <p>Holahola</p>
})

然后我得到这个错误: 在此处输入图像描述

标签: reactjstypescriptreact-hookssweetalert2

解决方案


这是已知问题,您可以在此处订阅:https ://github.com/sweetalert2/sweetalert2-react-content/issues/102

作为一种解决方法,SweetAlertOptions用于传递参数:

MySwal.fire({ 
  title: <p>Holahola</p> 
});

推荐阅读