简体   繁体   English

打字稿错误:“Window”类型上不存在“_myApiUrl”属性

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

I am new to typescript and I run into a problem.我是打字稿的新手,遇到了问题。 I had this method in a Typescript (.ts) file:我在 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
}

I have replaced http://myApiUrl with a variable window._myApiUrl and I get the error:我已经用一个变量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
}

Why does this happen?为什么会发生这种情况? How can I fix the issue?我该如何解决这个问题?

Typescript doesn't know that variable exists.打字稿不知道变量存在。 You can declare it:你可以声明它:

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

export class...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM