我使用Polymer-cli(版本0.16.0)来构建聚合物项目,但无法使用控制台访问shadow-root:
var element = document.getElementsByTagName("my-app")[0].shadowRoot
返回undefined,因此看起来Shadow DOM处于关闭模式。
如何强制自定义元素使用开放模式,以便我可以从外部访问它的shadowRoot?
编辑2: 这里的主要问题是访问shadowRoot需要做些什么,所以我将问题稍微更改为与阴影DOM模式无关
编辑:
参考https://developers.google.com/web/fundamentals/getting-started/primers/shadowdom#advanced高级主题部分
上的关闭模式重现场景的步骤:
npm install -g polymer-cli
mkdir my-app
cd my-app
polymer init starter-kit
polymer serve --open
一些代码段:
的index.html:
...
<head>
...
<script>
// Setup Polymer options
window.Polymer = {
dom: 'shadow',
lazyRegister: true
};
// Load webcomponentsjs polyfill if browser does not support native Web Components
(function() {
'use strict';
var onload = function() {
// For native Imports, manually fire WebComponentsReady so user code
// can use the same code path for native and polyfill'd imports.
if (!window.HTMLImports) {
document.dispatchEvent(
new CustomEvent('WebComponentsReady', {bubbles: true})
);
}
};
var webComponentsSupported = (
'registerElement' in document
&& 'import' in document.createElement('link')
&& 'content' in document.createElement('template')
);
if (!webComponentsSupported) {
var script = document.createElement('script');
script.async = true;
script.src = '/bower_components/webcomponentsjs/webcomponents-lite.min.js';
script.onload = onload;
document.head.appendChild(script);
} else {
onload();
}
})();
// Load pre-caching Service Worker
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('/service-worker.js');
});
}
</script>
<link rel="import" href="/src/my-app.html">
...
</head>
<body>
<my-app></my-app>
</body>
</html>
MY-app.html:
...
<dom-module id="my-app">
<template>
...
</template>
<script>
Polymer({
is: 'my-app',
properties: {
page: {
type: String,
reflectToAttribute: true,
observer: '_pageChanged'
}
},
observers: [
'_routePageChanged(routeData.page)'
],
_routePageChanged: function(page) {
this.page = page || 'view1';
},
_pageChanged: function(page) {
// Load page import on demand. Show 404 page if fails
var resolvedPageUrl = this.resolveUrl('my-' + page + '.html');
this.importHref(resolvedPageUrl, null, this._showPage404, true);
},
_showPage404: function() {
this.page = 'view404';
}
});
</script>
</dom-module>
答案 0 :(得分:0)
如果使用shady DOM(默认),则没有shadowRoot
。
请参阅https://www.polymer-project.org/1.0/docs/devguide/settings如何启用shadow DOM。
这样的事情应该有效
var element = Polymer.dom(document.getElementsByTagName("my-app")[0]).root
有关详细信息,请参阅https://www.polymer-project.org/1.0/docs/devguide/local-dom
答案 1 :(得分:0)
实际上应该可行。请参阅下面的代码段。
也许您没有在正确的位置添加{ dom: 'shadow' }
设置。它应该在<link>
到polymer.html
之前定义。
output.textContent = document.querySelector('my-app').shadowRoot
<head>
<script>
window.Polymer = {
dom: 'shadow',
lazyRegister: true
}
</script>
<link rel=import href="https://rawgit.com/polymer/polymer/master/polymer.html">
</head>
<body>
<dom-module id="my-app">
<template>
Hello
</template>
<script>
Polymer({
is: 'my-app'
})
</script>
</dom-module>
<my-app></my-app>
<div id=output></div>
</body>