[英]How to use stencil with third-party css library
我第一次嘗試使用stenciljs 。 我想構建一個小應用程序,而不僅僅是一個可重用的 Web 組件。
我的問題是是否有可能向我的應用程序添加第三方 CSS 庫,如Bootstrap 、 Skeleton或Bulma 。
我嘗試了以下操作,但似乎都不起作用:
簡單地在index.html
添加 CDN <link rel="stylesheet" href="link-here">
是行不通的。
我使用npm
安裝了 Bulma CSS 庫,並嘗試將其導入app.css
文件中,如下所示:
@import "../../node_modules/bulma/css/bulma.min.css";
或者
@import url("https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.1/css/bulma.min.css");
文檔說app.css
用於全局樣式。 但是這個方法也不管用。
如何將第三方 CSS 庫添加到我的模板項目中?
可能有更好的方法,但解決此問題的快速方法是在每個組件 css 文件中導入第三方 CSS 庫。
@import url("https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.1/css/bulma.min.css");
除了 JiiB 的解決方案之外,這是設置全局app.css
樣式表的方法:
src
目錄中創建app.css
文件,例如在/src/global/app.css
globalStyle: 'src/global/app.css'
的config
在目標/stecnil.config.ts
將/build/app.css
中生成的 css 文件/build/app.css
在/src/index.html
:
<link href="/build/app.css" rel="stylesheet">
我使用全局樣式的解決方案是避免使用 shadow-dom。 通過將scoped=true
傳遞給Component
裝飾器來做到這一點。
@Component({
tag: "my-component",
styleUrl: "my-component.css",
scoped: true, // <- This makes sure the component doesn't have native encapsulation
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.