10

If I return to the same routing path it show an error

"Uncaught (in promise): TypeError: Cannot read property 'isActivated' of undefined".

But not show at first time. can any one help me ?

routing

const mainRoutes: Routes = [
    { path: '', pathMatch: 'full', redirectTo: '/home' },
    {
        path: 'home', loadChildren: './apps/home/home.module#HomeModule', canActivate: [AuthGuard],
        resolve: {
            crisis: NavigarionResolve
        }
    }
    { path: '**', component: PageNotFoundComponent }
];

Component

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { AuthService }            from '../main/service/auth.service';

@Component({
  selector: 'home-app',
  templateUrl: 'apps/home/view/home.component.html',
  providers: [AuthService]
})

export class HomeComponent implements OnInit {
  loaded: boolean = false;
  title = 'Customer Management Home';
  slogan = 'Hi ...Home ';
  views: Object[] = [];
  constructor(private _authService: AuthService, private router: Router) {
    this.views = [
      {
        name: "My Account",
        description: "Edit my account information",
        icon: "assignment ind"
      },
      {
        name: "Potential dates",
        description: "Find your soulmate!",
        icon: "pets"
      }
    ];

  }
  logout() {
    this._authService.logout().then(() => {
      this.router.navigate(['/login']);
    });
  }

  ngOnInit() {

  }
}

Navigarion

 this.router.navigate(['/home']);
Sasxa
  • 40,334
  • 16
  • 88
  • 102
Kamruzzaman
  • 1,423
  • 1
  • 12
  • 14
  • could you please add your underlying component code? – Pankaj Parkar Oct 10 '16 at 08:47
  • I edit my post please see it – Kamruzzaman Oct 10 '16 at 09:06
  • 1
    I'm getting the same issue, it seems to have cropped up out of nowhere, I've undone all my changes that I can see since it was last working and it seems to have stuck around.... I can visit any any route fine, but on trying to navigate to any other component after loading the site using the router it throws the OP's exception. – James Oct 14 '16 at 15:33
  • Please add module and authguard code too. – Sefa Oct 16 '16 at 20:15
  • AuthGuard code seems to be missing – luis.mazoni Oct 17 '16 at 14:43
  • 1
    `isActivated` is nowhere in the code you posted. – Günter Zöchbauer Oct 18 '16 at 05:04
  • Why don't you do a search over your project for "isActivated". – Artur Stary Oct 19 '16 at 09:27
  • @GünterZöchbauer I'm getting the same error, and isActivated is nowhere in my project. I've submitted an edit with my stacktrace to the question. It comes from angular router – James Oct 19 '16 at 10:31
  • Can you please edit your question and add the full stack trace. There has to be an `isActivated` somewhere and we have to find where. – Günter Zöchbauer Oct 19 '16 at 10:39
  • 1
    @GünterZöchbauer line 2807 of router.umd.js v3.0.1 in `PreActivation.prototype.deactivateOutletMap` is where it throws from for me. – James Oct 19 '16 at 10:53
  • Seems there is something wrong with the router-outlets. I guess this requires a Plunker that allows to reproduce. – Günter Zöchbauer Oct 19 '16 at 10:55
  • For different route has different template with different , like one route has and other has and when I go from one route to other with outlet compatible routing then the error occur. – Kamruzzaman Oct 24 '16 at 02:07
  • I've never used multiple router outlets, but do you not need to use aux on the second router? See: http://stackoverflow.com/questions/34628848/angular2-multiple-router-outlet-in-the-same-template – James Oct 24 '16 at 08:27

2 Answers2

2

For me the issue was a second instance of the app being loaded. This was caused by an invalid templateUrl within a "control" component.

I moved a spinner component I have from app/components/client/spinner.component to app/components/shared/spinner.component but forgot to update the templateUrl.
This meant that my spinner was loading up inside my other components with the wrong html. I had used the correct path for the component in module.ts, so it wasn't throwing any build errors, but when it couldn't find the spinner.component.html, mvc6 was returning the default index page, so it tried to load up the entire app from inside the component. This inner app was then being removed once the spinner was removed as my content had loaded.

I don't fully understand it, but it appears the router was having some sort of conflict due to the app being loaded again from inside itself. In my case, I was lucky enough to notice that my app was glitching, so I paused javascript during the glitchy part and scanned the html, and noticed there was a <html> tag inside my spinner.

Try setting a breakpoint inside your app.component class and seeing if it gets hit twice, or running document.getElementsByTagName("my-app") (or whatever your app.component is called) in the console and if you have more than 1 element returned you should be able to debug the problem from there. In my case this only showed while the spinner was showing, so if you have components coming and going on your page, try breaking on each one show/hiding.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
James
  • 4,146
  • 1
  • 20
  • 35
0

Your problem is discussed at github, looks like the component dependency issue, the following link may help you:

Uncaught (in promise): TypeError: Cannot read property

Sohail xIN3N
  • 2,951
  • 2
  • 30
  • 29