8

I have two modules: FrontUI & AdminUI. The FrontUI is the root module and the AdminUI is lazy loaded.

In the Admin section, I need to load few .css files that are different from the FrontUI and is particular to admin section only. I tried the following -

  1. I have added these .css file path in angular.json as shown below and this works. But this will bundle these files during startup, which is not proper
"styles": [
              "node_modules/@xyzPlugIn/base.css",
              "node_modules/@xyzPlugIn/theme.css"
            ],
  1. I have added the .css file path in component.ts file as follows, but this is not rendering the css
@Component({
  selector: 'app-admin',
  templateUrl: './admin.component.html',
  styleUrls: ['../node_modules/@xyzPlugIn/base.css',
              '../node_modules/@xyzPlugIn/theme.css']
})

Can different styles be loaded for two separate modules, as the client/users are different? In production environment, can these files be bundled?

P.S: The project structure I have mentioned is short for better understanding.

3
  • 1
    You can add an import at the top of your component import '@xyzPlugIn/base.css';. If you use scss, add @import "~@xyzPlugIn/base"; to your stylesheet. Also, are your sure your relative path to node_modules is right ? Commented Mar 26, 2019 at 13:11
  • I tried with import, @import in component but it doesn't work for .css files. Yes, the relative path is correct in my project. Commented Mar 27, 2019 at 8:03
  • 1
    @import will be in your stylesheet (without extension and tilde at beginning). import will be in your component (with .css extension). I have used both at several occasions. Commented Mar 27, 2019 at 12:59

1 Answer 1

21

Thank you Jonathan Hamel.

I achieved by doing the following:

admin.component.ts

@Component({...
  styleUrls: ['./admin.component.css'],
  encapsulation: ViewEncapsulation.None
})

admin.component.css

@import "~@xyzPlugIn/base.css";
@import "~@xyzPlugIn/theme.css";
  1. Adding tilde '~' represents the node_module in Angular.
  2. Set encapsulation to ViewEncapsulation.None so that its sub components can also apply this style.
Sign up to request clarification or add additional context in comments.

2 Comments

Just wanted to mention that ViewEncapsulation.None is a bad thing and breaks the principles of component styles.
If the component is a part of Angular library and I publish it - this path does not work correct, because the css are sought in app root instead of node_modules: https://localhost:4200/~@xyzPlugIn/base.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.