引入 cdn减少 npm run build 构建体积
https://ant.design/docs/react/introduce-cn
CDN连接:https://unpkg.com/antd/dist/
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<title>Dva工程化配置</title>
<link rel="apple-touch-icon" href="logo192.png" />
<!--
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<link href="https://cdn.bootcdn.net/ajax/libs/antd/4.19.5/antd.min.css" rel="stylesheet">
<link rel="stylesheet" href="index.css" />
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root" class="root"></div>
<!-- 针对 antd3.x,不需要兼容IE的 忽略 polyfill.js -->
<script src="https://cdn.bootcss.com/babel-polyfill/7.6.0/polyfill.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/react/18.0.0/umd/react.development.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/moment.js/2.29.2/moment.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/antd/4.19.5/antd-with-locales.min.js"></script>
<script src="/index.js"></script>
</body>
</html>
index.html
首先引入 antd.css的样式表
再次引入相关JS类库,因为类库之间有依赖关系,不要改变顺序
编写 text/babel 代码,使用antd组件需要加上 antd. 才能正常使用,或者使用解构赋值,导出相关组件
- 依赖 react、react-dom、moment
- 需要引入babel,且指定script 的类型为 type=”text/babel”. 否则会报错
- Uncaught SyntaxError: Unexpected token ‘<’
- 文件头需要指定字符集utf-8, 否则会报错
- SyntaxError: Invalid regular expression
- 开发用 production.min.js,上线用 production.min.js
- when deploying, replace “development.js” with “production.min.js”
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Antd</title>
<link href="https://cdn.bootcdn.net/ajax/libs/antd/4.15.1/antd.min.css" rel="stylesheet">
</head>
<body>
<div id="root"></div>
<script src="https://cdn.bootcdn.net/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/babel-standalone/7.0.0-beta.3/babel.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/antd/4.15.1/antd-with-locales.js"></script>
<script type="text/babel" data-presets="env,stage-3,react">
const { ConfigProvider, DatePicker, Button, message } = antd;
function App() {
const [ date, setDate ] = React.useState(moment);
const handleChange = value => {
message.info(`您选择的日期是: ${value ? value.format('YYYY-MM-DD') : '未选择'}`);
setDate(value);
};
return (
<ConfigProvider>
<DatePicker onChange={handleChange} value={date}/>
<div>
当前日期:{date.format('YYYY-MM-DD')}
</div>
<Button type="primary">提交</Button>
</ConfigProvider>
);
}
ReactDOM.render(<App/>, document.getElementById('root'));
</script>
</body>
</html>
script中的 async 和 defer
script标签中的async和defer
https://www.cnblogs.com/jiasm/p/7683930.html
async
async的设置,会使得script脚本异步的加载并在允许的情况下执行async的执行,
并不会按着script在页面中的顺序来执行,而是谁先加载完谁执行
defer
如果script标签设置了 defer属性,则浏览器会异步的下载该文件并且不会影响到后续DOM的渲染;
如果有多个设置了defer的script标签存在,则会按照顺序执行所有的script;
defer脚本会在文档渲染完毕后,DOMContentLoaded事件调用前执行
<script async defer src="http://www.google.cn/maps/api/js?key=AIzaSyA5bpEs3xlB8vhxNFErwoo3MXR64uavf6Y&callback=initMap"></script>