2017-09-15 41 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

相關問題