2017-09-15 97 views
0

是否可以将计算的样式值作为JSON对象输出?你能得到一个计算风格(CSS混合)作为JSON对象吗?

 ... 
     ready: function() { 
      // themeClass = css mixin --theme-typography 
      style = this.getComputedStyleValue(this.themeClass); 

      console.log(style); 
      // outputs: 
      // font-size: 3.0000em; 
      // line-height: 1.0000em; 
      // margin-top: 0.5000em; 
      // margin-bottom: 0.0000em; 
      // font-weight: 400; 
      // font-family: Helvetica Neue, Helvetica, Arial, sans-serif; 

     } 

但理论上应该是这样的:

  {"font-size":"3.0000em;","line-height":"1.0000em;" etc.} 

回答

0
const style = this.getComputedStyleValue(this.themeClass); 

console.log(JSON.stringify(style)); 

这是不是一个真正的聚合物问题。

+0

确实,删除了聚合物标签,并添加了javascript标签。但是,JSON.stringify不会从字符串中创建对象。 – Remi

+0

@Remi我加回了标签,对不起,我与原生javascript函数'window.getComputedStyle(...)'混淆了。你在用聚合物1.0吗?我对此并不熟悉,在polymer2.0中'getComputedStyleValue'不存在。如果这是一个个人项目,我会建议你转向polymer2.0,你可能试图做一些没有理由在新版本中完成的事情。 Polymer目前正在开发3.0版本。对不起,我忍不住 – user544262772

+0

感谢您的努力。为了记录,'getComputeStyleValu'e在Polymer 2.0中也是已知的。然后再与ShadyCSS结合使用。见https://www.polymer-project.org/2.0/docs/upgrade – Remi

相关问题