简体   繁体   English

如何设置ng-bootstrap的样式-替代/ deep /

[英]How to Style ng-bootstrap - Alternative to /deep/

I am using the accordion component from ng-bootstrap in one of my apps and the /deep/ (aka: ::ng-deep and >>> ) selector to set its styles. 我正在其中一个应用程序中使用ng-bootstrap中的accordion组件,并使用/deep/ (aka ::ng-deep>>> )选择器来设置其样式。 However I saw this selector has been deprecated . 但是我看到此选择器已被弃用

Is there an alternative to set the styles of the ng-bootstrap components? 是否可以设置ng-bootstrap组件的样式?

Support for the emulated /deep/ CSS Selector (the Shadow-Piercing descendant combinator aka >>>) has been deprecated to match browser implementations and Chrome's intent to remove. 已不支持模拟的/ deep / CSS选择器(阴影穿透后代组合器,也称为>>>),以匹配浏览器实现和Chrome的删除意图。 ::ng-deep has been added to provide a temporary workaround for developers currently using this feature. 添加了:: ng-deep为当前正在使用此功能的开发人员提供临时解决方法。

From here: http://angularjs.blogspot.com/2017/07/angular-43-now-available.html 从这里: http : //angularjs.blogspot.com/2017/07/angular-43-now-available.html

I've done some digging and it is possible to overwrite the ng-bootstrap styles if you turn off the view encapsulation for a component, like so: 我已经做了一些挖掘,如果您关闭了组件的视图封装 ,则可以覆盖ng-bootstrap样式,如下所示:

@Component({
  selector: 'app-example',
  templateUrl: './example.component.html',
  styleUrls: ['./example.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class ExampleComponent {

    constructor(public activeModal: NgbActiveModal) { }

}

For example, here I am customizing the styles for a modal dialog , because I assumed that my example component should be shown as a modal dialog and I want its width to be at maximum 70% of the view port in small screen sizes: 例如,在这里,我要为模式对话框自定义样式,因为我假设示例组件应显示为模式对话框,并且在小屏幕尺寸下,我希望其宽度最大为视口的70%:

.modal-dialog {
  @include media-breakpoint-down(xs) {
    max-width: 70vw;
    margin: 10px auto;
  }
}

Please note that turning off the view encapsulation means that the styles for this particular component will be available throughout the application, therefore other components will have access to them and will eventually use them if you are not careful. 请注意,关闭视图封装意味着该特定组件的样式将在整个应用程序中可用,因此,如果您不小心,其他组件将可以访问它们,并最终使用它们。

You have to make custom styling to override the default bootstrap variable in scss. 您必须进行自定义样式以覆盖scss中的默认bootstrap变量。

You can find all possible variables here ../node_modules/bootstrap/scss/_variables.scss 您可以在此处找到所有可能的variables ../node_modules/bootstrap/scss/_variables.scss

Bootstrap also provide a custom file for us add our styling ../node_modules/bootstrap/scss/_custom.scss Bootstrap还为我们提供了一个custom文件,用于添加样式../node_modules/bootstrap/scss/_custom.scss

for eg: here i am overriding two variable that effect my accordion (i changed some random colors) 例如:在这里,我覆盖了两个影响手风琴的变量(我更改了一些随机颜色)

$link-color:$brand-warning;
$card-bg:$green;

and dont forget to remove !default in _custom.scss and you done with the custom styling. 并且不要忘记删除_custom.scss中的!default ,您便完成了自定义样式。

Now need to build the css from scss 现在需要从scss构建css

First install npm install -g grunt-cli globally 首先在全局安装npm install -g grunt-cli

then navigate to \\my-app\\node_modules\\bootstrap> npm install , this will install all dependencies. 然后导航到\\my-app\\node_modules\\bootstrap> npm install ,这将安装所有依赖项。

Lastly run \\my-app\\node_modules\\bootstrap> grunt , this will creates the css file. 最后运行\\my-app\\node_modules\\bootstrap> grunt ,这将创建css文件。

This should work hopefully. 这应该有望工作。

In my case "bootstrap.min.css" was not generated correctly, so now iam using "../node_modules/bootstrap/dist/css/bootstrap.css " in my .angular-cli.json file. 在我的情况下,“ bootstrap.min.css”未正确生成,所以现在我在我的.angular-cli.json文件中使用“ ../node_modules/bootstrap/dist/css/bootstrap.css”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM