3

I have an Angular 5 application in a subdirectory of another parent application.

The parent application is a set of JSP, and other angularJs applications... etc.

What I'm trying to do is include a CSS file, from this parent application, into a component of the Angular 5 application.

@Component({
  selector: 'app-my-form',
  templateUrl: './my-form.component.html',
  styleUrls: [....] //Here
})

Is it possible ?

I looked at this question : Load external css style into Angular 2 Component already asked, but it does not correspond to my case.

Thanks for your help

5
  • what's wrong with the answers in the link you mentionned? Commented Apr 13, 2018 at 15:26
  • 1
    @David : they use an url not a path.. Commented Apr 13, 2018 at 15:27
  • Is the css file available at build time? Or just at runtime, once the application is embedded Commented Apr 13, 2018 at 15:30
  • 1
    Yes, its available at built time Commented Apr 13, 2018 at 16:29
  • That's the same principle, you can either add in in styleUrls array in the component, or in .angular-cli styles setting if you are using it. You just need to provide the correct relative path Commented Apr 13, 2018 at 16:42

3 Answers 3

5

Try the following:

@Component({
  selector: 'app-my-form',
  templateUrl: './my-form.component.html',
  styleUrls: ['my-form.css']
})

Your file will be a SCSS file: myform.scss.

And then you can import an external css file like this:

@import "my-external-stylesheet.css"; // this on the top of `my-form.scss`
Sign up to request clarification or add additional context in comments.

1 Comment

"What I'm trying to do is include a CSS file, from this parent application, into a component of the Angular 5 application", means that you want to include an external CSS file into an Angular component. Therefore if you include the CSS file to the component's SCSS file... Isn't that what you want? Maybe I'm missing something...
3

In the .angular-cli.json file of your child application look for the styles property of your app. Add the path to your parent CSS in there.

Here is an example of the JSON config.

 "apps": [
    {
      "styles": [
        "../../../../apec-template-bootstrap-responsive/src/main/resources/css/old-apec.css",
        "styles.css"
      ],
    }
  ],

This should give your child application any style you require from the parent.

6 Comments

I have this error when using this : Module not found: Error: Can't resolve '...path to my css...' in '... path to my angular 5 app...'
you've set your path wrong. The path is relative to the location of the .angular-cli.json file.
And whats the path you have in your styles array?
and the absolute path for the css file you're trying to add?
Updated my answer.
|
2

your styleUrls can be the default one:

styleUrls: './my-form.component.css'

while in your my-form.component.css you can import the CSS files you want:

@import ".../component.css";

Comments

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.