【发布时间】:2015-08-10 08:38:04
【问题描述】:
我的项目结构看起来像这样
-client
----index.html
----index.ts
-core
----controls
--------myControl.html
--------myControl.ts
----css
--------common.css
myControl.html 包含通过 shadow dom 注册的自定义组件的定义。 在其模板中导入属于“核心”库的 common.css:
<template id="t">
<style>
@import url('../css/common.css');
....
</style>
....
</template>
<script>
(function() {
var importDoc = document.currentScript.ownerDocument; // importee
var proto = Object.create(HTMLElement.prototype);
proto.createdCallback = function()
{
// get template in import
var template = importDoc.querySelector('#t');
// import template into
var clone = document.importNode(template.content, true);
var root = this.createShadowRoot();
root.appendChild(clone);
var control = this;
System.import('core/controls/myControl').then(function(m)
{
var t = new m.myControl(control.shadowRoot);
});
};
document.registerElement('my-control', {prototype: proto});
})();
</script>
现在,当我在 index.html 上使用 my-control 时,浏览器会抱怨它无法导入 common.css。因为它使用相对于 index.html 的路径搜索它,而不是相对于它的原始位置。我知道这是合乎逻辑的,因为我们从 index.html 上下文中运行的脚本创建了 shadow dom。
我的问题是:我应该如何开发我的控制自定义组件,它是“核心”库的一部分,可以在不同的地方重用/分发,并且仍然正确引用 css/images/etc 等资源,这些资源也是“核心”库。
提前致谢。
【问题讨论】:
标签: javascript css relative-path custom-component shadow-dom