Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 特定于平台的样式不会从构件样式继承_Angular_Nativescript_Angular2 Nativescript - Fatal编程技术网

Angular 特定于平台的样式不会从构件样式继承

Angular 特定于平台的样式不会从构件样式继承,angular,nativescript,angular2-nativescript,Angular,Nativescript,Angular2 Nativescript,当特定于平台的CSS文件发挥作用时,我对样式有问题 请参阅以下文件: items.component.ts: @Component({ selector: 'ns-items', moduleId: module.id, template: ` <Label [nsRouterLink]="['/item', item.id]" [text]="item.name" class="list-group-item"></Label

当特定于平台的CSS文件发挥作用时,我对样式有问题

请参阅以下文件:

items.component.ts:

@Component({
  selector: 'ns-items',
  moduleId: module.id,
  template: `
      <Label [nsRouterLink]="['/item', item.id]" [text]="item.name"
             class="list-group-item"></Label>
  `,

  styleUrls: ['./items.component.css' ]
})
export class ItemsComponent implements OnInit {
  items: Item[];

  constructor(private itemService: ItemService) {
  }

  ngOnInit(): void {
    this.items = this.itemService.getItems();
  }
}
items.component.ios.css:

Label {
    background-color: red;
}
items.component.android.css为空

在iOS上,它的行为与预期相同,
标签的背景色为红色。
在Android上,
标签
的背景色没有任何变化。我所期望的行为是,它继承了
items.component.css
的样式,因此
标签的背景色应该是蓝色,而不是蓝色

我试图在
items.component.android.css
中添加
@import items.component.css
,但随后出现
超过最大调用堆栈大小的错误,因为我正在声明组件中已经存在的css


让组件全局CSS和从组件全局CSS继承的平台特定CSS的正确方法是什么?还是我做错了什么?

组件全局应该是什么意思?使用ViewEncapsulation.Emulated(默认设置)和.Native(浏览器的本机方式与Emulated相同,但只有少数浏览器实现了它;完全没有使用!),组件中的样式仅应用于组件

除非您在css中使用
:host::ng deep
选择器,否则在这种情况下,它们也应用于底层子组件。但不建议这样做,因为有一天Chrome会不推荐使用此功能,所以如果确实需要,您应该使用@Input()将样式/配置传递给底层组件

如果需要全局样式,可以在全局资源文件夹中声明它们,并将其作为资源包含在angular-cli.json中

或者,您可以为一个特定组件设置ViewEncapsulation.None,该组件的样式将泄漏到应用程序的其余部分。我不推荐这种方法,因为它会混淆应用的全局样式来自何处,即使您在负责主题化的核心组件中这样做。谷歌的Angular材质库不使用ViewEncapsulation,也没有

TLDR:仅使用angular-cli.json嵌入全局css文件

Label {
    background-color: red;
}