元素隐式具有 'any' 类型,因为类型 'ActivatedRouteSnapshot' 没有索引签名
Element implicitly has an 'any' type because type 'ActivatedRouteSnapshot' has no index signature
我正在使用 Angular2 创建简单的路由,我想从对象 属性 名称传递给 url 并且一切正常,我的链接是动态生成的,但几秒钟后我收到该错误:
我的路由器链接代码:
<a [routerLink]="['category/', electronic.name]" *ngFor="let electronic of electronics ">
{{electronic.name}}
</a>
我的组件代码:
import { Component, OnInit } from '@angular/core';
import { DatabaseService } from "../../service/database.service";
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-category-list',
templateUrl: './category-list.component.html',
styleUrls: ['./category-list.component.css']
})
export class CategoryListComponent implements OnInit {
constructor(private database: DatabaseService, private activeRoute: ActivatedRoute) { }
ngOnInit() {
let name = this.activeRoute.snapshot['name'];
console.log(name);
}
}
TypeScript 编译器抱怨以下行
let name = this.activeRoute.snapshot['name'];
这是因为如果您查看 Angular docs,它在 ActivatedRouteSnapshot
界面上没有索引器来像您尝试的那样检索信息。索引器看起来像这样:
interface Example {
[key:string]: any;
}
您需要查看文档以了解使用该对象的正确方法。
我正在使用 Angular2 创建简单的路由,我想从对象 属性 名称传递给 url 并且一切正常,我的链接是动态生成的,但几秒钟后我收到该错误:
我的路由器链接代码:
<a [routerLink]="['category/', electronic.name]" *ngFor="let electronic of electronics ">
{{electronic.name}}
</a>
我的组件代码:
import { Component, OnInit } from '@angular/core';
import { DatabaseService } from "../../service/database.service";
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-category-list',
templateUrl: './category-list.component.html',
styleUrls: ['./category-list.component.css']
})
export class CategoryListComponent implements OnInit {
constructor(private database: DatabaseService, private activeRoute: ActivatedRoute) { }
ngOnInit() {
let name = this.activeRoute.snapshot['name'];
console.log(name);
}
}
TypeScript 编译器抱怨以下行
let name = this.activeRoute.snapshot['name'];
这是因为如果您查看 Angular docs,它在 ActivatedRouteSnapshot
界面上没有索引器来像您尝试的那样检索信息。索引器看起来像这样:
interface Example {
[key:string]: any;
}
您需要查看文档以了解使用该对象的正确方法。