IONIC ERROR Error: Uncaught (in promise): Error: Cannot find module '../home/home.module' Error: Cannot find module '../home/home.module' - ionic-framework

Firstly, when i login the error show:
ERROR Error: Uncaught (in promise): Error: Cannot find module '../home/home.module'
Error: Cannot find module '../home/home.module'
Here the code for the error part:
import { NgModule } from '#angular/core';
import { CommonModule } from '#angular/common';
import { FormsModule } from '#angular/forms';
import {Routes, RouterModule} from '#angular/router';
import { IonicModule } from '#ionic/angular';
import { MenuPage } from './menu.page';
const routes: Routes =[
{
path: '',
component: MenuPage,
children: [
{
path: 'home',
loadChildren: '**../home/home.module#HomePageModule**'
}, /* '../home/home.module#HomePageModule' */
{
path: 'tabs',
loadChildren: () => import('../tabs/tabs.module').then( m => m.TabsPageModule)
}, /* '../tabs/tabs.module#TabsPageModule' */
{
path: 'status',
loadChildren: '../status/status.module#StatusPageModule'
},
{
path: 'butiranmaklumat',
loadChildren: '../butiran-maklumat/butiran-maklumat.module#ButiranMaklumatPageModule'
},
{
path: 'tabs/butiranmaklumat',
loadChildren: '../butiran-maklumat/butiran-maklumat.module#ButiranMaklumatPageModule'
},
{
path: 'butiranpekerjaan',
loadChildren: '../butiran-pekerjaan/butiran-pekerjaan.module#ButiranPekerjaanPageModule'
},
{
path: 'tabs/butiranpekerjaan',
loadChildren: '../butiran-pekerjaan/butiran-pekerjaan.module#ButiranPekerjaanPageModule'
},
{
path: 'butiranpasangan',
loadChildren: '../butiran-pasangan/butiran-pasangan.module#ButiranPasanganPageModule'
},
{
path: 'tabs/butiranpasangan',
loadChildren: '../butiran-pasangan/butiran-pasangan.module#ButiranPasanganPageModule'
},
{
path: 'maklumattanggungan',
loadChildren: '../maklumat-tanggungan/maklumat-tanggungan.module#MaklumatTanggunganPageModule'
},
{
path: 'tabs/maklumattanggungan',
loadChildren: '../maklumat-tanggungan/maklumat-tanggungan.module#MaklumatTanggunganPageModule'
},
{
path: 'sejarahpermohonan',
loadChildren: '../sejarah-permohonan/sejarah-permohonan.module#SejarahPermohonanPageModule'
},
{
path: 'tabs/sejarahpermohonan',
loadChildren: '../sejarah-permohonan/sejarah-permohonan.module#SejarahPermohonanPageModule'
}
/* {
path: '',
redirectTo: 'home',
pathMatch: 'full'
}
*/
]
}
];
#NgModule({
imports: [
CommonModule,
FormsModule,
IonicModule,
RouterModule.forChild(routes)
],
declarations: [MenuPage]
})
export class MenuPageModule {}
So, i try to delete ".." on the bold line and write back ".." . and it can successfully login.
But the other error show:
ERROR TypeError: datas.pr_master_pengguna is not iterable
Here the code for this part:
import { Component, OnInit } from '#angular/core';
import {Provider} from '../../providers/provider';
import {Router} from '#angular/router';
import {Storage} from '#ionic/storage';
#Component({
selector: 'app-login',
templateUrl: './login.page.html',
styleUrls: ['./login.page.scss'],
})
export class LoginPage {
LoginPengguna:string;
KataLaluan:string;
task:any[];
constructor(
private provider:Provider,
private router:Router,
private storage: Storage
) { }
login(){
return new Promise(resolve =>{
let body = {
LoginPengguna:this.LoginPengguna,
KataLaluan:this.KataLaluan
};
this.provider.postData(body, 'login.php').subscribe(data => {
// console.log(data);
if(data['user']){
this.storage.set('IdPengguna', data['user'][0]['IdPengguna']);
this.router.navigate(['menu/tabs/tabs/home']);
}else {
alert("Login failed");
}
});
})
}
}
but for this error, i just refresh it and data show.
So i try to terminate and run it back. and the error still show the same. it just like cache in my code.
Hopefully u guys can help.

Error was this part
children: [
{
path: 'home',
loadChildren: '**../home/home.module#HomePageModule**'
}, /* '../home/home.module#HomePageModule' */
change it
// Full path including `src` at the start:
loadChildren: '/home/home.module#HomePageModule'
or this:
// Relative path from the `app-routing.module.ts` file:
loadChildren: './home/home.module#HomePageModule'

Related

Setting opening page in app-routing module doesn't redirect as expected

I have walkthrough page built using ion-slides.
I had set this page to be the first page to open when the app had launched in app-routing.module.ts:
{
path: '',
redirectTo: 'walkthrough',
pathMatch: 'full'
},
However if I change the starting page to any other page the app will consistently start with the waklthourgh page:
{
path: '',
redirectTo: 'login',
pathMatch: 'full'
},
All paths are set correctly:
{
path: 'walkthrough',
loadChildren: () => import('./walkthrough/walkthrough.module').then(m => m.WalkthroughPageModule)
},
{
path: 'login',
loadChildren: () => import('./auth/login/login.module').then( m => m.LoginPageModule)
},
Any input appreciated.
EDIT: adding login routing.module.ts:
import { NgModule } from '#angular/core';
import { Routes, RouterModule } from '#angular/router';
import { LoginPage } from './login.page';
const routes: Routes = [
{
path: '',
component: LoginPage
}
];
#NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
})
export class LoginPageRoutingModule {}

My ionic 5 refresh is not working, when i m trying to refresh "Domain/home/dashboard" in my browser the page goes blank

I have built an app, which has 2 types of routes
Domain/login
Domain/home/dashboard
1st domain works perfectly
2nd domain also works but when i refresh the page like "Domain/home/dashboard" my page goes blank
given below is the screenshot
Before login index.module handles the pages, like login, sign up etc
Index-routing
import { NgModule } from "#angular/core";
import { Routes, RouterModule } from "#angular/router";
import { IndexPage } from "./index.page";
import { IndexGuard } from "../guards/index.guard";
const routes: Routes = [
{
path: "",
component: IndexPage,
canActivate: [IndexGuard],
children: [
{
path: "",
loadChildren: () => import("../pages/welcome/welcome.module").then((m) => m.WelcomePageModule)
},
{
path: "login",
loadChildren: () => import("../pages/login/login.module").then((m) => m.LoginPageModule)
},
{
path: "signup",
loadChildren: () => import("../pages/signup/signup.module").then((m) => m.SignupPageModule)
} /*
{
path: "home",
loadChildren: () => import("../home/home.module").then((m) => m.HomePageModule)
}*/
]
}
];
#NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class IndexPageRoutingModule {}
Index.guard
import { Injectable } from "#angular/core";
import { CanActivate, Router } from "#angular/router";
import { AuthConstants } from "../config/auth-constant";
import { StorageService } from "../services/storage.service";
#Injectable({
providedIn: "root"
})
export class IndexGuard implements CanActivate {
constructor(public storageService: StorageService, public router: Router) {}
canActivate(): Promise<boolean> {
return new Promise((resolve) => {
this.storageService
.get(AuthConstants.AUTH)
.then((res) => {
if (res) {
this.router.navigate(["home/poultry"]);
resolve(false);
} else resolve(true);
})
.catch((err) => {
resolve(true);
});
});
}
}
After logging in home.module handles page like dashboard etc.
Home.routing
import { NgModule } from "#angular/core";
import { Routes, RouterModule } from "#angular/router";
import { HomePage } from "./home.page";
import { HomeGuard } from "../guards/home.guard";
import { UserDataResolver } from "../resolvers/userData.resolver";
const routes: Routes = [
{
path: "home",
component: HomePage,
canActivate: [HomeGuard],
resolve: {
userData: UserDataResolver
},
children: [
{
path: "feed",
loadChildren: () => import("../pages/feed/feed.module").then((m) => m.FeedPageModule)
},
{
path: "notifications",
loadChildren: () =>
import("../pages/notifications/notifications.module").then((m) => m.NotificationsPageModule)
},
{
path: "messages",
loadChildren: () => import("../pages/messages/messages.module").then((m) => m.MessagesPageModule)
},
{
path: "webinar",
loadChildren: () => import("../pages/webinar/webinar.module").then((m) => m.WebinarPageModule)
},
{
path: "webinar-detail/:id",
loadChildren: () =>
import("../pages/webinar-detail/webinar-detail.module").then((m) => m.WebinarDetailPageModule)
},
{
path: "feed-detail/:id",
loadChildren: () =>
import("../pages/feed-detail/feed-detail.module").then((m) => m.FeedDetailPageModule)
},
{
path: "dashboard",
loadChildren: () => import("../pages/dashboard/dashboard.module").then((m) => m.DashboardPageModule)
},
{
path: "book/:id",
loadChildren: () => import("../pages/book/book.module").then((m) => m.BookPageModule)
},
{
path: "book-title/:id/:id2",
loadChildren: () => import("../pages/book-title/book-title.module").then((m) => m.BookTitlePageModule)
},
{
path: "book-content/:id/:id2/:id3",
loadChildren: () =>
import("../pages/book-content/book-content.module").then((m) => m.BookContentPageModule)
},
{
path: "view-pdf",
loadChildren: () => import("../pages/view-pdf/view-pdf.module").then((m) => m.ViewPdfPageModule)
},
{
path: 'poultry',
loadChildren: () => import('../pages/poultry/poultry.module').then( m => m.PoultryPageModule)
},
{
path: 'grocery',
loadChildren: () => import('../pages/grocery/grocery.module').then( m => m.GroceryPageModule)
},
{
path: 'vegitables',
loadChildren: () => import('../pages/vegitables/vegitables.module').then( m => m.VegitablesPageModule)
},
{
path: 'cart',
loadChildren: () => import('../pages/cart/cart.module').then( m => m.CartPageModule)
},
{
path: 'thankyou',
loadChildren: () => import('../pages/thankyou/thankyou.module').then( m => m.ThankyouPageModule)
}
]
}
];
#NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class HomePageRoutingModule {}
Home guard
import { Injectable } from "#angular/core";
import { CanActivate, Router } from "#angular/router";
import { AuthConstants } from "../config/auth-constant";
import { StorageService } from "../services/storage.service";
#Injectable({
providedIn: "root"
})
export class HomeGuard implements CanActivate {
constructor(public storageService: StorageService, public router: Router) {}
canActivate(): Promise<boolean> {
return new Promise((resolve) => {
this.storageService
.get(AuthConstants.AUTH)
.then((res) => {
if (res) {
resolve(true);
} else {
this.router.navigate(["login"]);
resolve(false);
}
})
.catch((err) => {
resolve(false);
});
});
}
}
app.routing
import { NgModule } from "#angular/core";
import { PreloadAllModules, RouterModule, Routes } from "#angular/router";
const routes: Routes = [
{
path: "",
loadChildren: () => import("./index/index.module").then((m) => m.IndexPageModule)
},
{
path: "",
loadChildren: () => import("./home/home.module").then((m) => m.HomePageModule)
},
{
path: "webinar",
loadChildren: () => import("./pages/webinar/webinar.module").then((m) => m.WebinarPageModule)
}
];
#NgModule({
imports: [RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules, onSameUrlNavigation: "reload" })],
exports: [RouterModule]
})
export class AppRoutingModule {}

How to do a lazy loaded ionic framework app with a login page

I have an app that is working great, but I want to move it behind a login page. The various modules are lazy-loaded and have been working great. However, when I change the app to always go to the login page first (where I will check login status and redirect to the app if logged in), I get an error about routes.
app-routing.module.ts
import { NgModule } from '#angular/core';
import { PreloadAllModules, RouterModule, Routes } from '#angular/router';
const routes: Routes = [
{ path: '', loadChildren: './login/login.module#LoginPageModule' }
// this next line was how the app routed before I tried adding the login page
// { path: '', loadChildren: './tabs/tabs.module#TabsPageModule' }
];
#NgModule({
imports: [
RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
],
exports: [RouterModule]
})
export class AppRoutingModule {}
login.router.module.ts
import { NgModule } from '#angular/core';
import { RouterModule, Routes } from '#angular/router';
import { LoginPage } from './login.page';
const routes: Routes = [
{ path: '', component: LoginPage },
];
#NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
})
export class LoginRoutingModule { }
login.page.ts
import { ChangeDetectionStrategy, Component } from '#angular/core';
import { Router } from '#angular/router';
import { Store } from '#ngrx/store';
import { AppState } from '../_store/store/app.store';
import { filter } from 'rxjs/operators';
#Component({
selector: 'app-login',
templateUrl: 'login.page.html',
styleUrls: ['login.page.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class LoginPage {
constructor(private router: Router, private store: Store<AppState>) {
this.userSubscription = this.store.select(state => state.users.user).pipe(
filter(user => !!user)
).subscribe(user => {
if (user) {
// THIS IS WHERE THE ERROR HAPPENS
this.router.navigate(['/tabs']);
}
});
}
}
this.router.navigate(['/tabs']); is where the error happens:
ERROR Error: Uncaught (in promise): Error: Cannot match any routes.
URL Segment: 'tabs/behaviors' Error: Cannot match any routes. URL
Segment: 'tabs/behaviors'
I'm sure I'm missing something really obvious here. First attempt at lazy loading all the modules. I'm pretty certain I need to reference the tabs module in the login.page file somehow, or in the login.router.module. Any help would be greatly appreciated. The state check for user status works great, I've verified that all of that is working, it is just where it attempts to navigate if user is found.
Try to change to
const routes: Routes = [
{ path: '', redirectTo: 'login', pathMatch: 'full' },
{ path: 'login', loadChildren: './login/login.module#LoginPageModule' }
{ path: 'tabs', loadChildren: './tabs/tabs.module#TabsPageModule' }
];
With this code this.router.navigate(['/tabs']); you are routing to tabs, but you don't have it declare it. This is for the app.routing.ts file. Don't understand why you have the login.router.ts file.
So after following a suggestion above, and that giving me the same error, and finding no help in the docs for either angular or ionic, I went on a multi-hour change every combination of routings I could and finally something worked.
These are the routes in the app-routing.module.ts
{ path: 'login', loadChildren: './login/login.module#LoginPageModule' },
{ path: 'tabs', loadChildren: './tabs/tabs.module#TabsPageModule' },
{ path: '', redirectTo: 'login', pathMatch: 'full' },
This is the tabs.router.module.ts before the change that made it work:
{ path: 'tabs', component: TabsPage, children: [ .... ] },
{ path: '', redirectTo: 'tabs/behaviors', pathMatch: 'full' }
This is it now:
{ path: '', component: TabsPage, children: [ ... ] },
That's it. Finally figured it out when accidentally typing in the browser .com:8100/tabs/tabs/behaviors worked. Removing the path: 'tabs' in the tabs routing module fixed it. /tabs goes to the tabs routing, and then /behaviors is the child. My code had added a 2nd layer of /tabs in between the 1st and the /behavior.

PageNotFound route in root routes triggered after outsourcing routes to component

my routes used to work fine when they were all together, the notfound route
{ path: '**', component: PageNotFoundComponent}
was at the last place to capture any other not defined paths.
After I moved the recipe routes to its own module these are never called. Instead, the pagenotfound is called.
Everything works fine if I remove the PageNotFoundComponent route from the root routes. Any ideas regarding whats going on here?
This is the root app routing module:
import { Routes, RouterModule } from '#angular/router';
import { NgModule, OnInit } from '#angular/core';
import { ShoppingListComponent } from './shopping-list/shopping-list.component';
import { PageNotFoundComponent } from './errors/page-not-found/page-not-found.component';
import { AuthComponent } from './auth/auth.component';
const appRoutes: Routes = [
{ path: '', redirectTo: 'recipes', pathMatch: 'full' },
{ path: 'shopping-list', component: ShoppingListComponent },
{ path: 'auth', component: AuthComponent },
{ path: '**', component: PageNotFoundComponent}
];
#NgModule({
imports: [RouterModule.forRoot(appRoutes)],
exports: [RouterModule]
})
export class AppRoutingModule {
OnInit() {
console.log(appRoutes);
}
}
This is the child recipe routing module:
import { NgModule } from '#angular/core';
import { Routes, RouterModule } from '#angular/router';
import { RecipesComponent } from './recipes.component';
import { AuthGuard } from '../auth/auth.guard';
import { RecipeStartComponent } from './recipe-start/recipe-start.component';
import { RecipeEditComponent } from './recipe-edit/recipe-edit.component';
import { RecipeDetailComponent } from './recipe-detail/recipe-detail.component';
import { RecipesResolverService } from './recipes-resolver.service';
const routes: Routes = [
{
path: 'recipes', component: RecipesComponent, canActivate: [AuthGuard] , children: [
{ path: '', component: RecipeStartComponent },
{ path: 'new', component: RecipeEditComponent },
{
path: ':id',
component: RecipeDetailComponent,
resolve: [RecipesResolverService]
},
{
path: ':id/edit',
component: RecipeEditComponent,
resolve: [RecipesResolverService]
},
]
}
];
#NgModule({
declarations: [
],
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class RecipesRoutingModule {
}
Thanks for taking the time to get this far, any idea would be appreciated.
The problem was that the wildcard route ('**') always should be at the end of your routes list because of the way it works.
It watches all the routes before it, and checks if any given URL, that the user is searching on matches those.
Since you've outsourced your recipes paths, and probably imported them into app.module, those paths get concatenated after the original path list that you have in your app-routing.module.
Therefore your paths in the recipe routing module end up being AFTER the wildcard route ('**'), so they get ignored by it. That's why searching on any URL listed in the recipe routing module will reroute the user to the wild card path, to your PageNotFoundComponent.
Great solution tho.
I solved the problem lazy loading all the routes and then adding the PageNotFound route '**' at the end, hope this helps anyone that faced the same problem:
import { Routes, RouterModule } from '#angular/router';
import { NgModule } from '#angular/core';
import { PageNotFoundComponent } from './errors/page-not-found/page-not-found.component';
// If new and :id children were inverted that would make angular take new as id
// ant that would break the app, the order of the routes is very important
// that's why the 404 PageNotFoundComponent goes the last one
const appRoutes: Routes = [
{ path: '', redirectTo: 'recipes', pathMatch: 'full' },
{ path: 'recipes', loadChildren: () => import('./recipes/recipes.module').then(m => m.RecipesModule) },
{ path: 'shopping-list', loadChildren: () => import('./shopping-list/shopping-list.module').then( m => m.ShoppingListModule) },
{ path: 'auth', loadChildren: () => import('./auth/auth.module').then( m => m.AuthModule) },
{ path: '**', component: PageNotFoundComponent }
];
#NgModule({
imports: [RouterModule.forRoot(appRoutes)],
exports: [RouterModule]
})
export class AppRoutingModule {
}

LazyLoading routing each time creates new Component in memory and does not destroy it after loading new same component

I use:
nativescript 4.2
angular 6.0.0
page-router-outlet
navigation from page to page by 'lazyloading' (lateral navigation):
export const routes = [
{
path: "",
loadChildren: "~/pages/start-page/start-page.module#StartPageModule"
},
{
path: "login",
loadChildren: "~/pages/login/login.module#LoginModule"
}, ...
navigate with RouterExtensions with 'clearHistory = true'
When i navigate from page 'Home' to another page and return to 'Home' i see in memory two component 'HomeComponent'. When I repeat the operation their becomes three, four, five, .....
chrome debug window
Try to this for lazy loading routing.
app-routing.module.ts :-
import { NgModule } from "#angular/core";
import { Routes } from "#angular/router";
import { NativeScriptRouterModule } from "nativescript-angular/router";
const routes: Routes = [
{ path: "", redirectTo: "/login", pathMatch: "full" },
{ path: "startpage", loadChildren: "~/pages/start-page/start-page.module#StartPageModule" },
{ path: "login", loadChildren: "~/pages/login/login.module#LoginModule" }
]
#NgModule({
imports: [NativeScriptRouterModule.forRoot(routes)],
exports: [NativeScriptRouterModule]
})
export class AppRoutingModule { }
Redirection Code:-
import { NgZone } from "#angular/core";
import { isIOS } from "tns-core-modules/platform";
import { RouterExtensions } from "nativescript-angular/router";
constructor(private zone: NgZone,
private _routerExtensions: RouterExtensions){ }
gotoStartPage() {
setTimeout(() => {
this.zone.run(() => {
this._routerExtensions.navigate(["startpage"], {
clearHistory: (isIOS) ? true : false,
});
});
}, 500);
}