0

我一直在开发一个应用程序,它需要处理在我的 index.html 内部<script>标签和窗口范围内定义的外部变量。我需要在我的打字稿文件中访问它以执行一些操作,但在编译期间显示错误,如下所示。

//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'.`
4

1 回答 1

0

简单的

declare global {
    interface Window {
        widgetResources: {
            sessionId: string,
            staticResourceUrl: string,
            offers: {}
        };
    } 
}
export class WidgetResourcesList  {
      widgetResources = window.widgetResources;
}
于 2016-12-15T08:29:00.783 回答