2016-10-07 1 views
0

J'ai développé une application qui doit gérer une variable externe qui est définie dans mon index.html à l'intérieur de la balise <script>. J'ai besoin d'accéder à cela dans mon fichier de tapuscrit pour faire une opération mais pendant la compilation de son erreur d'affichage comme indiqué ci-dessous.Comment gérer une variable externe indéfinie dans typecript 2.0

//index.html 
<!DOCTYPE html> 
<html> 
    <head> 
    <title>Angular QuickStart</title> 
    <script> 
     window.widgetResources = { 
     'sessionId': '2f60e0a2-3fa2-46f4-9a5c-4a8afe5007c8', 
     'staticResourceURL': 'http://localhost:9090/OfferFinder/16101/1/0/', 
     'offers': { 
    </script> 
    </head> 

<body> 
    <app>loadings...</app> 
</body> 
</html> 




//WidgetResourcesList.js 
export class WidgetResourcesList { 

    //noinspection TypeScriptUnresolvedVariable 
    widgetResources = window.widgetResources; 
} 


//error getting 
C:\quickstart>tsc 
app/services/WidgetResourcesList.ts(5,28): error TS2339: 
`Property 'widgetResources' does not exist on type 'Window'.` 

Répondre

0

Simple

declare global { 
    interface Window { 
     widgetResources: { 
      sessionId: string, 
      staticResourceUrl: string, 
      offers: {} 
     }; 
    } 
} 
export class WidgetResourcesList { 
     widgetResources = window.widgetResources; 
}