Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

通过CDN引入antd-mobile,会报错Uncaught ReferenceError: mobile is not defined #3710

Closed
1 task done
Kilims opened this issue Sep 15, 2020 · 3 comments
Closed
1 task done

Comments

@Kilims
Copy link

Kilims commented Sep 15, 2020

  • I have searched the issues of this repository and believe that this is not a duplicate.

Reproduction link

http://106.55.10.117/tlink_loan/RegistPage

Steps to reproduce

在create-react-app脚手架,成功引入antd和antd-mobile后,将两者都换成CDN形式引入,模仿"通过CDN引入antd"的步骤来引入antd-mobile:

  1. 在index.html引入CDN链接:
<script crossorigin src="https://cdn.bootcdn.net/ajax/libs/antd/4.3.5/antd.min.js"></script>
<script crossorigin src="https://cdn.bootcdn.net/ajax/libs/antd-mobile/2.3.3/antd-mobile.min.js"></script>
  1. 在cra 的 config-overrides 中,external antd-mobile
config.plugins.push(new HtmlWebpackExternalsPlugin({
    externals: [
        {
            module: "antd",
            entry: "https://cdn.bootcdn.net/ajax/libs/antd/4.3.5/antd.min.js",
            global: "antd"
        }, {
            module: "antd-mobile",
            entry: "https://cdn.bootcdn.net/ajax/libs/antd-mobile/2.3.3/antd-mobile.min.js",
            global: "antd-mobile"
        },
    ]
})
...
module.exports = override(
....
addWebpackExternals({
    "antd": "antd",
    "antd-mobile": "antd-mobile"
})
...
  1. 注释掉babel import
module.exports = override(
    //   fixBabelImports("antd", {
//     libraryName: "antd",
//     libraryDirectory: "es",
//     style: true,
//   }),
//   fixBabelImports("antd-mobile", {
//     libraryName: "antd-mobile",
//     libraryDirectory: "es",
//     style: true,
//   }),
)

What is expected?

antd和antd-mobile都能正常引入,无报错且页面正常

What is actually happening?

antd通过上述方法能正常引入且无报错
antd-mobile报错 Uncaught ReferenceError: mobile is not defined

Environment Info
antd 2.3.2
React 16.13.1
System MAC OS Catalina
Browser Chrome

antd成功引入,在控制台能console出window.antd,无报错,然而antd-mobile换成CDN的话,console.log(window.antd-mobile)会出现Uncaught ReferenceError: mobile is not defined错误,经查看window对象,是已经定义了antd-mobile的全局变量的,通过window["antd-mobile"]是可以看到对应的方法,但是window.antd-mobile不行,貌似默认是window.antd-mobile?怎么解决呢?
上面的网址,是已经在index.html引入了antd-mobile,但是webpack打包没有exclude的例子,所以页面能正常显示哈。进去后进入控制台,打印出window对象的话,是可以看到window里有antd-mobile对象的。但是如果我webpack打包exclude了antd-mobile,就会报跟window.antd-mobile一样的Uncaught ReferenceError: mobile is not defined 错误

image

image

应该是语法的问题,js里如果window.antd-mobile的话,-后的字符会undefined,但是antd-mobile cdn引入后默认是antd-mobile这个对象,需要通过window["antd-mobile"]才能调用到这个对象,这怎么解决呢?

@CHU295
Copy link

CHU295 commented Jul 1, 2021

坑爹项目没人维护了;
我是这样解决的
在html下面重新定义全局变量window.AntdMobile = window['antd-mobile']
然后吧externals 改成"antd-mobile" : "AntdMobile"

@awmleer
Copy link
Member

awmleer commented Sep 23, 2021

v5 支持 umd 引入,是 window.antdMobile,可以参考这个 codepen:
https://codepen.io/awmleer/pen/zYwpMMd

@awmleer awmleer closed this as completed Sep 23, 2021
@Pateo-fanyang
Copy link

v5 支持 umd 引入,是 window.antdMobile,可以参考这个 codepen: https://codepen.io/awmleer/pen/zYwpMMd

进去看了, 有报错: Uncaught ReferenceError: antdMobile is not defined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants