今天在react项目中,style.css中使用import { injectGlobal } from 'styled-components',发现样式不起作用了,在网上找了半天文档,才发现了具体原因:The injectGlobal API was removed and replaced by createGlobalStyle in styled-components v4. 用官方的话来讲,就是这个API 从现在开始废除了,换成 createGlobalStyle 新的API ,作为一个样式组件出现,按照样式组件思想,以一个标签形式被引入。
具体解决方式:
1. 用createGlobalStyle定义全局样式
import { createGlobalStyle } from 'styled-components'
export const Globalstyle = createGlobalStyle`
body{
margin:0;
padding:0;
font-family:sans-serif;
background:green;
}
`
2.在项目主文件导入样式
import { Globalstyle } from './style';
3.以样式组件的方式当作标签引入
import { Globalstyle } from './style';
render() {
return (
<div>
<Globalstyle/>
</div>
);
如此设置,样式问题得到解决。