React的reset.css无法引入和injectGlobal图标不能显示解决办法

自学React过程中遇到的坑解决办法

如:reset.css引入报错问题:
React的reset.css无法引入和injectGlobal图标不能显示解决办法_第1张图片

Failed to compile
./src/App.js
Attempted import error: 'GlobalStyle' is not exported from './style'.
import {injectGlobal} from 'styled-components';

injectGlobal`
    html, body, div, span, applet, object, iframe,
    h1, h2, h3, h4, h5, h6, p, blockquote, pre,
    a, abbr, acronym, address, big, cite, code,
    del, dfn, em, img, ins, kbd, q, s, samp,
    small, strike, strong, sub, sup, tt, var,
    b, u, i, center,
    dl, dt, dd, ol, ul, li,
    fieldset, form, label, legend,
    table, caption, tbody, tfoot, thead, tr, th, td,
    article, aside, canvas, details, embed, 
    figure, figcaption, footer, header, hgroup, 
    menu, nav, output, ruby, section, summary,
    time, mark, audio, video {
        margin: 0;
        padding: 0;
        border: 0;
        font-size: 100%;
        font: inherit;
        vertical-align: baseline;
    }
    /* HTML5 display-role reset for older browsers */
    article, aside, details, figcaption, figure, 
    footer, header, hgroup, menu, nav, section {
        display: block;
    }
    body {
        line-height: 1;
    }
    ol, ul {
        list-style: none;
    }
    blockquote, q {
        quotes: none;
    }
    blockquote:before, blockquote:after,
    q:before, q:after {
        content: '';
        content: none;
    }
    table {
        border-collapse: collapse;
        border-spacing: 0;
    }
`;

解决办法:使用createGlobalStyle这种引用但是好像不能生效

import {createGlobalStyle} from 'styled-components';

createGlobalStyle`

正确的解决办法

import {createGlobalStyle} from 'styled-components';

export const GlobalStyle = createGlobalStyle`
    html, body, div, span, applet, object, iframe,
    h1, h2, h3, h4, h5, h6, p, blockquote, pre,
    a, abbr, acronym, address, big, cite, code,
    del, dfn, em, img, ins, kbd, q, s, samp,
    small, strike, strong, sub, sup, tt, var,
    b, u, i, center,
    dl, dt, dd, ol, ul, li,
    fieldset, form, label, legend,
    table, caption, tbody, tfoot, thead, tr, th, td,
    article, aside, canvas, details, embed, 
    figure, figcaption, footer, header, hgroup, 
    menu, nav, output, ruby, section, summary,
    time, mark, audio, video {
        margin: 0;
        padding: 0;
        border: 0;
        font-size: 100%;
        font: inherit;
        vertical-align: baseline;
    }
    /* HTML5 display-role reset for older browsers */
    article, aside, details, figcaption, figure, 
    footer, header, hgroup, menu, nav, section {
        display: block;
    }
    body {
        line-height: 1;
    }
    ol, ul {
        list-style: none;
    }
    blockquote, q {
        quotes: none;
    }
    blockquote:before, blockquote:after,
    q:before, q:after {
        content: '';
        content: none;
    }
    table {
        border-collapse: collapse;
        border-spacing: 0;
    }
`

在入口文件以标签的形式引入就可以了

import React, {Component} from 'react';
import Header from './common/header/index';
import {GlobalStyle} from './style';

class App extends Component{
    render(){
        return (
            

现在是React引入iconfond图标不能显示问题。
React的reset.css无法引入和injectGlobal图标不能显示解决办法_第2张图片
能搜到这的应该react的iconfond图标会使用了,那就不教如何使用了啊。我就直接上我的代码了。
先将iconfont.css改成iconfont.js,至于为什么css文件一旦在一个文件引用将会全局生效,就会导致样式不好控制,就会用到styled-components.

import { createGlobalStyle } from 'styled-components';

export const GlobalIcon = createGlobalStyle`
    @font-face {font-family: "iconfont";
      src: url('./iconfont.eot?t=1561086646569'); /* IE9 */
      src: url('./iconfont.eot?t=1561086646569#iefix') format('embedded-opentype'), /* IE6-IE8 */
      url('data:application/x-font-woff2;charset=utf-8;base64,d09GMgABAAAAAAOwAAsAAAAAB8wAAANhAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHEIGVgCDHAqDDIJpATYCJAMQCwoABCAFhG0HQRvUBsgehXEbHUKqVFb2l048/Ltfdl9+ZpoSKlAMlj2QJ7I9NYqMqPE1mnYlG9H/y6t+0INk1yPUSq0BnSwUVHoyjeqXrrVv4lnUCb6BJ8b//xwzXVqD5bfZXKLSup6P9gY4bh1YdGjcAd+BnKDfB2zoflYTaDJoJGarsr4dImRmp0DcmR4FIjI6uSE9FKFeMTJDvIBNSSvYO8Bz8PvxHxYiAiS1CsyeOxcVOhT/Svx4n5v/f77xHgL401nAj6PCFJCJq0rbCS4KTOE0qfV5K0BRJKaqyvx4/z8JjiIr1r88QiURdTBTW8ASBSt+JTpMgl+ZvCr5fUyR4IMoCibdA69g4vrtmiS5o29Mt4iIaHHaug2x8ob1MdL6tVHta9dFj9+QxY7Tjl5bnDs2JMTsf848eyY9edL+9GnHqud50dCxr4rk589ltnGDnoLUrV3wimPGrk8RtsaNVk6LqwCaQrggrLzHLJDlBcH3ye84sgPm7xRXXWaY7QtLFg4YLkcNdQezXKujRsh9h5X4hQGzBtzaGDW0lGSWLoy6tT4qn0dvIqfC+kHo9IN+kosNrF4dYAnrSViOjbMR1in1SzmDbtw4c/JGA+jnTANTDxyXzGAsbsAXwwWj6uHQ8oe4x3+17FlXYVMHW6qahc/534UhejXbwdhK7GSYZHafINYV576fKNPb8dG/EwZ9G5QCQeYm6Sc3AoDqmH0DvrkL3I/7G9uPu22Teub/ihB5gHe391jg+OZhmKLciz34TfyOWVkTmvqcFZHD0+3ZXKqThCZN+DkRurycQtHbOZVQ9CaQNBiASjGEzNgpqGm2BHXFKjSZVDW+WTcLJ3IkMGEJQOiwH5I2b1DpcBeZsS+gps8nqOsIDpocRd8Zm42EKZRZWKFYR+oMZGokYNhGKoTprdgzxadYaWFAkbAVMt3IZXfmw1U4gK0p+oSmetyUGsiwiB9VwnrY5yMoaJEurFF7J6XBXIfDqHqSXSN+EGyyYAoK0yGqGRCThggwvPyUUPj5VpjHFD4KqyGjyirBLCFm7YiLnbMDskoW6JRxK+eETOXhRlEGxGAh/JBKsGA+K0AgwepmXTANZdc5wBeUy0G5jK4y+/IK/xsugCbmwTlYhmN4xgZND9hUTYVQ02VTTLcrhy1RAAAAAAA=') format('woff2'),
      url('./iconfont.woff?t=1561086646569') format('woff'),
      url('./iconfont.ttf?t=1561086646569') format('truetype'), /* chrome, firefox, opera, Safari, Android, iOS 4.2+ */
      url('./iconfont.svg?t=1561086646569#iconfont') format('svg'); /* iOS 4.1- */
    }
  
    .iconfont {
      font-family: "iconfont" !important;
      font-size: 16px;
      font-style: normal;
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
    }
`;

在以标签的形式引入就可以了

import React, {Component} from 'react';
import Header from './common/header/index';
import {GlobalStyle} from './style';
import { GlobalIcon } from './statics/iconfond/iconfont';

class App extends Component{
    render(){
        return (
            
) } } export default App;

React的reset.css无法引入和injectGlobal图标不能显示解决办法_第3张图片
这样就可以正确的显示了,

我是一个React小白,如果那位大佬有更好的方法和建议欢迎交流

你可能感兴趣的:(学习总结,React)