Angular 6库共享样式表 [英] Angular 6 library shared stylesheets
问题描述
如何设置index.scss并将变量,混入等的全局样式表导入到angular 6库中?
Angular CLI生成具有根组件&的库.组件scss,但是添加或导入到根组件的样式不适用于子组件.默认情况下,封装样式是有道理的,但是我还没有找到任何有关如何设置样式的信息或示例.
"projectType": "application"
可以用于此的angular.json "styles": [...]
路径,似乎也不能与"projectType": "library"
一起使用.
提前感谢您的帮助!
更新:我的项目是按照以下指南使用angular cli v6.0.5启动的: 打包版本:
对于全局样式,我已经在此问题. 对于 现在直接支持将资产复制到输出文件夹,如此页面 对于其他版本 因此,
或组件scss文件所在的相对路径.
现在,非常重要,请勿使用 在应用程序项目的scss文件中包含
现在您已经在所有安装库的项目中拥有所有库mixins. 干杯! PS 变通办法不是最优雅的解决方案,但是当其他任何方法都无法解决时,它们就会变通! How can you setup a index.scss and import global stylesheets for variables, mixins, etc, to an angular 6 library? Angular CLI generates a lib with a root component & component scss, but the styles added or imported to the root component are not available to children components. Which makes sense by default to encapsulate the styles, but I just can't find any information or examples on how to set this up yet. The angular.json Thanks in advance for your help! UPDATE: My project was initiated using angular cli v6.0.5, following this guide: https://medium.com/@tomsu/how-to-build-a-library-for-angular-apps-4f9b38b0ed11 TL;DR for the guide: This is the file structure angular 6 generates: Package Versions:
For global styles, I've answered it in this question. For Copying assest to output folder is now directly supported as explained in this page For other versions So,
or whatever relative path your component scss file is at.
Now, very important, DO NOT use Include the
Now you have all your library mixins in all of the projects you installed your library. Cheers! PS Workarounds are not the most elegant solutions, but when nothing else works, they work around! 这篇关于Angular 6库共享样式表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! Angular CLI: 6.0.5
Node: 10.2.1
OS: darwin x64
Angular: 6.0.3
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
Package Version
------------------------------------------------------------
@angular-devkit/architect 0.6.5
@angular-devkit/build-angular 0.6.5
@angular-devkit/build-ng-packagr 0.6.5
@angular-devkit/build-optimizer 0.6.5
@angular-devkit/core 0.6.5
@angular-devkit/schematics 0.6.5
@angular/cli 6.0.5
@ngtools/json-schema 1.1.0
@ngtools/webpack 6.0.5
@schematics/angular 0.6.5
@schematics/update 0.6.5
ng-packagr 3.0.0
rxjs 6.2.0
typescript 2.7.2
webpack 4.8.3
更新
ng-packgr
9.x及更高版本 {
"ngPackage": {
"assets": [
"CHANGELOG.md",
"./styles/**/*.theme.scss"
],
"lib": {
...
}
}
}
旧答案
index.scss
文件.如果您遵循来自Angular的本指南,则您的路径将为my-project/projects/my-library/index.scss
.这也是package.json
所在的文件夹.index.scss
将是包含变量和mixins的文件
$grey: #222;
@mixin mymixin {
background: #222;
}
import
@import '../../index.scss';
package.json
文件(不是库).
{
"name": "my-project",
"version": "1.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build && npm run copyScss",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"copyScss": "xcopy \"projects\my-library\index.scss\" \"dist\\my-library\\\""
},
...
}
ng build
来构建您的库,而应使用npm run build
.这将自动执行复制命令.现在,index.scss
文件将与您的库一起导出到my-project/dist
文件夹中.index.scss
// ~ stands for the node_modules folder
@import '~my-library/index.scss';
"styles": [...]
paths that can be used for this with "projectType": "application"
, don't seem to work with "projectType": "library"
either.
ng new my-app --style=scss
ng generate library my-library --prefix ml
my-app
projects/
my-library/
src/
lib/
shared/..
widgets/..
my-library.component.ts
my-library.module.ts
sass/
_variables.scss
styles.scss // <<< This is where I want to `@import 'variables';`, and for it to be available in all the components of the "my-library" project.
public_api.ts
src/
app/
app.module.ts // << imports projects/my-library/lib/my-library.module as "my-library".
main.ts
index.scss
index.html
README.md
Angular CLI: 6.0.5
Node: 10.2.1
OS: darwin x64
Angular: 6.0.3
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
Package Version
------------------------------------------------------------
@angular-devkit/architect 0.6.5
@angular-devkit/build-angular 0.6.5
@angular-devkit/build-ng-packagr 0.6.5
@angular-devkit/build-optimizer 0.6.5
@angular-devkit/core 0.6.5
@angular-devkit/schematics 0.6.5
@angular/cli 6.0.5
@ngtools/json-schema 1.1.0
@ngtools/webpack 6.0.5
@schematics/angular 0.6.5
@schematics/update 0.6.5
ng-packagr 3.0.0
rxjs 6.2.0
typescript 2.7.2
webpack 4.8.3
Update
ng-packgr
versions 9.x and above{
"ngPackage": {
"assets": [
"CHANGELOG.md",
"./styles/**/*.theme.scss"
],
"lib": {
...
}
}
}
Old Answer
index.scss
file in your library's root folder. If you follow this guide from Angular, then your path will be my-project/projects/my-library/index.scss
. This is also the folder where your package.json
is.index.scss
will be the file with your variables and mixins$grey: #222;
@mixin mymixin {
background: #222;
}
import
@import '../../index.scss';
package.json
file (NOT THE LIBRARY'S).{
"name": "my-project",
"version": "1.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build && npm run copyScss",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"copyScss": "xcopy \"projects\my-library\index.scss\" \"dist\\my-library\\\""
},
...
}
ng build
to build your library, instead use npm run build
. This will automatically execute the copy command. Now the index.scss
file is exported along with your library in the my-project/dist
folder.index.scss
in your app project's scss files// ~ stands for the node_modules folder
@import '~my-library/index.scss';