【问题标题】:Property 'intlTelInput' does not exist on type 'Window'“窗口”类型上不存在属性“intlTelInput”
【发布时间】:2019-10-12 23:55:24
【问题描述】:

我正在使用 angular7。我的网站需要国际电话输入 (https://intl-tel-input.com/)。

所以我使用下面的注释来安装 CDN。

npm uninstall ng2-tel-input --save

之后我调用了 angular.json 中的 CSS 和 JS 文件

"styles": [
     "node_modules/intl-tel-input/build/css/intlTelInput.min.css",
      "src/styles.css"
],
"scripts": [
    "node_modules/intl-tel-input/build/js/utils.js",
    "node_modules/intl-tel-input/build/js/intlTelInput.min.js"
],

在“contactform.compenent.html”中添加了电话号码的输入字段

<input type="tel" class="form-control" value="" placeholder="Mobile number"
       id="telnumber-field" required>

并在“app.compenent.ts”中添加了电话区的脚本

ngOnInit(){

jQuery( document ).ready( function( $ ) {   

    if(jQuery('#telnumber-field').length){
        var input = document.querySelector("#telnumber-field");
        window.intlTelInput(input, {
            preferredCountries: ['in'],
            separateDialCode: true
        });
    }       

}); }

但是有一个错误来了。

src/app/app.component.ts(65,11) 中的错误:错误 TS2339:“窗口”类型上不存在属性“intlTelInput”。

【问题讨论】:

  • 尝试window['intlTelInput'] 或将其添加到界面中。 interface window { intlTelInput: any}
  • 感谢您的帮助,继续努力。

标签: angular angular6 angular7 intl-tel-input


【解决方案1】:

在 typescript 编译时 intlTelInput 对象未在窗口对象上创建。将窗口类型更改为 any 以跳过窗口持续时间编译的属性检查。 intlTelInput 在浏览器中加载应用程序后可见,因此应用程序将按预期工作。

  ngOnInit(){

   jQuery( document ).ready( function( $ ) {   

    if(jQuery('#telnumber-field').length){
        var input = document.querySelector("#telnumber-field");
       (<any>window).intlTelInput(input, {
            preferredCountries: ['in'],
            separateDialCode: true
        });
    }       

}); 
}

【讨论】:

  • @MulammadNasir 我刚刚尝试了您的解决方案,并给了我与上述相同的错误。角 11。
猜你喜欢
  • 2019-09-30
  • 2019-02-01
  • 2019-08-23
  • 1970-01-01
  • 2017-04-01
  • 2018-10-01
  • 1970-01-01
  • 2018-02-07
  • 1970-01-01
相关资源
最近更新 更多