如何在HTML中使用全局变量并在打字稿中使用它? [英] How to use a global variable in HTML and use it in typescript?
问题描述
我需要在我的index.html文件中定义一个变量,并在我的angular4 / typescript代码中使用它,并且出现编译错误。
我的编译错误是:
错误:(109,27)TS2339:'Window'类型中不存在属性'standalone'。
HTML中的变量定义为:
var standalone ='true' ;
System.import('app')。catch(function(err){
console.error(err );
});
< / script>
打字稿的代码是:
if(window.standalone!== undefined){
console.log('standalone');
}
任何人都能看到我做错了什么?
> declare var standalone:boolean;
为了引用 window
属性,一个全局也应该这样指定:
pre $ declare global {
interface Window {
standalone:boolean ;
$ b如果全局变量被使用一次或几次,类型可以被有意跳过:
if((< any> window).standalone!==未定义)...
或者:
if(window ['standalone']!== undefined)...
I need to define a variable in my index.html file and use it in my angular4/typescript code and I get a compile error.
my compile error is:
Error:(109, 27) TS2339: Property 'standalone' does not exist on type 'Window'.
the variable definition in HTML is: var standalone = 'true';
System.import('app').catch(function (err) {
console.error(err);
});
</script>
the typescript code is:
if(window.standalone !== undefined) {
console.log('standalone');
}
Anyone sees what I am doing wrong?
Global variables should be declared as globals in TypeScript:
declare var standalone: boolean;
In order to be referenced window
property, a global should be also specified as such:
declare global {
interface Window {
standalone: boolean;
}
}
If global variable is used one or several times and cannot benefit from type checks, types can be intentionally skipped instead:
if((<any>window).standalone !== undefined) ...
Or:
if(window['standalone'] !== undefined) ...
这篇关于如何在HTML中使用全局变量并在打字稿中使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!