3

I am new to Angular 6 and trying to use my css files in a particular component.

My angular.json file says this:

"angapp": {
  "root": "",
  "sourceRoot": "src",
  ....

My css files are placed under src/assets/css directory. I have a component named dashboard.component.ts in

src/app/components/secure/dasbhoard

I am trying to include file src/assets/css/sidebar-nav.min.css in following ways but could not:

@Component({
  selector: 'dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: ['src/assets/css/sidebar-nav.min.css']
})

or

@Component({
  selector: 'dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: ['/src/assets/css/sidebar-nav.min.css']
})

or

@Component({
  selector: 'dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: ['/assets/css/sidebar-nav.min.css']
})

or

@Component({
  selector: 'dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: ['./dashboard.component.css']
})

and tried to import css in dashboard.component.css

@import '/src/assets/css/sidebar-nav.min.css';

None of the above is working. I get the error:

Module not found: Error: Can't resolve './src/assets/css/sidebar-nav.min.css' 

The file does exist in: ..../src/app/components/secure/dashboard

I think I am missing path from the root directory.

2 Answers 2

4

Absolute path in styleUrls is not trivial, as documented here.

You can still use relative path though : ../../../etc.

Going with @import in your scss is another solution indeed, and you can just use:

@import 'src/assets/css/sidebar-nav.min.css';
Sign up to request clarification or add additional context in comments.

Comments

-1

Found the answer in this thread:

Angular 6 - How to apply external css stylesheet (leaflet) at component level?

Imported css like this:

@import url('src/assets/css/sidebar-nav.min.css');

url() is not required, the actual statement should be:

@import 'src/assets/css/sidebar-nav.min.css';

3 Comments

I don't think it is required. I don't use url() in my project and it works well.
Yes, thanks Mic, url is not necessary, but looks like styles are not being applied :(
Seems weird. Try doing syntax error in the css and see what happens in the browser and compiler. Also, look for styles in the browser to see where they appear. They are most likely there, but maybe something in the HTML prevents them from being applied.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.