typescript 绑定元素“index”隐式具有“any”类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40745992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:01:46  来源:igfitidea点击:

Binding element 'index' implicitly has an 'any' type

angulartypescriptmaterial-designangular2-mdl

提问by InsaneBot

Using the demo project of angular2-mdlas a guide I ported the tab component and tried to implement it as follow:

angular2-mdldemo工程为指导,我移植了tab组件,尝试实现如下:

import { Component } from '@angular/core';

@Component({
    selector: 'my-dashboard',
    templateUrl: './landing.my.html'
})
export class MyDashboard {
    public activeIndex = 0;

    public tabChanged({index}): void {
        this.activeIndex = index;
    }

}

and the template is:

模板是:

<mdl-tabs mdl-ripple mdl-tab-active-index="0" (mdl-tab-active-changed)="tabChanged($event)">
    <mdl-tab-panel mdl-tab-panel-title="home">
        <mdl-tab-panel-title>
            <mdl-icon class="mdl-color-text--primary">home</mdl-icon><span>Home</span>
        </mdl-tab-panel-title>
        <mdl-tab-panel-content>
            <ul>
                <li>Stanis</li>
                <li>Joffrey</li>
            </ul>
        </mdl-tab-panel-content>
    </mdl-tab-panel>
    <mdl-tab-panel mdl-tab-panel-title="something">
        <mdl-tab-panel-title>
            <mdl-icon class="mdl-color-text--primary">group_work</mdl-icon><span>Ontology</span>
        </mdl-tab-panel-title>
        <mdl-tab-panel-content>
            <ul>
                <li>Stanis</li>
                <li>Joffrey</li>
            </ul>
        </mdl-tab-panel-content>
    </mdl-tab-panel>
    <mdl-tab-panel mdl-tab-panel-title="another">
        <mdl-tab-panel-title>
            <mdl-icon class="mdl-color-text--primary">list</mdl-icon><span>Cognitive</span>
        </mdl-tab-panel-title>
        <mdl-tab-panel-content>
            <ul>
                <li>Robert</li>
            </ul>
        </mdl-tab-panel-content>
    </mdl-tab-panel>
    <mdl-tab-panel mdl-tab-panel-title="else">
        <mdl-tab-panel-title>
            <mdl-icon class="mdl-color-text--primary">call_split</mdl-icon><span>Cognition</span>
        </mdl-tab-panel-title>
        <mdl-tab-panel-content>
            <ul>
                <li>Robert</li>
                <li>Renly</li>
            </ul>
        </mdl-tab-panel-content>
    </mdl-tab-panel>
    <mdl-tab-panel mdl-tab-panel-title="last">
        <mdl-tab-panel-title>
            <mdl-icon class="mdl-color-text--primary">backup</mdl-icon><span>Streaming</span>
        </mdl-tab-panel-title>
        <mdl-tab-panel-content>
            <ul>
                <li>Joffrey</li>
                <li>Myrcella</li>
                <li>Tommen</li>
            </ul>
        </mdl-tab-panel-content>
    </mdl-tab-panel>
</mdl-tabs>

I am using webpack, and i get the following error:

我正在使用 webpack,但出现以下错误:

ERROR in [default] home/my-app-ui/src/app/landing.my.ts:10:23 
Binding element 'index' implicitly has an 'any' type.

however the app displays the desired functionality, can someone explain how to fix this ?

但是应用程序显示了所需的功能,有人可以解释如何解决这个问题吗?

采纳答案by Thyagarajan C

use anyor specific type of the variable like ( numbers,string,etc )

使用any或特定类型的变量,如(数字、字符串等)

public tabChanged(index:any): void {
    this.activeIndex = index;
}

回答by lwpro2

for an object, you need to declare the type as

对于对象,您需要将类型声明为

{index} : {index:any}

回答by Nicolas Henneaux

It is a check of the type script compiler. You can either remove the check or specify explicitly the any type in the declaration (answer from @Thyagu). In the tsconfig.jsonfile, you could change the line

它是对类型脚本编译器的检查。您可以删除检查或明确指定声明中的任何类型(来自@Thyagu 的回答)。在tsconfig.json文件中,您可以更改行

"noImplicitAny": false,

to

"noImplicitAny": true,