how to import a css file from an npm module - webcomponent

*爱你&永不变心* 提交于 2019-12-11 04:41:32

问题


I'm trying to use MDC Components for material design components on my application. I have a custom element in Polymer (LitElement):

_render(props) {
        return html`
            ${SharedStyles}
            <style>
                .js-panel {
                    display: none;
                }
                .js-panel.is-active {
                    display: block;
                }
            </style>

            <div class="mdc-toolbar">
                <div class="mdc-toolbar__row">
                    <div class="mdc-toolbar__section mdc-toolbar__section--align-start">
                        <nav id="tab1" class="mdc-tab-bar mdc-tab-bar--indicator-accent  js-tabs" role="tablist">
                            <a role="tab" aria-controls="panel-1" class="mdc-tab mdc-tab--active">Item One</a>
                            <a role="tab" aria-controls="panel-2" class="mdc-tab">Item Two</a>
                            <span class="mdc-tab-bar__indicator"></span>
                        </nav>
                    </div>
                </div>
            </div>
            <section>
                <div class="js-panels" for='tab1'>
                    <p class="js-panel is-active" role="tabpanel" aria-hidden="false">Item One</p>
                    <p class="js-panel" role="tabpanel" aria-hidden="true">Item Two</p>
                </div>
            </section>
        `
    }

I have imported from @material/tabs all the classes needs to create the component, and it actually works perfectly fine but the styles doesn't apply:

In the dist folder we have the css file mdc.tabs.css that has all the css for the component. I did the test copying all the file content into the style tag and it applies all the styles correctly, but I think it should be a better way to do that... My question is how can I import the css file into my webcomponent?


回答1:


You can always use the traditional approach when you render() your html:

<link href="./mdc.tabs.css" rel="stylesheet">



来源:https://stackoverflow.com/questions/50518495/how-to-import-a-css-file-from-an-npm-module-webcomponent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!