Comprendre la ViewEncapsulation dans un Component Angular

Simon Bernard
Mar 8, 2017 · 3 min read

Mais qu’est-ce que le Shadow DOM

Et pourquoi est-ce important en Angular ?

Les types d’encapsulations

Comment déclarer le type d’encapsulation ?

import {ViewEncapsulation} from '@angular/core';

@Component({
selector: 'my-random',
templateUrl: 'my-random.component.html',
encapsulation: ViewEncapsulation.None})
class MyRandomComponent {}
import {ViewEncapsulation} from '@angular/core';

@Component({
selector: 'my-random',
templateUrl: 'my-random.component.html',
encapsulation: ViewEncapsulation.Emulated})
class MyRandomComponent {}
<my-random _ngcontent-0 _nghost-1>
<div class="random" _ngcontent-1>
...

</div>
</my-random>
<head>
<style>.random[_ngcontent-1] {
...
}</style>
</head>
import {ViewEncapsulation} from '@angular/core';

@Component({
selector: 'my-random',
templateUrl: 'my-random.component.html',
encapsulation: ViewEncapsulation.Native})
class MyRandomComponent {}
<my-random>
#shadow-root
| <style>
| .random {
| ...
| }
| </style>
| <div class="random">
| ...
| </div>
</my-random>

Simon Bernard

Written by

Senior developer & Architect Front End @SignkickUK. Focused on modern JS/TS, Angular & Node.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade