【问题标题】:Input Value Change causes font import输入值更改导致字体导入
【发布时间】:2019-03-23 00:56:41
【问题描述】:

我正在使用 reactjs 和 styled-components。 我得到了一个包含 styled.input 的 InputComponent。 我的父组件包含这个 Input 组件、一个 styled.h1 const 和一个按钮组件。

我的父渲染如下所示:

  render() {
    return (
      <Div>
        <GlobalStyle/>
        <H1>Hallo</H1>
        <MyInput value={this.state.value} onChangeValue={this.handleChangeValue} />
        <br/><MyButton msg={this.state.value}/>
      </Div>
    );
  }

也许你注意到了 GlobalStyle 组件。 GlobalStyle 组件来自 styled-components 并提供可继承的样式。 它看起来像这样:

const GlobalStyle = createGlobalStyle `
  body {
    @import url('https://fonts.googleapis.com/css?family=Lato');
    font-family: 'Lato',sans-serif;
  }
`

我的问题是: 字体最初可以正常工作,但是当我在输入字段中输入内容时,它会重新下载字体并重置我的 h1。 Live it 看起来像是变得粗体和粗体,但它只是在改变字体。

几个字母后,我的网络调试看起来像这样:Network image

感谢您帮助我。

项目https://codesandbox.io/s/github/robertdudaa/reactjstest

【问题讨论】:

    标签: javascript html css reactjs styled-components


    【解决方案1】:

    你可以试试:

    const GlobalStyle = createGlobalStyle `
     @import url('https://fonts.googleapis.com/css?family=Lato');
      body {
        font-family: 'Lato',sans-serif;
      }
    `
    

    或者您可以简单地在项目目录的公共文件夹中的 index.html 中导入字体:

    &lt;link href="https://fonts.googleapis.com/css?family=css?family=Lato" rel="stylesheet" type='text/css'&gt;

    希望对你有帮助 谢谢你:)

    【讨论】:

    • 嘿,感谢您的帮助,但两种解决方案都不起作用。
    • 哇..你能把你的文件上传到here
    • 它超级奇怪,有时有效,有时无效codesandbox.io/s/github/robertdudaa/reactjstest
    • 我没有找到您在此处应用的任何东西是您的 public html 和 here 是您的 css 您可以在公共文件夹的 index.html 文件中添加 &lt;link href="https://fonts.googleapis.com/css?family=css?family=Lato" rel="stylesheet" type='text/css'&gt; 并添加 @ 987654328@ 在你的 CSS 中
    猜你喜欢
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 2016-04-22
    • 2016-12-29
    • 2020-04-09
    • 2016-11-05
    • 2018-08-11
    相关资源
    最近更新 更多