My AngularJS 2 application has several style files which I concatenate with a gulp
task. That's all fine, since they'll end up in a big file I send to the browser in production. My question is about the Angular 2 @Component
and its styleUrls
attribute.
@Component({
selector: 'hero',
templateUrl: 'hero/hero.template.html',
styleUrls: ['hero/hero.component.css'],
inputs: ['hero']
})
Thanks to shadow DOM emulation in default mode (emulated) the styles defined in the hero/hero.component.css
are applied only to the component just like I want. My question is, what happens with concatenation? I cannot bundle all the CSS files specified in the multiple styleUrls
since we'd be defeating the purpose of encapsulation: the styles for a component would leak to the whole document. However, I don't want either to make a call in production for every CSS file a component needs. How can I concatenate those styles (and possibly minify them) so the client gets them all in a single call, and still preserve encapsulation?
The Angular 2 “styles” or “styleUrls” should only be used for css rules and it is affect the style of the template elements. This is the best approaches to add styles directly to the components and the view encapsulation is set per component. It is use for some situations. Stayed Informed – Angular 2 Documentations and Examples
Encapsulating styles in Angular is possible by defining styles in the component itself. This allows the styles defined to be specific to the given component, without leakage to other parts of the application. styles property: Used to specify an array of declared styles.
Every app framework offers different ways of styling HTML components. In Angular, there are two ways to keep CSS code to style your component's HTML template. For each of these methods, a different property in component's metadata has to be used. styles property should be used when the number of style properties is less than ten or so.
There are few ways to concat the strings in AngularJS. In this article, we will see 2 of them. Example 1: In the first example, we are using the ‘+’ operator to concat the strings
The template and css files can be bundled along the js files using a system js plugin.
import { Component } from '@angular/core';
import html from './hero/hero.template.html!text';
import css from './hero/hero.component.css!text';
@Component({
selector: 'hero',
template: html,
styles: [css],
})
export class HeroComponent implements {
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With