简体   繁体   English

谷歌地图的css覆盖了我自己的样式,如何防止这种情况?

[英]Google map's css overrides my own styles, how can I prevent that?

I'm using Angular 7.2.2 with Typescript. 我正在将Angular 7.2.2与Typescript一起使用。

I'm importing Google Maps from " https://maps.googleapis.com/maps/api/js?key=myAPIkey " via <script> -tag in my index.html 我正在通过index.html中的<script> -tag从“ https://maps.googleapis.com/maps/api/js?key=myAPIkey ”导入Google Maps

As soon as the script has finished loading, I can see some of my styles being overwritten application wide by Google Map's styles. 脚本加载完成后,我可以看到我的某些样式被Google Map的样式覆盖了整个应用程序。

How can I prevent this and ensure that Google Map's styles are only applied to the component in which I will render the map? 如何防止这种情况并确保Google Map的样式仅应用于将在其中渲染地图的组件?

I have tried to load the script dynamically from within the displaying component, but obviously the style encapsulation works only for styles provided by the "styleUrls" parameter. 我试图从显示组件内部动态加载脚本,但是显然,样式封装仅适用于“ styleUrls”参数提供的样式。

Thank you! 谢谢!

So the classes you are using in your angular app are named the same thing in the styles from the Google Maps API. 因此,在您的角度应用程序中使用的类在Google Maps API样式中的名称相同。

Your styles are imported in the order that you have them in your angular.json file, then your index.html file loads its' scripts once it is initialized which happens after your style sheets from angular.json is read. 样式将按照将它们放入angular.json文件中的顺序导入,然后,一旦初始化,则index.html文件将加载其脚本,该脚本将在从angular.json读取样式表之后发生。

So it seems your option is to change the name of the classes that are being overwritten so that Google does not overwrite them because I do not know of an NPM Package for Google Maps that is kept up to date with the current Google Maps, but if you find one, that might work too. 因此,您似乎可以选择更改要覆盖的类的名称,以使Google不会覆盖它们,因为我不知道与当前Google Maps保持最新关系的Google Maps NPM软件包。您会找到一个,也可能会起作用。

Thanks guys, I think I have the answer to my question. 谢谢大家,我想我已经回答了我的问题。

In case anyone else stumbles over that stupid mistake: 万一其他人因这个愚蠢的错误而迷迷糊糊:

I use Angular Material which in turn uses Roboto Medium as a default font. 我使用Angular Material,后者又将Roboto Medium作为默认字体。

Since I didn't install the correct webfont, my application used Arial as a fallback. 由于我没有安装正确的webfont,因此我的应用程序使用Arial作为后备。

Now when I load the Google Maps script, which also uses Roboto and loads that font correctly, my whole application switches from Arial to Roboto. 现在,当我加载还使用Roboto并正确加载该字体的Google Maps脚本时,我的整个应用程序从Arial切换到Roboto。

That made me think that styles are overwritten. 这使我认为样式被覆盖。

(And thanks Lorenz for handling my poor writing :)) (感谢洛伦兹处理我的拙劣著作:)

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

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