0

I am trying to make a breadcrumb component for angular7 but I can't get my array to properly show into my HTML and would like some tips because I can't find it on the internet. if you would be so kind as to help me I am still new to angular and can't think of a better way to make a breadcrumb component.

my component.ts -

breadcrumbLists: Array <any> = []; breadcrumburl: Array <any> = []; constructor(private _router: Router) { } ngOnInit() { this.listenrouting(); } listenrouting(): void { let routerUrl: string, routerList: Array < any > ; let i; let done; this._router.events.subscribe((router: any) => { routerUrl = router.urlAfterRedirects; if (routerUrl && typeof routerUrl === 'string') { this.breadcrumbLists.length = 0; routerList = routerUrl.slice(1).split('/'); this.breadcrumbLists = routerList; if (done != false) { this.breadcrumbLists.forEach(bread => { if (i != null) { this.breadcrumburl.push([{ name: bread }, { url: i + "/" + bread }]) } else { this.breadcrumburl.push([{ name: bread }, { url: "/" + bread }]) } i = "/" + bread }); } console.log("url", this.breadcrumburl) console.log(this.breadcrumbLists) done = false; } }); } 

and then my HTML -

<nav> <div> <li *ngFor="let Breadcrumb of breadcrumburl"> <a routerLink="{{Breadcrumb.url}}">{{Breadcrumb.name}}</a> </li> </div> </nav> 
3
  • 5
    Breadcrumb.url - missed the capital B Commented Feb 20, 2019 at 13:45
  • if that was it I was going to scream but it wasn't it was a mistake in my code to Commented Feb 20, 2019 at 13:47
  • it doesn't work like that but your answer works thanks I tried it because I knew they where nested array's but I thought I tried that but I don't think so because now no errors Commented Feb 20, 2019 at 14:16

1 Answer 1

2

If I'm not wrong, you want to push into breadcrumburl array as -

this.breadcrumburl.push({name: bread, url: i + "/" + bread}); 

Currently, you created nested arrays & pushed 2 elements in all nested arrays in breadcrumburl

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.