首页 > 解决方案 > ionic , angular - index.html 中的全局变量,由其他 ts 页面调用

问题描述

组件(ts 文件)如何访问 index.html 中的变量?例子:

内部:index.html <javascript> var myVar = "test"; </javascript>

如何让这个变量“myVar”在组件页面中使用

例如在组件 html 上使用,例如 {{myVar}}

标签: angularionic2ionic3

解决方案


在 ts 文件中,您可以像这样声明它:

//Other imports
declare var myVar: any;

@Component({
selector: 'my-component',
templateUrl: './my-component.component.html',
 styleUrls: ['./my-component.component.scss']
})

export class MyComponent {
  my_javascript_variable = myVar;
}

推荐阅读