[英]How can I use styled-components CDN build on browser?
index.html索引.html
I'm getting the CDN file from:我从以下位置获取 CDN 文件:
<script src="https://cdnjs.cloudflare.com/ajax/libs/styled-components/4.3.2/styled-components-macro.cjs.min.js"></script>
How can I access the styled
function?如何访问
styled
化功能?
const {styled} = window.styled-components
doesn't work. const {styled} = window.styled-components
不起作用。
For version 5, as mentioned in the docs you now need to include react-is
before styled-components
对于版本 5,如文档中所述,您现在需要在
styled-components
之前包含react-is
<!-- react, react-dom dev bundles -->
<script crossorigin src="//unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="//unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<!-- react-is -->
<script crossorigin src="//unpkg.com/react-is/umd/react-is.production.min.js"></script>
<!-- styled-components -->
<script crossorigin src="//unpkg.com/styled-components/dist/styled-components.min.js"></script>
<script>
const Component = window.styled.div`background-color: green;`;
</script>
styled-components-macro.cjs.min.js
is not UMD , so if you include this file you did not get global variable. styled-components-macro.cjs.min.js
不是UMD ,因此如果包含此文件,则不会获得全局变量。
If you're not using a module bundler or package manager we also have a global ("UMD") build hosted on the unpkg CDN.
如果您不使用模块捆绑器或包管理器,我们还有一个托管在 unpkg CDN 上的全局(“UMD”)构建。 Simply add the following
<script>
tag to the bottom of your HTML file:只需将以下
<script>
标记添加到 HTML 文件的底部:
<script src="//unpkg.com/styled-components/dist/styled-components.min.js"></script>
Once you've added
styled-components
you will have access to the globalwindow.styled
variable.添加
styled-components
后,您将可以访问全局window.styled
变量。
const Component = window.styled.div`
color: red;
`
source: https://www.styled-components.com/docs/basics#installation来源: https ://www.styled-components.com/docs/basics#installation
UPDATE FROM OP:来自 OP 的更新:
Newer versions has stopped working (probably v5) from the URL above.较新的版本已从上面的 URL 停止工作(可能是 v5)。 But it's still working like this on v4:
但它在 v4 上仍然像这样工作:
<script src="//unpkg.com/styled-components@4.0.1/dist/styled-components.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.