【问题标题】:How To import sanitize.js to react apps Function / class not found如何导入 sanitize.js 以响应应用程序功能/类未找到
【发布时间】:2020-03-17 04:25:29
【问题描述】:

我有一些问题,在我的项目中我需要在我的项目中添加Sanitize.js,我已经复制到我自己的第 3 方文件夹前供应商 导入它我正在使用

import  {san} from '../../vendor/Sanitize' //There's No error when compiling this one

但是当我运行页面时出现错误,我正在尝试从 Sanitize.js 调用该函数,就像自述文件中所说的那样使用它

var s = new san.Sanitize({ 
elements:   ['a', 'span'],
attributes: { 
    a: ['href', 'title'], 
    span: ['class'] 
},
protocols:  { 
    a: { href: ['http', 'https', 'mailto'] }
}
});
s.clean_node(p);

错误是

san.Sanitize 不是函数/类构造函数

知道为什么会这样吗?还是我错过了什么?编译过程中没有错误,只有在我尝试运行网页时才会出现错误,

【问题讨论】:

    标签: javascript reactjs typescript


    【解决方案1】:

    因为Sanitize.js 不是模块。

    也许你可以尝试以下解决方案:

    1. sanitize.js末尾添加export default Sanitize;
    2. 使用import Sanitize from "./sanitize"; 导入。
    3. sanitize.js中删除以下代码。
    if ( typeof define === "function" ) {
        define( "sanitize", [], function () { return Sanitize; } );
    }
    

    【讨论】:

    • 您好,谢谢您的回答,我尝试了您的回答仍然没有成功,我收到另一个错误[12:04:39] 'browserify_typescript' errored after 3.85 s [12:04:39] SyntaxError in plugin 'gulp-browserify' /d/Work/nubela/untitled-web/vendor/Sanitize.js:258 export default Sanitize; ^ ParseError: 'import' and 'export' may appear only with 'sourceType: module' [12:04:39] 'build-js' errored after 12 s
    • @dmh 你好,看来这是一个新问题,你可以试试这个吗? stackoverflow.com/questions/40029113/…
    • 嗨,谢谢你的建议,我正在使用这个module.exports.Sanitize = Sanitize;,结果我的浏览器无法识别导出默认值
    猜你喜欢
    • 2014-07-31
    • 2022-01-09
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 2023-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多