我正在使用create-react-app
样板,并为布局实现一个管理主题。在index.html
文件中,有一个来自主题资产的script
导入,如下所示:
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>
<script src="%PUBLIC_URL%/assets/global/scripts/app.min.js" type="text/javascript"></script>
</body>
在该脚本中,有一个名为App
的全局javascript对象来操作UI。现在,我想在我的React
代码中调用这个对象:
export const alertMessage = (options = {}) => {
const defaultOption = {
type: 'info',
message: 'message',
closeInSeconds: 5
};
if (ALERT_TYPE_CORRECT[options.type] != null)
options.type = ALERT_TYPE_CORRECT[options.type];
options.icon = DEFAULT_ALERT_ICON_MAP[options.type];
App.alert(Object.assign(defaultOption, options)); // here
};
但是,它抛出了一个错误:
./src/Commons/utils/index.js
Line 25:3: 'App' is not defined no-undef
如何做到这一点?我应该使用window.App
访问它吗?
你能发布index.html文件吗?脚本文件是否位于根div之上?如果是这样,您将能够通过使用窗口变量来访问它。