我的问题:
例如,烤面包图片:示例烤面包
我想将我的CSS添加到特定的烤面包消息中,以便我可以将消息文本居中对齐,即文件导入已开始..
我的Angular目录长什么样?
| app
|
|-components
| |
| |-test [/app.ts , /app.css]
|
|style.css
我尝试的:
- 我将以下代码添加到我的CSS和TS代码中:
我的粗略app.css代码
.test {
text-align : center !important // I tried without ! important also
}
我的粗略app.ts代码
import { Component } from '@angular/core';
import {ToastrService} from 'ngx-toastr';
@Component({
selector: 'my-app',
templateUrl: './app.html',
styleUrls: [ './app.css' ]
})
export class app {
constructor(private toastr: ToastrService) {}
showSuccess() {
// I tried \" test \" also
this.toastr.success('<span class="test">File import started</span>', '', {
enableHtml : true //even I have added the messageClass : 'test'
});
}
}
如何实现,但我不想用以下方式:
- 将我的CSS代码放入style.css(主要全局CSS)中 (X 我不想改变我的主要样式)
- 通过添加
::ng-deep .test
而不是.test
:这很危险,它会更改所有我的toast-dialogue。 - 在@component中添加
encapsulation: ViewEncapsulation.None
:但这将更改我其他的CSS。 - 使用
<center>
标签:仍然不想这样做,因为它会解决我的问题,但如果我想添加多个CSS呢?
我该如何实现?
::ng-deep .exampleToast .test { // 在这里编写您的自定义 CSS }
来仅为您的 toast 组件设置样式。 - ilkengin::ng-deep .test
时,你是在为添加了test类的span设置样式(请参见你的代码)。然而,messageClass将类添加到你的toast消息本身(而不是span)。因此,当你说::ng-deep .exampleToast .test时,它意味着样式应用于具有classtest
的元素,该元素位于具有classexampleToast
的另一个元素内部。此外,ng-deep帮助你搜索与你的条件匹配的DOM中的每个元素。如果你不添加ng-deep,Angular只会在你组件的html中搜索匹配的元素。希望我能帮到你 :) - ilkengin