这是我的聚合物元素:
<link rel="import" href="../polymer/polymer.html">
<script src="bower_components/cytoscape/dist/cytoscape.js"></script>
<dom-module id="dependency-graph">
<template>
<style>
#surface {
width: 200px;
height: 200px;
}
</style>
<div id="surface"></div>
</template>
<script>
Polymer({
is: 'dependency-graph',
ready: function () {
var surface = this.$.surface;
var cy = cytoscape({
container: surface
});
...运行此操作将引发胞状海角中的异常。
return ( _p.sizeCache = _p.sizeCache || ( container ? (function(){
var rect = container.getBoundingClientRect();
var style = window.getComputedStyle( container );
var val = function( name ){ return parseFloat( style.getPropertyValue( name ) ); };
return {
width: rect.width - val('padding-left') - val('padding-right') - val('border-left-width') - val('border-right-width'),
height: rect.height - val('padding-top') - val('padding-bottom') - val('border-top-width') - val('border-bottom-width')
};因为style.getPropertyValue返回""。如果我使用document.body作为容器,它将返回一个数字,并运行。我做错了什么,我如何解决它?
我找到了一篇关于类似问题的文章,但我没有理解(或者它不起作用?):webcomponentsjs不兼容使用window.getComputedStyle的第三方脚本。?
发布于 2017-03-07 14:31:55
我把我的代码移到了attached: function () {上,现在它没有任何问题。Thx @maxkfranz来表达这个想法。
发布于 2017-03-03 20:34:52
确保您对DOM中已经存在的元素调用Cytoscape,并且只在DOMContentLoaded之后初始化。如果window.getComputedStyle()不能工作,那么至少有一个要求没有得到满足。
https://stackoverflow.com/questions/42511627
复制相似问题