【问题标题】:How can we use the CDN URL mechanism for package management if we use Typescript?如果我们使用 Typescript,我们如何使用 CDN URL 机制进行包管理?
【发布时间】:2018-12-01 10:29:14
【问题描述】:

我需要使用可以通过CDN URL指向机制解析的包,见Material Icon package installation here,如下所示

<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">

但是,当我想在我的 Typescript 中使用上述包中的图标时,

import MenuIcon from '@material-ui/icons/Menu';

它告诉我以下错误

Module not found: Can't resolve '@material-ui/icons/Menu' 

我知道我可以通过npm install对应的包解决包丢失问题(建议像这样https://github.com/mui-org/material-ui/issues/10571),但我想知道基于CDN的包管理是否可以用于Typescript。

【问题讨论】:

    标签: reactjs typescript npm material-ui


    【解决方案1】:

    如果你想用这个来加载图标:

    <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
    

    那么你只能使用这样的图标:

    import Icon from '@material-ui/core/Icon';
    
    // To use an icon simply wrap the icon name (font ligature) with the Icon component:
    <Icon>star</Icon>
    

    阅读这些说明:https://material-ui.com/style/icons/#font-icons

    这与 Typescript 没有任何关系。


    如果您想导入单个图标 (import MenuIcon from '@material-ui/icons/Menu';),那么您确实需要:

    npm install @material-ui/icons
    

    【讨论】:

      猜你喜欢
      • 2018-09-03
      • 1970-01-01
      • 2018-02-08
      • 1970-01-01
      • 2022-06-25
      • 1970-01-01
      • 2021-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多