简体   繁体   English

如何添加外部CSS文件 - styleUrls - Angular 5组件

[英]How to add external CSS files - styleUrls - Angular 5 component

I have an Angular 5 application in a subdirectory of another parent application. 我在另一个父应用程序的子目录中有一个Angular 5应用程序。

The parent application is a set of JSP, and other angularJs applications... etc. 父应用程序是一组JSP,以及其他angularJs应用程序......等。

What I'm trying to do is include a CSS file, from this parent application, into a component of the Angular 5 application. 我要做的是将来自此父应用程序的CSS文件包含到Angular 5应用程序的一个组件中。

 @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. 我看了这个问题:将外部css样式加载到已经问过的Angular 2 Component中 ,但它与我的情况不符。

Thanks for your help 谢谢你的帮助

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 . 您的文件将是一个SCSS文件: myform.scss

And then you can import an external css file like this: 然后你可以像这样导入一个外部的css文件:

@import "my-external-stylesheet.css"; // this on the top of `my-form.scss`

In the .angular-cli.json file of your child application look for the styles property of your app. 在子应用程序的.angular-cli.json文件中,查找应用程序的styles属性。 Add the path to your parent CSS in there. 在那里添加父CSS的路径。

Here is an example of the JSON config. 以下是JSON配置的示例。

 "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. 这应该为您的孩子申请从父母那里获得您需要的任何风格。

your styleUrls can be the default one: 你的styleUrls可以是默认的:

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

while in your my-form.component.css you can import the CSS files you want: my-form.component.css中,您可以导入所需的CSS文件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM