Skip to content

React 框架中使用 symbol引用 出错 #1631

@condorheroblog

Description

@condorheroblog

同样的方式在 Vue 框架中没事,效果非常理想,但在 React 中当我试图 import "./iconfont/iconfont.js" JS 文件的时候,出现以下错误:

Failed to compile
./src/iconfont/iconfont.js
  Line 1:1:       Expected an assignment or function call and instead saw an expression  no-unused-expressions
  Line 1:256489:  Expected an assignment or function call and instead saw an expression  no-unused-expressions
  Line 1:256503:  Expected an assignment or function call and instead saw an expression  no-unused-expressions
  Line 1:256562:  Expected an assignment or function call and instead saw an expression  no-unused-expressions
  Line 1:256972:  Expected an assignment or function call and instead saw an expression  no-unused-expressions
  Line 1:257259:  Expected an assignment or function call and instead saw an expression  no-unused-expressions

Search for the keywords to learn more about each error.

使用的是最新的 create-react-app 16.13 版本建的项目:

Activity

condorheroblog

condorheroblog commented on Jun 10, 2020

@condorheroblog
Author

看官网的回答率,估计不可能被翻牌子了,还是自问自答吧😂

在 React 的组件中无法使用,但是在 html 文件是可以使用的,那我们就从项目的 index.html 下手,只需要在这个文件里面按教程配置下就OK了。

具体点就是把在 阿里妈妈 (iconfont)下载的文件包,放在项目 index.html 文件所在目录,即 public 文件夹下。然后按教程根据你的情商配置:

注意⚠️在 React 组件中使用的时候,xlink:href 替换成 xlinkHref

<svg className="icon" aria-hidden="true">
	<use xlinkHref="#shanshu-business-user"></use>
</svg>

看到的,不用谢。😄

tthallos

tthallos commented on Jul 9, 2020

@tthallos

加个

// eslint-disable-next-line

就可以了

yang-builds

yang-builds commented on Apr 12, 2021

@yang-builds

哎 这就比较麻烦咯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @tthallos@yang-builds@condorheroblog

        Issue actions

          React 框架中使用 `symbol引用` 出错 · Issue #1631 · thx/iconfont-plus