2017-07-04 67 views
4

我有一个像下面打字稿代码: -住宅 'addRule' 和 'insertRule' 上类型不存在 '样式表'

export function getRootWindow():Window { 
 
    return window.top; 
 
} 
 

 
export function getRootDocument():HTMLDocument { 
 
    return getRootWindow().document; 
 
} 
 

 

 
declare global { 
 
    interface Document { 
 
     documentMode?: any; 
 
    } 
 
} 
 

 

 
export function isBrowserIE() { 
 
    return getRootDocument().documentMode; 
 
} 
 

 
export function addCssRule(cssString:string, num:number) { 
 
    let isIE = isBrowserIE(); 
 

 
    if(getRootDocument().styleSheets[0] == undefined) { 
 
     let head = getRootDocument().head || getRootDocument().getElementsByTagName('head')[0]; 
 
     let style = getRootDocument().createElement('style'); 
 

 
     head.appendChild(style); 
 
    } 
 

 
    if(isIE) { 
 
     getRootDocument().styleSheets[0].addRule(cssString,num); 
 
    }else { 
 
     getRootDocument().styleSheets[0].insertRule(cssString,num); 
 
    } 
 
}

此代码工作正常在javascript: -

<!DOCTYPE html> 
 
<html> 
 
<head> 
 

 
</head> 
 
<body style="color: #fff; font-family: arial, sans-sarif; background-color: #333; text-align: center;"> 
 

 
<h2>My mute speaker</h2> 
 

 
<a href="#" class="datawrkz_speaker"> 
 
    <span></span> 
 
</a> 
 

 
<script> 
 

 
    if(document.styleSheets[0] == undefined) { 
 
     var head = document.head || document.getElementsByTagName('head')[0]; 
 
     var style = document.createElement('style'); 
 

 
     head.appendChild(style); 
 
    } 
 

 
    
 
    var datawrkz_speaker  = document.getElementsByClassName("datawrkz_speaker")[0]; 
 

 
    datawrkz_speaker.addEventListener("click", function() { 
 
     this.classList.toggle("mute");    
 
    }); 
 

 
    function addCssRule(cssString, num) { 
 
     var isIE = /*@[email protected]*/false || !!document.documentMode; 
 

 
     if(isIE) { 
 
      document.styleSheets[0].addRule(cssString, num); 
 
     }else { 
 
      document.styleSheets[0].insertRule(cssString,num); 
 
     } 
 
     
 
    } 
 

 
    // set css rules as below for mute button 
 

 
    addCssRule(".datawrkz_speaker {height: 30px; width: 30px; position: relative; overflow: hidden; display: inline-block; }", 0); 
 

 
    addCssRule(".datawrkz_speaker span {display: block; width: 8px; height: 8px; background: #fff; margin: 11px 0 0 2px; }", 1); 
 

 
    addCssRule(".datawrkz_speaker span:after {content: ''; position: absolute; width: 0; height: 0; border-style: solid; border-color: transparent #fff transparent transparent; border-width: 10px 14px 10px 15px; left: -13px; top: 5px;}", 2); 
 

 
    addCssRule(".datawrkz_speaker span:before {transform: rotate(45deg); border-radius: 0 50px 0 0; content: ''; position: absolute; width: 5px; height: 5px; border-style: double; border-color: #fff; border-width: 7px 7px 0 0; left: 18px; top: 9px; transition: all 0.2s ease-out; }", 3); 
 

 
    addCssRule(".datawrkz_speaker:hover span:before {transform: scale(0.8) translate(-3px, 0) rotate(42deg); }", 4); 
 
    
 
    addCssRule(".datawrkz_speaker.mute span:before {transform: scale(0.5) translate(-15px, 0) rotate(36deg); opacity: 0; }", 5); 
 
    
 

 
</script> 
 

 
</body> 
 
</html>

我在Typescript中发现错误(上面的第一个代码片段): -

a。 'StyleSheet'上不存在属性'addRule'

b。属性'insertRule'在类型'StyleSheet'上不存在

回答

2

这是因为类型StyleSheet实际上是缺少addRule和insertRule方法。这些方法在CSSStyleSheet类型上定义。

可以说打字稿,你真的知道类型为CSSStyleSheet,而不是预测StyleSheet通过强制分型和addRule电话上的num增加的ToString调用(因为分型它需要)。

有2种可能的语法如何做到这一点。

if (isIE) { 
    (getRootDocument().styleSheets[0] as CSSStyleSheet).addRule(cssString, num.toString()); 
} else { 
    (getRootDocument().styleSheets[0] as CSSStyleSheet).insertRule(cssString, num); 
} 

替代语法

if (isIE) { 
    (<CSSStyleSheet>getRootDocument().styleSheets[0]).addRule(cssString, num.toString()); 
} else { 
    (<CSSStyleSheet>getRootDocument().styleSheets[0]).insertRule(cssString); 
} 
+0

类型断言为我工作。做得有点不正确。 – MiHawk

相关问题