Shiv Kumar Ganesh
Shiv Kumar Ganesh

Reputation: 3825

TypeScript throws error "Property 'breadcrumb' does not exist on type 'Data'."

I have a Breadcrumb Component. It works fine as when compiled to JavaScript and also renders the desired result but the IDE throws an error which I am not able to correct. Its says as follows:-

[ts] Property 'breadcrumb' does not exist on type 'Data'.

The code for my breadcrumb is as follows :-

import { Component } from '@angular/core';
import { Router, ActivatedRoute, NavigationEnd } from '@angular/router';
import 'rxjs/add/operator/filter';

import { BreadcrumbModel } from './breadcrumb.model';

@Component({
  selector: 'breadcrumb',
  template: `
  <ol class="breadcrumb"> 
    <li *ngFor="let breadcrumb of breadcrumbs; let last = last">
      <a [routerLink]="breadcrumb.url">{{breadcrumb.label}}</a>
    </li>
  </ol>`
})
export class BreadcrumbComponent {
  breadcrumbs: Array<BreadcrumbModel>;
  constructor(private router: Router, private route: ActivatedRoute) { }
  ngOnInit() {
    this.router.events
      .filter(event => event instanceof NavigationEnd)
      .subscribe(event => {  // note, we don't use event
        this.breadcrumbs = [];
        let currentRoute = this.route.root,
          url = '';
        do {
          let childrenRoutes = currentRoute.children;
          currentRoute = null;
          childrenRoutes.forEach(route => {
            if (route.outlet === 'primary') {
              let routeSnapshot = route.snapshot;
              url += '/' + routeSnapshot.url.map(segment => segment.path).join('/');
              this.breadcrumbs.push({
                label: route.snapshot.data.breadcrumb,
                url: url
              });
              currentRoute = route;
            }
          })
        } while (currentRoute);
      })
  }
}

The code for breadcrumb model is as follows:-

export class BreadcrumbModel{
    label:string;
    url:string;
}

The error I get in the browser is as follows:-

[at-loader] src\app\shared\core\breadcrumb\breadcrumb.component.ts:34:44 
    Property 'breadcrumb' does not exist on type '{ [name: string]: any; }'.
errors  @   client?cd17:80
sock.onmessage  @   socket.js?e5d0:37
EventTarget.dispatchEvent   @   eventtarget.js?3e89:51
(anonymous) @   main.js?45b8:274
SockJS._transportMessage    @   main.js?45b8:272
EventEmitter.emit   @   emitter.js?927b:50
WebSocketTransport.ws.onmessage @   websocket.js?c17e:35
wrapFn  @   zone.js?fad3:1039
ZoneDelegate.invokeTask @   zone.js?fad3:275
Zone.runTask    @   zone.js?fad3:151
ZoneTask.invoke @   zone.js?fad3:345

I dont want to change the Target from ES5 to ES6. This does not seem to be viable solution. The IDE screenshot is below:- enter image description here

Please help to resolve the issue. TSLint does not throw any warning on the browser after I get this error.

Upvotes: 0

Views: 1409

Answers (1)

Poul Kruijt
Poul Kruijt

Reputation: 71961

In angular you 'have to' use the [name] notation to get the value. Obviously, there is no 'have to', because it works either way, but to get the TypeScript compiler to stop complaining use:

route.snapshot.data['breadcrumb'];

Read the Custom Preloading Strategy and scroll down to the app/selective-preloading-strategy.ts sample file

Upvotes: 2

Related Questions