简体   繁体   English

Angular 6 - 如何在组件级别应用外部 CSS 样式表(传单)?

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

Trying to use Leaflet in an Angular 6 component.尝试在 Angular 6 组件中使用 Leaflet。 Depending on how the css file is linked, the map shows ok or is messed up, there are missing tiles not in the right order, which means the css is not taken into account.根据 css 文件的链接方式,地图显示正常或混乱,丢失的图块顺序不正确,这意味着未考虑 css。

I managed to make it work with 2 solutions linking the css to the application level (global), but never only to the component.我设法使它与将 css 链接到应用程序级别(全局)的 2 个解决方案一起工作,但不仅仅是组件。 Here's what I tried (in addition to reading several posts about css/leaflet/Angular):这是我尝试过的(除了阅读有关 css/leaflet/Angular 的几篇文章之外):

Worked - global level:工作- 全球层面:

// styles.css
@import url("assets/lib/leaflet/leaflet.css");

Worked - global level:工作- 全球层面:

// index.html
<link rel="stylesheet" href="./assets/lib/leaflet/leaflet.css" type="text/css">

Did not work - global level:没用- 全球层面:

// angular.json
"styles": [
    "src/styles.css",
    "src/assets/lib/leaflet/leaflet.css"
],

Did not work - component level:没用- 组件级别:

// ...

import * as L from "../assets/lib/leaflet/leaflet.js";
import "../assets/lib/leaflet/leaflet-bing-layer.js";
import { BingHelper } from "../assets/lib/bing/bing-helper.js";

// -> importing the .css file here does not work

@Component({
    templateUrl: "./map.component.html",
    selector: "app-map",
    styleUrls: ["../assets/lib/leaflet/leaflet.css"] // -> does not work
    
    // -> also tried to put the content of the .css in style: "", did not work neither

})
export class MapComponent implements AfterViewInit {
    ngAfterViewInit() {
        var map = L.map("map", {
            attributionControl: false,
            zoom: 8,
            minZoom: 3,
            maxZoom: 15,
            center: new L.LatLng(40.737, -73.923)
        });
        // ...

Did not work : encapsulation - component level: Load external CSS into component不起作用:封装 - 组件级别:将外部 CSS 加载到组件中

Loading from CDN instead of local file does not change the issue.从 CDN 而不是本地文件加载不会改变问题。

Note: I am using a Bing layer extension, but this has no impact on this issue.注意:我正在使用 Bing 层扩展,但这对此问题没有影响。 I also had the same issue using Mapbox tiles instead.我也有同样的问题,而是使用 Mapbox 瓷砖。

Question : is there a way to link Leaflet css in a component and make it only available to the component, but not to the whole Angular application?问题:有没有办法在组件中链接 Leaflet css 并使其仅可用于组件,而不是整个 Angular 应用程序?

Ok, here's what worked (thanks @Palsri, I read once more the blog post and the Angular styling guidelines and tried the following, which worked):好的,这就是有效的方法(感谢@Palsri,我再次阅读了博客文章和 Angular 样式指南并尝试了以下方法,它有效):

In a separate css file, import the leaflet css:在单独的 css 文件中,导入传单 css:

// map.component.css
@import url("../assets/lib/leaflet/leaflet.css");

.mapstyle {
    width: 100%;
    height:100%;
};

Then in the component, reference this css instead of the leaflet css as follows:然后在组件中,引用这个 css 而不是传单 css,如下所示:

@Component({
    templateUrl: "./map.component.html",
    selector: "app-map",
    styleUrls: ["./map.component.css"],
    encapsulation: ViewEncapsulation.None
})

Here's the code in the html template:这是html模板中的代码:

<div id="map" class="mapstyle"></div>

One more thing: for the height % to work, you need to define the parents size, which I currently did in the index.html as follows:还有一件事:要使高度 % 起作用,您需要定义父母的大小,我目前在 index.html 中这样做如下:

<style>
html, body {
    min-height: 100% !important;
    height:     100%;
    padding: 0px 0px 0px 0px;
    margin:  0px 0px 0px 0px;
}
</style>

Doing this这样做

@import url("path")

ex:前任:

@import url("https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css")

or doig also或 doig 也

@import "bootstrap/dist/css/bootstrap.min.css"

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

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