I'm not able to display my page with the help of ion-menu and not able to click the item - ionic-framework

ion-menu is not working
This is my sidemenu.component.html
<ion-split-pane contentId="menu-content" when="xs">
<ion-menu contentId="menu-content" >
<ion-content>
<ion-item *ngIf="tabSelec == 'lead'" lines="none" routerLink="/hometab/lead
routerDirection="root" routerLinkActive="active-link">
<ion-icon src="assets/imgs/leadsSelec.svg"></ion-icon>
<ion-label> Leads </ion-label>
</ion-item>
<ion-item *ngIf="tabSelec != 'lead'" lines="none" routerLink="/hometab/lead
routerDirection="root" routerLinkActive="active-link">
<ion-icon src="assets/imgs/sitesSelec.svg"></ion-icon>
<ion-label>Leads</ion-label>
</ion-item>
<ion-item *ngIf="tabSelec == 'sites'" lines="none" routerLink="/hometab/sites
[queryParams]="{pri: 'leads', sec: true}" routerDirection="root"
routerLinkActive="active-link">
<ion-icon class="img" src="assets/imgs/sitesSelec.svg"></ion-icon>
<ion-label > Sites </ion-label>
</ion-item>
<ion-item *ngIf="tabSelec != 'sites'" lines="none" routerLink="/hometab/sites
[queryParams]="{pri: 'leads', sec: true}" routerDirection="root"
routerLinkActive="active-link">
<ion-icon class="img" src="assets/imgs/sitesSelec.svg"></ion-icon>
<ion-label> Sites </ion-label>
</ion-item>
</ion-content>
</ion-menu>
<ion-router-outlet id="menu-content"></ion-router-outlet>
</ion-split-pane>
This is my hometab-rotuing.module.ts
{
path:'',
redirectTo: '/hometab/lead.module',
pathMatch: 'full'
},
{
path:'',
component: HometabPage,
children :[
{
path: 'lead',
loadChildren: () => import('../lead/lead.module').then(m => m.LeadPageModule)
},
{
path: 'sites',
loadChildren: () => import('../sites/sites.module').then(m => m.SitesPageModule)
},
{
path: 'testing',
loadChildren: () => import('../testing/testing.module').then(m => m.TestingPageModule)
},
]
},
Any idea how does it suppose to work? It should automatically redirect me to the lead page after login
Also I'm not able to click on my ion-item, any help appreciated

You have to provide <ion-menu> tag with the contentId attribute, which represent the id of the container to show when you navigate with the sidemu.
Example:
<ion-menu contentId="your-main-content-id" side="start">...</ion-menu>
...
//you can use any order valid html container tag
<ion-content id="your-main-content-id">...</ion-content>

Related

Ionic menu for logged pages is not appearing

I want to show a menu template on ionic app but i want first of all a login page and then, when the user is logged, show the ionic template.
Now i have in app.routing.module that if the user is not logged, the app redirects to the loginPage and if the user is logged, redirects to pages routing module where there are many pages like the home page '' = '/home':
App.routing.module
const routes: Routes = [
{ path: '',
loadChildren: () => import('./pages/pages.module').then( m => m.PagesPageModule),
...canActivate(redirectUnauthorizedToLogin)
},
{
path: 'login',
loadChildren: () => import('./pages/login/login.module').then(m => m.LoginPageModule),
...canActivate(redirectLoggedInToHome)
},
];
pages-routing.module.ts
const routes: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{
path: 'home',
loadChildren: () => import('./home/home.module').then(m => m.HomePageModule),
...canActivate(redirectUnauthorizedToLogin)
},
{
path: 'appointments',
loadChildren: () => import('./appointments/appointments.module').then(m => m.AppointmentsPageModule),
...canActivate(redirectUnauthorizedToLogin)
},
{
path: 'customers',
loadChildren: () => import('./customers/customers.module').then(m => m.CustomersPageModule),
...canActivate(redirectUnauthorizedToLogin)
},
];
The app.component:
<ion-app>
<!-- Content -->
<ion-router-outlet id="app"></ion-router-outlet>
<!-- !Content -->
</ion-app>
The login page:
<div class="login">
<ion-card>
<ion-card-content>
<form [formGroup]="loginForm" (ngSubmit)="login()">
<!-- Email -->
<app-base-input label="email.label" [control]="email" errorMessage="email.notValid" formControlName="email">
</app-base-input>
<!-- Password -->
<app-base-input type="password" label="password.label" [control]="password" errorMessage="password.notValid"
formControlName="password"></app-base-input>
<!-- Button -->
<ion-button expand="full" type="submit">{{ 'login.label' | translate }}</ion-button>
</form>
</ion-card-content>
</ion-card>
</div>
Im including the menu on pagePage.component.html that has the pages inside but is not showing the menu.
<!-- Menu -->
<ion-menu contentId="main-content" type="overlay">
<ion-content>
<ion-list id="inbox-list">
<ion-list-header>{{ 'appName' | translate }}</ion-list-header>
<ion-note>hi#ionicframework.com</ion-note>
<ion-menu-toggle auto-hide="false" *ngFor="let p of appPages">
<ion-item routerDirection="root" [routerLink]="[p.url]" lines="none" detail="false" routerLinkActive="selected">
<ion-icon slot="start" [ios]="p.icon + '-outline'" [md]="p.icon + '-outline'"></ion-icon>
<ion-label>{{ p.title | translate }}</ion-label>
</ion-item>
</ion-menu-toggle>
</ion-list>
</ion-content>
</ion-menu>
<!-- !Menu -->
<ion-router-outlet id="main-content"></ion-router-outlet>
How can i add a menu in logged pages?
I'm not really sure what your goal is, but if you just want to show an ion-menu, then you just add it at the next page after the login.
This is an example:
<ion-menu side="start" menuId="first" contentId="main">
<ion-header>
<ion-toolbar color="primary">
<ion-buttons slot="start">
<ion-menu-button autoHide="true"></ion-menu-button>
</ion-buttons>
<ion-title>Menu</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-list>
<ion-item>
<ion-button routerLink="/appointment" fill="clear">Appointments</ion-button>
</ion-item>
<ion-item>
<ion-button routerLink="/customer" fill="clear">Customer</ion-button>
</ion-item>
</ion-list>
</ion-content>
<ion-footer>
<ion-item>
<ion-button fill="clear" (click)="signOut()">Abmelden</ion-button>
</ion-item>
</ion-footer>
</ion-menu>
It's between
<ion-header></ion-header>
//ion-menu here!
<ion-content></ion-content>
If you like to display a menu in your login page then do it with an *ngIf.
userlogin: Boolean = false
Do your login and set userlogin to true.

Ionic 6 ionViewWillEnter cannot read property 0 of undefined

I am learning ionic. So i have added data to an array declared in a service. When i go back to the lists page, ngOnInit will not execute if i have been to that page before. To load the data i need to use ionViewWillEnter or ionViewDidEnter. However, when i move the function call to the ionViewWillEnteri get the following error:
ERROR TypeError: Cannot read property '0' of undefined
at DiscoverPage_Template (template.html:39)
at executeTemplate (core.js:9544)
at refreshView (core.js:9413)
at refreshComponent (core.js:10579)
at refreshChildComponents (core.js:9210)
at refreshView (core.js:9463)
at refreshEmbeddedViews (core.js:10533)
at refreshView (core.js:9437)
at refreshComponent (core.js:10579)
at refreshChildComponents (core.js:9210)
The only way i can get rid of the above error is by calling the function in both ngOnInIt and ionViewWillEnter.
loadedPlaces: Place[];
listedLoadedPlaces: Place[]; //for virtual scroll
constructor(private placesService: PlacesService, private menuCntrl: MenuController) { }
ngOnInit() {
console.log('ngOnInIt');
this.loadData();
}
ionViewWillEnter(){
console.log('ionViewWillEnter');
this.loadData();
}
loadData(){
this.loadedPlaces = this.placesService.places; //getter property
//for the virtual scroll
this.listedLoadedPlaces = this.loadedPlaces.slice(1);
}
I have also tried declaring an empty array and then removing the function call from ngOnInit. In this case i get following issue:
core.js:6157 ERROR TypeError: Cannot read property 'title' of undefined
at DiscoverPage_Template (template.html:39)
at executeTemplate (core.js:9544)
at refreshView (core.js:9413)
at refreshComponent (core.js:10579)
at refreshChildComponents (core.js:9210)
at refreshView (core.js:9463)
at refreshEmbeddedViews (core.js:10533)
at refreshView (core.js:9437)
at refreshComponent (core.js:10579)
at refreshChildComponents (core.js:9210)
My environment
node: v14.15.5
npm: 6.14.11
angular: 11.2.0
ionic: 6.13.1
How can i solve this issue?
Update:HTML
<ion-header>
<ion-toolbar>
<!--menu drawer-->
<ion-buttons slot="start">
<ion-menu-button menu="menu1"></ion-menu-button>
</ion-buttons>
<ion-title>Discover Places</ion-title>
</ion-toolbar>
</ion-header>
<ion-content class="ion-padding">
<ion-segment value="all" (ionChange)="segmentChanged($event)">
<ion-segment-button value="all">All Places</ion-segment-button>
<ion-segment-button value="bookable">Bookable Places</ion-segment-button>
</ion-segment>
<ion-grid>
<!--open the side drawer manually -->
<ion-row>
<ion-button fill="clear" (click)="onMenuDrawerOpen()">
Menu
<ion-icon slot="start" name="apps-outline"></ion-icon>
<ion-icon slot="end" name="checkmark-done-outline" color="primary"></ion-icon>
</ion-button>
</ion-row>
<!--Featured place-->
<ion-row>
<ion-col size="12" size-sm="8" offset-sm="2" class="ion-text-center">
<ion-card>
<ion-card-header>
<ion-card-title>{{ loadedPlaces[0].title }}</ion-card-title>
<ion-card-subtitle>{{ loadedPlaces[0].price | currency }} / Night</ion-card-subtitle>
</ion-card-header>
<ion-img [src]="loadedPlaces[0].imageUrl"></ion-img>
<ion-card-content>
<div>{{ loadedPlaces[0].description}}</div>
</ion-card-content>
<div class="ion-text-right">
<!--fill Clear mean no background -->
<ion-button fill="clear" color="primary" routerDirection="forward" [routerLink]="['/', 'places', 'tabs', 'discover', 'place', loadedPlaces[0].id, 'detail']">
More
<ion-icon slot="start" name="star"></ion-icon>
<ion-icon slot="end" name="arrow-forward-circle-outline" color="primary"></ion-icon>
</ion-button>
</div>
</ion-card>
</ion-col>
</ion-row>
<!--Other places-->
<ion-row>
<ion-col size="12" size-sm="8" offset-sm="2" class="ion-text-center">
<!--since using scroll, removed the for loop from the ion-item moved it to the ion-virtual-scroll-->
<!--it takes the items property -->
<!--get the approxItemHeight using the dev tools and picking the height of an ion-item. The default is 40px-->
<ion-virtual-scroll [items]="listedLoadedPlaces" approxItemHeight="60px">
<!--<ion-list>-->
<!--Excluding the featured item above-->
<!--<ion-item *ngFor="let place of loadedPlaces.slice(1); let i = index" [routerLink]="['/', 'places', 'tabs', 'discover', 'place', place.id, 'detail']" detail>-->
<ion-item [routerLink]="['/', 'places', 'tabs', 'discover', 'place', place.id, 'detail']"
detail
*virtualItem="let place">
<ion-thumbnail slot="start">
<ion-img [src]="place.imageUrl"></ion-img>
</ion-thumbnail>
<ion-label>
<h2>{{ place.title}}</h2>
<p>{{ place.description }}</p>
</ion-label>
</ion-item>
<!--</ion-list>-->
</ion-virtual-scroll>
</ion-col>
</ion-row>
</ion-grid>
</ion-content>
I had to check for the null. Changing to following fixed the issue, did it for all the properties:
loadedPlaces[0]?.title

Side-menu auto open loading time issue in ionic

I use ion-menu component in my app. I change it based route. so I modified that. For ex is below.
My issue is first time menu clicked & the app load next that menu
related view time the side menu was close correctly but reopened next few second.
My approutemodule.ts
[{
path: '',
canActivate: [AuthGuard],
component: SideMenuComponent,
children: [{
path: '',
redirectTo: 'dashboard',
pathMatch: 'full'
}, {
path: 'dashboard',
loadChildren: './pages/dashboard/dashboard.module#DashboardPageModule'
}, {
path: 'tabs-view',
loadChildren: './layouts/footer-tabs/footer-tabs.module#FooterTabsPageModule'
}]
}, {
path: 'login',
loadChildren: './pages/login/login.module#LoginPageModule'
}];
My appcomponent.html
<ion-app>
<ion-router-outlet></ion-router-outlet>
</ion-app>
My sidemenucomponent.html
<ion-split-pane>
<ion-menu id="app-side-manu" #sideMenu>
<ion-header class="user-img">
<ion-toolbar>
<ion-avatar>
<img *ngIf="userImg" [src]="userImg" alt="user-img" />
</ion-avatar>
<div>
<h2>{{userName}}</h2>
<p>{{userLoc}}</p>
</div>
</ion-toolbar>
</ion-header>
<ion-content class="side-menu">
<ion-list>
<ion-menu-toggle auto-hide="false" *ngFor="let p of pages">
<ion-item lines="none" class="link-hover" routerDirection="root" [routerLink]="p.url">
<ion-thumbnail slot="start">
<ion-img [src]="p.icon"></ion-img>
</ion-thumbnail>
<ion-label>
{{p.title}}
</ion-label>
</ion-item>
</ion-menu-toggle>
<ion-menu-toggle auto-hide="false" >
<ion-item lines="none" class="link-hover" (click)="globalHelper.logout()">
<ion-thumbnail slot="start">
<ion-img src="/assets/images/logout.png"></ion-img>
</ion-thumbnail>
<ion-label>Logout</ion-label>
</ion-item>
</ion-menu-toggle>
</ion-list>
</ion-content>
</ion-menu>
<ion-router-outlet main></ion-router-outlet>
</ion-split-pane>
Use side-menu component selector in your app component html file.
For Ex:
<ion-app>
<app-sidemenu-component></app-sidemenu-component>
</ion-app>
If you want side-menu show hide different screen. It's possible.
For Ex:
home screen is need to side-menu
// first import MenuController in your home screen
import { MenuController } from '#ionic/angular';
// Next add that constructor to assign private variable menuCtrl
constructor(private menuCtrl: MenuController){}
// then use it ionViewWillEnter method inside
ionViewWillEnter() {
this.menuCtrl.enable(true);
}
login screen is no need to side-menu
ionViewWillEnter() {
this.menuCtrl.enable(false);
}

refresh app.components in ionic 3

i want refresh app.component because in my sidemenu there is image and name so i store name and image in local storage but when i login and go to dashboard my app.component not refresh so need refresh app.components
My menu file
<ion-menu [content]="content">
<ion-header>
<ion-toolbar>
<ion-title>Menu</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<div class="profile">
<img class="profile-picture" src="assets/imgs/user_profile.png" />
<h3 class="name">{{name}}</h3>
</div>
<ion-list class="bg-color-menu" no-lines>
<ion-item menuClose ion-item *ngFor="let p of pages" (click)="openPage(p)">
<ion-item>
<ion-avatar item-start>
<img [src]="p.icon" />
</ion-avatar>
{{p.title}}
</ion-item>
</ion-item>
</ion-list>
</ion-content>
</ion-menu>
<!-- Disable swipe-to-go-back because it's poor UX to combine STGB with side menus -->
<ion-nav [root]="rootPage" #content swipeBackEnabled="false"></ion-nav>
app.components
name:any;
this.name = localStorage.getItem("name");
Make your user data as BehaviorSubject.
Add a service - user.service.ts
export class UserService {
private currentUserSub = new BehaviorSubject<User>(null);
constructor(
private http: HttpClient
) {
const user = JSON.parse(localStorage.getItem('user'));
this.currentUserSub.next(user);
}
login(loginData): Observable<User> {
return this.http.post('login', loginData)
.map((res: any) => res.data)
.do((user: User) => {
localStorage.setItem('user', JSON.stringify(user));
this.currentUserSub.next(user);
});
}
getCurrentUserDetails(): Observable<User> {
const user = JSON.parse(localStorage.getItem('user'));
this.currentUserSub.next(user);
return this.currentUserSub;
}
}
call getCurrentUserDetails in app.component.ts to get the current user data
getUserDetails() {
this.userService.getCurrentUserDetails().subscribe(res => {
this.userProfile = res;
});
}
In app.html
<div class="profile">
<img class="profile-picture" src="userProfile.imgUrl" />
<h3 class="name">{{userProfile.name}}</h3>
</div>
this is an example. Do it as per your requirement.

ionic 3 youtube api data

I'm having a problem calling the data into the view. Console log is working as I included a screenshot.
ytapiUrl =https://www.googleapis.com/youtube/v3/playlistItems? part=snippet&playlistId='blahblahbla'
getECvideolists(){
return new Promise(resolve => {
this.httpClient.get(this.ytapiUrl).subscribe(ytdata => {
resolve(ytdata);
this.Ecvideos = ytdata;
console.log(ytdata);
}, err => {
console.log(err);
});
});
}
<ion-list *ngSwitchCase="'videos'">
<ion-item *ngFor="let Ecvideo of Ecvideos">
<h2> ***this doesn't display*** {{Ecvideo.items.snippet.title}}</h2>
</ion-item>
</ion-list>
[
items key has an array so you need to iterate it through *ngFor
for example,
<ion-list *ngSwitchCase="'videos'">
<ion-item *ngFor="let item of Ecvideos.items">
<h2>{{item.snippet.title}}</h2>
</ion-item>
</ion-list>