如何将CSS类应用于AngularDart中的另一个组件?



假设有一个简单的框架来显示弹出窗口:

@Component(
selector: 'popup-host',
template: '''
<div class="popup-container">
<ng-template #popupRef></ng-template>
</div>
''',
styles: ['.popup-container { position: absolute; top: 100; left: 100; z-index: 100; }'],
)
class PopupContainerComponent {
final PopupController _controller;
final ComponentLoader _loader;
PopupContainerComponent(this._controller, this._loader);
void ngOnInit() {
_controller.container = this;
}
@ViewChild('popupRef', read: ComponentRef)
ComponentRef popupRef;
void render(PopupConfig config) {
final componentRef = _loader.loadNextTo(config.factory, popupRef);
if (componentRef.instance is HasValueSetter) {
componentRef.instance.value = config.value;
}
}
}
@Injectable()
class PopupController {
PopupContainerComponent _container;
set container(PopupContainerComponent container) => _container = container;
void showPopup(PopupConfig config) {
container.render(config);
}
...
}
class PopupConfig {
final ComponentFactory factory;
final dynamic value;
PopupConfig(this.factory, [this.value]);
}
abstract class HasValueSetter {
set value(dynamic value);
}

然后可以像这样使用:

// Somewhere in the root template
<popup-host></popup-host>
// In popup.dart
@Component(
selector: 'happy-popup',
template: '''
<div class="header">This is the popup content.</div>
<div class="main">The value is {{value}}.</div>
<div class="footer">I am happy!</div>
''',
)
class HappyPopupComponent implements HasValueSetter {
@override
dynamic value;
}
// In some_other.dart
@Component(
...
styles: [
'.header { font-weight: bold }',
'.main { color: red }',
'.footer { color: green; font-style: italic }',
],
...
)
class SomeOtherComponent {
final PopupController _popupController;
...
SomeOtherComponent(this._popupController, ...) ...;
void displayPopup() {
_popupController.showPopup(HappyPopupComponentNgFactory, 42);
}
}
...

有没有办法将样式从<some-other-component>转发到<happy-popup>,而无需在应用程序的根目录中定义它们?

您可以通过将组件代码拆分为单独的文件来实现此目的 - 或者在您的情况下将单独的 CSS 文件拆分。

与其直接在组件的 -styles中编写样式,不如使用styleUrls导入 CSS 文件。这样,您就可以传递带有样式的文件,并且可以在多个组件之间共享该文件。

@Component(
styleUrls: ['./hero1.css', './folder/hero2.css'],
)

请记住,styleUrls 中的 URL 是相对于组件的。

使用styleUrls导入 css 的另一个好处是,它还使您能够在该文件中使用导入。

英雄1.css

@import './folder/hero2.css';

仅供参考:将组件代码拆分为单独的组件代码是一种常见的做法 文件。

  • 英雄飞镖
  • 英雄.css
  • 英雄.html

然后在您的飞镖文件中将它们引用为:

@Component(
templateUrl: './hero.html',
styleUrls: ['./hero.css'],
)

请参考AngularDart - 组件样式 了解这方面的简要信息。

由于您的弹出窗口不是打开它的组件的子项,因此您不能使用 ::ng-deep

我认为唯一可行的是从主机、弹出窗口和打开弹出窗口的组件中删除视图封装(仅尝试弹出窗口和首先打开弹出窗口的组件,如果这不起作用,请删除主机的封装。

@Component(
selector: 'happy-popup',
template: '''
<div class="header">This is the popup content.</div>
<div class="main">The value is {{value}}.</div>
<div class="footer">I am happy!</div>
''',
encapsulation: ViewEncapsulation.None // <=== no encapsulation at all
)
class HappyPopupComponent implements HasValueSetter {

您可以将 scss 与代码结合使用

首先,您需要分离要在应用程序中共享的scss文件

例如:

在风格1.scss

.header { font-weight: bold }

然后在风格2.scss

@import "style1"

或者您可以通过在数组列表中定义来组合组件代码中的scss文件列表

styleUrls: ['./style1.scss', './style2.scss'],
  • 注意:请根据您的文件路径更改路径,这也仅在您使用 scss 而不是 css 时才有效

这是您可以手动配置 scss 对角度 cli 的支持的方法

在 angular.json 中添加

"projects": {
"app": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": { // add this config
"@schematics/angular:component": {
"style": "scss"
}
},

您可以将 prop 值发送到组件,该组件可以是自定义类并将其放入您的 popop html 中。然后在scss文件中为特定类添加额外的css覆盖。因此,对于每个自定义组件,您可以拥有自定义 css 代码。

PS:是的,我建议导入scss文件,例如:

@Component(
styleUrls: ['./hero1.css'],
)

最好将css 与 js 分开 + 您的 css 代码,然后可以更长,包含所有样式情况。

除了已经提到的方法之外,我还建议探索另外两种途径:

由于 Angular 将 CSS 范围限定为组件,
  1. 我们希望保持这种方式,通过找出 Angular 分配给它的范围并将手动作用域的 CSS 添加到页面上的全局<style>标记中,可以完成交叉组件边界:

    @ViewChild('popupRef'( popupRef; ngAfterViewInit(( { this.popupRef.nativeElement.attributes[0].name//这将具有类似于_ngcontent-tro-c1的值,您需要使用它来限定所有自定义 CSS 的范围。 }

这种方法的一个明显缺点是 Angular 隐藏了 CSS 管理,因此您必须求助于普通 JS 来管理它。(这里有一个例子(

  1. 您可以尝试在创建 @Component 中定义 CSS,利用自定义装饰器工厂,如本答案所示

就个人而言,我会探索第二种选择,因为它似乎不那么笨拙

最新更新