如何在Web组件中使用Material Design图标?

詹姆士

看起来MDI无法在Web组件内部运行,还是我错过了什么?

我想开发一个封装其依赖项的Web组件,将链接添加到父文档中,但是它违反了最初的意图。

<html>
<body>
<x-webcomponent></x-webcomponent>
<script>
customElements.define(
  "x-webcomponent",
  class extends HTMLElement {
    constructor() {
      super();
      this.attachShadow({ mode: "open" });
      this.shadowRoot.innerHTML = `
        <style>@import url('https://cdn.materialdesignicons.com/4.9.95/css/materialdesignicons.min.css');</style>
        <span class="mdi mdi-home"></span>
      `;
    }
  }
);
</script>
</body>
</html>

https://codepen.io/Jamesgt/pen/MWwvJaw

超锐利

@font-face您要使用的字体CSS规则必须在主文档中声明,而不是在Shadow DOM中声明。

因为在您的情况下,它是在materialdesignicons.min.css文件中定义的,所以您需要通过global将其加载到主文档中<link>

请注意,由于浏览器的缓存,CSS文件不会被加载两次。

或者,您可以将其添加到Web组件的轻型DOM中,也可以只声明@font-face规则(从materialdesignicons.css文件复制)。

这是一个正在运行的示例:

customElements.define( "x-webcomponent", class extends HTMLElement {
    constructor() {
      super()
      this.attachShadow({ mode: "open" })
      this.shadowRoot.innerHTML = `
        <link rel=stylesheet  href=https://cdn.materialdesignicons.com/4.9.95/css/materialdesignicons.min.css>
        <span class="mdi mdi-home"></span>`
    }
    connectedCallback () {
      this.innerHTML = `<style>
          @font-face {
            font-family: "Material Design Icons";
            src: url("https://cdn.materialdesignicons.com/4.9.95/fonts/materialdesignicons-webfont.woff?v=4.9.95") format("woff");
          }
       </style>`
    }
} )
<x-webcomponent></x-webcomponent>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Angular 4中使用Material Design图标?

如何在Android App中使用Material Design?

如何使用TabLayout Android Design使用图标创建应用栏?

在css pseudo中添加Google Material Design图标:after内容

如何在Android的XML中使用Material Design图标?

如何在Bootstrap 4中使用Material Design

如何在React中使用Material UI图标?

如何离线托管Material Design图标?

如何在Android Studio 3.2.1最新版本中导入Google Material Design图标?

如何在Ant Design中设置图标的大小?

如何在ant-design react app中使用ant-design-pro组件?

如何在HTML画布上渲染图标字体,尤其是在Material Design图标字体上?

在FloatingActionButton中使用Material design图标库

使用TypeScript和Formik反应如何在Textfield Material-UI中使用图标

如何在React样式的组件中使用自定义图标?

Android Material Design导航抽屉菜单图标变化

如何使用Material Design CSS在“选择”或“多选”表单元素上添加图标前缀?

在Angular中使用时缺少Material Design Lite图标

Material Design图标字体未在Google Chrome 53上加载

如何在android studio项目中使用Material Design

Material Design 风格的图标如何基于文本节点子节点

如何在 Nuxt.js 中使用 Material.io 中的 Material Design 组件

Android 中图标的 Material Design 灰色是什么?

如何在角度组件中使用字体真棒品牌图标?

如何在 V4 中使用 Ant-design/icons 中的图标

如何使用条件语句在 Vue JS 中切换 Material Design 图标?

React Material Design 图标悬停无法正常工作

如何在 Primefaces 组件中使用 Material Icons 图标

如何在 Vaadin Flow (14.6) 中使用 Material 图标