xau*_*pez 23 libraries angular-cli angular angular6 angular-cli-v6
如何设置index.scss并将变量,mixins等的全局样式表导入角度6库?
Angular CLI生成具有根组件和组件scss的lib,但添加或导入到根组件的样式不可用于子组件.默认情况下,封装样式是有道理的,但我找不到任何关于如何设置它的信息或示例.
"styles": [...]可以用于此的angular.json 路径"projectType": "application"似乎"projectType": "library"也不适用.
在此先感谢您的帮助!
更新:我的项目是使用angular cli v6.0.5启动的,遵循本指南:https://medium.com/@tomsu/how-to-build-a-library-for-angular-apps-4f9b38b0ed11
TL; DR指南:
ng new my-app --style=scss
ng generate library my-library --prefix ml
Run Code Online (Sandbox Code Playgroud)
这是angular 6生成的文件结构:
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
Run Code Online (Sandbox Code Playgroud)
包版本:
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
Run Code Online (Sandbox Code Playgroud)
Nei*_*rao 15
对于全局样式,我已经在这个问题中回答了。
对于ng-packgr9.x 及以上版本
在解释现在直接支持复制入资产到输出文件夹此页
{
"$schema": "./node_modules/ng-packagr/package.schema.json",
"name": "@my/library",
"version": "1.0.0",
"ngPackage": {
"assets": [
"CHANGELOG.md",
"./styles/**/*.theme.scss"
],
"lib": {
...
}
}
}
Run Code Online (Sandbox Code Playgroud)
index.scss在库的根文件夹中创建一个文件。如果您遵循Angular 的本指南,那么您的路径将是my-project/projects/my-library/index.scss. 这也是您所在的文件夹package.json。所以,index.scss将是你的变量和混合文件
{
"$schema": "./node_modules/ng-packagr/package.schema.json",
"name": "@my/library",
"version": "1.0.0",
"ngPackage": {
"assets": [
"CHANGELOG.md",
"./styles/**/*.theme.scss"
],
"lib": {
...
}
}
}
Run Code Online (Sandbox Code Playgroud)
import$grey: #222;
@mixin mymixin {
background: #222;
}
Run Code Online (Sandbox Code Playgroud)
或您的组件 scss 文件所在的任何相对路径。
package.json文件(而不是库的)。@import '../../index.scss';
Run Code Online (Sandbox Code Playgroud)
现在,非常重要的是,不要用于ng build构建您的库,而是使用npm run build. 这将自动执行复制命令。现在index.scss文件与my-project/dist文件夹中的库一起导出。
将 包含index.scss在您的应用程序项目的 scss 文件中
{
"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\\\""
},
...
}
Run Code Online (Sandbox Code Playgroud)
现在,您在安装库的所有项目中都拥有所有库 mixin。
干杯!
PS变通方法不是最优雅的解决方案,但是当其他方法无效时,它们会变通!
Arn*_*kie -1
在项目上运行ng init,以便它将项目初始化为 Angular CLI 项目。
编辑:
我的错,他们似乎init从 CLI 中删除了。你可以试试ng new --src
它可能会覆盖某些文件,因此请在项目的副本上尝试此操作。
| 归档时间: |
|
| 查看次数: |
6905 次 |
| 最近记录: |