0

I have a CSS file I ONLY want to be loaded when a component is on the page.

I have tried the following:

@Component({
  selector: 'testview-testview',
    templateUrl: './templates/testview.html',
    styles: [
        "../../../assets/vendors/custom/reveal.js-3.6.0/css/reveal.css",
        "../../../assets/vendors/custom/reveal.js-3.6.0/css/theme/black.css"
    ],
})

However without any luck. Does anyone know of a way to do this?

EDIT

So i am attempting to follow the answer below

Here is my folder structure:

enter image description here

My Component now looks like this:

import {Component, OnInit} from '@angular/core';

declare var Reveal: any;

@Component({
    selector: 'testview-testview',
    templateUrl: './templates/testview.html',
    styleUrls: ['./templates/testview.css'],
})
export class TestviewComponent implements OnInit {

    constructor() {
    }


    ngOnInit() {
        Reveal.initialize({});
    }

}

And my testview.css file looks like this:

    @import url("../../../assets/vendors/custom/reveal.js-3.6.0/css/reveal.css");
@import url("../../../assets/vendors/custom/reveal.js-3.6.0/css/theme/black.css");
3
  • Does this also mean you want the CSS to be unloaded when the component is not being displayed anymore? Commented May 23, 2018 at 10:02
  • @balu preferably Commented May 23, 2018 at 11:14
  • Take a look at Angular's ViewEncapsulation feature. While this won't help you load or unload the CSS at specific times, it will give you precise control over which DOM element / which component the styles are applied to—which, I suppose, is what you actually want(?) Commented May 23, 2018 at 21:37

1 Answer 1

1

You have to inside the urls inside array of styls: styleUrls instead of styles

SO

@Component({
  selector: 'testview-testview',
    templateUrl: './templates/testview.html',
    styleUrls: [
        "../../../assets/vendors/custom/reveal.js-3.6.0/css/reveal.css",
        "../../../assets/vendors/custom/reveal.js-3.6.0/css/theme/black.css"
    ],
})

EDIT:

@Component({
  selector: 'testview-testview',
    templateUrl: './templates/testview.html',
    styleUrls: ['./templates/testview.css'

    ],
})

in you css ./templates/testview.css import files

@import url("../../../assets/vendors/custom/reveal.js-3.6.0/css/reveal.css");
@import url("../../../assets/vendors/custom/reveal.js-3.6.0/css/theme/black.css");
Sign up to request clarification or add additional context in comments.

6 Comments

when i do this i get a compiler error: Expected 'styles' to be an array of strings
@MarcRasmussen did you change 'styles' to 'styleUrls'? (subtle change)
The styles get bundled when building. This is technically not a seperate stylesheet anymore getting loaded when the component is created.
I am updating my question
try import /assets/vendors/custom/reveal.js-3.6.0/css/reveal.css
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.