gpt4 book ai didi

javascript - 带有内部 js 和 css 的 Webcomponent 不显示内容

转载 作者:行者123 更新时间:2023-11-30 13:50:50 28 4
gpt4 key购买 nike

学习webcomponents,我试着做了一个像这样的非平凡的,包裹了一张Leaflet slippy map。然而,我无法让它工作...这是我的尝试 - 如您所见,没有 map :

enter image description here

website-with-webcomponent-online.html

<html>
<head>
<title>A Leaflet map!</title>
<script src="./leaflet-map-webcomponent-online.js"></script>
</head>
<body>

Text before Leaflet.
<leaflet-map></leaflet-map>
Text after Leaflet.

</body>
</html>

leaflet-map-webcomponent-online.js

(function(){
const template = document.createElement('template');
template.innerHTML = `
<style>
display: block;
.map-root{
border: 1px solid black;
height: 180px;
}
</style>
<h1>Leaflet web component</h1>
<div id="map" class="map-root"></div>
`;

class CustomLeafletMapWebComponent extends HTMLElement {
constructor() {
super();
console.log('CustomLeafletMapWebComponent constructed!');
this._shadowRoot = this.attachShadow({ 'mode': 'open' });
this._shadowRoot.appendChild(template.content.cloneNode(true));
this.loadDependencies();
}

connectedCallback() {
console.log('CustomLeafletMapWebComponent connected!');

const mapRoot = this._shadowRoot.querySelector(".map-root")

// timeout to avoid error "L is not defined"
setTimeout(function(){

// initialize the map
var map = L.map(mapRoot).setView([42.35, -71.08], 13);

// load a tile layer
L.tileLayer('http://tiles.mapc.org/basemap/{z}/{x}/{y}.png',
{
attribution: 'Tiles by <a href="http://mapc.org">MAPC</a>, Data by <a href="http://mass.gov/mgis">MassGIS</a>',
maxZoom: 17,
minZoom: 9
}).addTo(map);
}, 100);
}

disconnectedCallback() {
console.log('CustomLeafletMapWebComponent disconnected!');
}

attributeChangedCallback(name, oldVal, newVal) {
console.log(`Attribute: ${name} changed!`);
}

adoptedCallback() {
console.log('CustomLeafletMapWebComponent adopted!');
}

loadCssFile(cssfile){
var link = document.createElement("link");
link.rel = "stylesheet";
link.href = cssfile
this._shadowRoot.appendChild(link)
// // Variation:
// var head = document.getElementsByTagName("head")[0]
// head.appendChild(link)
}
loadScriptFile(scriptfile){
var script = document.createElement("script");
script.type = "text/javascript";
script.src = scriptfile
this._shadowRoot.appendChild(script)
// // Variation:
// var head = document.getElementsByTagName("head")[0]
// head.appendChild(script)
}
loadDependencies(){
this.loadCssFile("https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.5.1/leaflet.css")
this.loadScriptFile("https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.5.1/leaflet.js")
}
}
window.customElements.define('leaflet-map', CustomLeafletMapWebComponent);
})()

在一个细微的变化中,我将脚本和 css 文件附加到文档的头部而不是影子 dom,它看起来像这样: enter image description here

最佳答案

找到解决方案。将模板的 css 更改为:

template.innerHTML = `
<style>
.map-root{
border: 1px solid black;
height: 180px;
}
</style>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.5.1/leaflet.css">
<h1>Leaflet web component</h1>
<div id="map" class="map-root"></div>
`;

将 leaflet.js 包含到 shadowdom 或文档中并不重要 - 至少对于此示例而言。

loadScriptFile(scriptfile){
var script = document.createElement("script");
script.type = "text/javascript";
script.src = scriptfile
var head = document.getElementsByTagName("head")[0]
this._shadowRoot.appendChild(script)
}
loadDependencies(){
//this.loadCssFile("https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.5.1/leaflet.css")
this.loadScriptFile("https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.5.1/leaflet.js")
}

关于javascript - 带有内部 js 和 css 的 Webcomponent 不显示内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58272007/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com