簡體   English   中英

打字稿錯誤:“Window”類型上不存在“_myApiUrl”屬性

[英]Typescript error: Property '_myApiUrl' does not exist on type 'Window'

我是打字稿的新手,遇到了問題。 我在 Typescript (.ts) 文件中有這個方法:

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
  providers: [Service1, Service2]
})

export class AppComponent implements OnInit, OnDestroy {
// more code here
    myMethod() {
       this.isLoading = true;
       const serverPath = 'http://myApiUrl';
       // more code here
    }
// more code here
}

我已經用一個變量window._myApiUrl替換了http://myApiUrl window._myApiUrl並且我得到了錯誤:

Property '_myApiUrl' does not exist on type 'Window' 

myMethod() {
    this.isLoading = true;
    const serverPath = window._myApiUrl;
    // more code here
}

為什么會發生這種情況? 我該如何解決這個問題?

打字稿不知道變量存在。 你可以聲明它:

declare global {
    interface Window {
        _myApiUrl: string;
    }
}

export class...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM