I'm developping an Hybrid App with IONIC 4.
The first page is a login (left) the second the home once authenticated (right):
As you can see the content for each tab is hidden on the bottom and only the component header is shown.
Here is some code:
Home routes
import { NgModule } from '#angular/core';
import { RouterModule, Routes } from '#angular/router';
import { HomePage } from './home.page';
const routes: Routes = [
{
path: 'tabs',
component: HomePage,
children: [
{
path: 'newsfeed',
outlet: 'newsfeed',
loadChildren: '../news-feed/news-feed.module#NewsFeedPageModule'
},
{
path: 'sitters',
outlet: 'sitters',
loadChildren: '../sitters/sitters.module#SittersPageModule'
},
{
path: 'userprofile',
outlet: 'userprofile',
loadChildren: '../user-profile/user-profile.module#UserProfilePageModule'
}
]
},
{
path: '',
redirectTo: '/home/tabs/(newsfeed:newsfeed)'
}
];
#NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class HomePageRoutingModule { }
Homepage router outlet and tabs
<ion-tabs color="warning" useRouter="true">
<ion-tab label="Feed" icon="paw" href="/home/tabs/(newsfeed:newsfeed)">
<ion-router-outlet stack name="newsfeed"></ion-router-outlet>
</ion-tab>
<ion-tab label="Sitters" icon="people" href="/home/tabs/(sitters:sitters)">
<ion-router-outlet stack name="sitters"></ion-router-outlet>
</ion-tab>
<ion-tab label="Profile" icon="person" href="/home/tabs/(userprofile:userprofile)">
<ion-router-outlet stack name="userprofile"></ion-router-outlet>
</ion-tab>
</ion-tabs>
Each tab component has somethig like
<ion-header>
<ion-toolbar>
<ion-title>news-feed</ion-title>
</ion-toolbar>
</ion-header>
<ion-content padding>
<ion-list>
<ion-item>
<p>Hello there.</p>
</ion-item>
</ion-list>
</ion-content>
But the content is always on the bottom. Any ideas? Thanks in advance warriors
I just encounter the same issue. And i spent almost 4 days trying to figure it out !
This is because of lazy loading. So you must do that :
import { NgModule } from '#angular/core';
import { RouterModule, Routes } from '#angular/router';
import NewsfeedPage from '../news-feed/news-feed.page';
import SittersPage from '../sitters/sitters.page';
import UserProfilePage from '../user-profile/user-profile.page';
import { HomePage } from './home.page';
const routes: Routes = [
{
path: 'tabs',
component: HomePage,
children: [
{
path: 'newsfeed',
outlet: 'newsfeed',
component: NewsFeedPage
},
{
path: 'sitters',
outlet: 'sitters',
component: SittersPage
},
{
path: 'userprofile',
outlet: 'userprofile',
loadChildren: UserProfilePage
}
]
},
{
path: '',
redirectTo: '/home/tabs/(newsfeed:newsfeed)'
}
];
#NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class HomePageRoutingModule { }
Don't forget to import the modules in your home.module.ts
Hope it will help.
Related
In a project that I have to do I was copying some lines from a youtube project, trying to connect the login page to a mysql database. All the project is in a htdocs folder of xampp, also there is a server_api folder with a config.php (I describe its content below) and a file_aski.php (with nothing in it). Please any help would be great! Thanks.
The files that I describe below are:
config.php
app.module.ts
app.component.ts
app-routing.module.ts
login.page.ts
login.page.html
---------- config.php ----------
<?php
// setting config database
define('DB_NAME', 'dbtotalinspector');
define('DB_USER', 'root');
define('DB_PASSWORD', ''); // default
define('DB_HOST', 'localhost'); // default xampp
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
?>
---------- app.module.ts ----------
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { RouteReuseStrategy } from '#angular/router';
import { IonicModule, IonicRouteStrategy } from '#ionic/angular';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
/* import { HttpModule } from '#angular/http';
import { PostProvider } from '../providers/post-provider';
import { IonicStorageModule } from '#ionic/storage-angular';
*/
#NgModule({
declarations: [AppComponent],
imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule /* , IonicStorageModule.forRoot(), PostProvider*/],
providers: [{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy } /*, PostProvider */ ],
bootstrap: [AppComponent],
})
export class AppModule {}
---------- app.component.ts ----------
import { Component } from '#angular/core';
#Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.scss'],
})
export class AppComponent {
constructor() {}
}
---------- app-routing.module.ts ----------
import { NgModule } from '#angular/core';
import { PreloadAllModules, RouterModule, Routes } from '#angular/router';
const routes: Routes = [
{
path: 'home',
loadChildren: () => import('./home/home.module').then( m => m.HomePageModule)
},
{
path: '',
redirectTo: 'login',
pathMatch: 'full'
},
{
path: 'login',
loadChildren: () => import('./login/login.module').then( m => m.LoginPageModule)
},
{
path: 'seleccionar-obra',
loadChildren: () => import('./seleccionar-obra/seleccionar-obra.module').then( m => m.SeleccionarObraPageModule)
},
{
path: 'cargar-obra',
loadChildren: () => import('./cargar-obra/cargar-obra.module').then( m => m.CargarObraPageModule)
},
{
path: 'cargar-item',
loadChildren: () => import('./cargar-item/cargar-item.module').then( m => m.CargarItemPageModule)
},
{
path: 'desplegar-items-de-obra',
loadChildren: () => import('./desplegar-items-de-obra/desplegar-items-de-obra.module').then( m => m.DesplegarItemsDeObraPageModule)
},
{
path: 'tomar-fotografia',
loadChildren: () => import('./tomar-fotografia/tomar-fotografia.module').then( m => m.TomarFotografiaPageModule)
},
{
path: 'generar-informe',
loadChildren: () => import('./generar-informe/generar-informe.module').then( m => m.GenerarInformePageModule)
},
{
path: 'confirmar-enviar-informe',
loadChildren: () => import('./confirmar-enviar-informe/confirmar-enviar-informe.module').then( m => m.ConfirmarEnviarInformePageModule)
},
{
path: 'desplegar-foto',
loadChildren: () => import('./desplegar-foto/desplegar-foto.module').then( m => m.DesplegarFotoPageModule)
},
];
#NgModule({
imports: [
RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
],
exports: [RouterModule]
})
export class AppRoutingModule { }
---------- login.page.ts ----------
import { Component, OnInit } from '#angular/core';
import { Router } from '#angular/router';
#Component({
selector: 'app-login',
templateUrl: './login.page.html',
styleUrls: ['./login.page.scss'],
})
export class LoginPage implements OnInit {
constructor(private router: Router) { }
ngOnInit() {
}
btnClickIngresar(){
this.router.navigateByUrl('/home');
}
}
---------- login.page.html ----------
<app-header></app-header>
<ion-content>
<div class="container">
<ion-card>
<ion-card-header>
<ion-card-title>Iniciar sesión</ion-card-title>
</ion-card-header>
<ion-card-content>
<ion-item>
<ion-icon name="person-outline" slot="start"></ion-icon>
<ion-item>
<ion-label position="floating">Usuario</ion-label>
<ion-input></ion-input>
</ion-item>
</ion-item>
<ion-item>
<ion-icon name="lock-open-outline" slot="start"></ion-icon>
<ion-item>
<ion-label position="floating">Contraseña</ion-label>
<ion-input type="password"></ion-input>
</ion-item>
</ion-item>
<ion-button expand="full" (click)="btnClickIngresar();">INGRESAR</ion-button>
</ion-card-content>
</ion-card>
</div>
</ion-content>
Thanks for all the advices, I'm reading the guidelines. Ok, I see that I have to cut the code to the most importan parts. As I see, the main problem is in "post-providers.ts" and maybe in in "app.module.ts" too. I took out the code between /* */ I will show you how the code looks and the error messages.
post-providers.ts
// providers api
import { Injectable } from "#angular/core";
import { Http,Headers, RequestOptions } from '#angular/http';
import 'rxjs/add/operator/map';
#Injectable()
export class PostProvider {
server: string = "http://localhost/server_api";
constructor (public http: Http){
}
postData(body, file){
let type = "application/json; charset=UTF-8";
let headers = new Headers({ 'Content-Type': type });
let options = new RequestOptions ({ headers: headers });
return this.http.post(this.server + file, JSON.stringify(body), options)
.map(res => res.json());
}
}
NOTE: on ".map" of the code above it says "Property 'map' does not exist on type 'Observable'.ts(2339)"
app.module.ts
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { RouteReuseStrategy } from '#angular/router';
import { IonicModule, IonicRouteStrategy } from '#ionic/angular';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { HttpModule } from '#angular/http';
import { PostProvider } from '../providers/post-provider';
import { IonicStorageModule } from '#ionic/storage-angular';
#NgModule({
declarations: [AppComponent],
imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule, IonicStorageModule.forRoot(), PostProvider, HttpModule],
providers: [{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }, PostProvider ],
bootstrap: [AppComponent],
})
export class AppModule {}
NOTE: in app.module.ts, the "HttpModule" is cross out (or strike out) in both, "import {}" and the imports inside #NgModule of
The site appears in blank and it is compiled succesfully. But I don't see the login page.
THE ERRORS ARE:
./node_modules/rxjs/add/operator/map.js:7:0-39 - Error: Module not found: Error: Can't resolve 'rxjs-compat/add/operator/map' in 'C:\xampp\htdocs\totalinspector\node_modules\rxjs\add\operator'
[ng]
[ng] Error: src/providers/post-provider.ts:21:10 - error TS2339: Property 'map' does not exist on type 'Observable<Response>'.
[ng]
[ng] 21 .map(res => res.json());
[ng] ~~~
[ng] × Failed to compile.
I am using the following code in visual studio. Here is the routing part when I click to FAQ component through page component and perform according in FAQ component backs to page component.
Here is routing part
import { NgModule } from '#angular/core';
import { RouterModule, Routes } from '#angular/router';
import { PageComponent } from './components/page/page.component';
import { FaqComponent } from './Folder/faq/faq.component';
import { HomeComponent } from './Folder/home/home.component';
const routes: Routes = [
{path: 'login', component: HomeComponent },
{path: 'data', component: FaqComponent },
{ path: '', component: PageComponent },
];
#NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
FaqComponent=>PageComponent
data => /
<a routerLink="/">xxx</a>
I have a project on angular 9. where i want execute Three components in my homepage named header, body, and footer. and i have other two componets login and signup. These are working fine but homepage components are not working. Only header component is loading, others are not. I have found similar questions but they are not solving my problem
what i have tried
app-routing.module.ts
import { NgModule } from '#angular/core';
import { Routes, RouterModule } from '#angular/router';
import { LoginComponent } from './component/login/login.component';
import { SignupComponent } from './component/signup/signup.component';
import { HeaderComponent } from './component/header/header.component';
import { BodyComponent } from './component/body/body.component';
import { FooterComponent } from './component/footer/footer.component';
const routes: Routes = [
{
path: '' , redirectTo: 'header,body,footer', pathMatch: 'full'
},
{
path: 'header', component:HeaderComponent
},
{
path: 'body' , component:BodyComponent
},
{
path: 'footer', component:FooterComponent
},
{
path: 'login', component:LoginComponent
},
{
path: 'signup' , component:SignupComponent
}
];
#NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
app.component.html
<router-outlet></router-outlet>
Thankyou
you need to create one common component called home component and include header,body,footer into that
home.component.html
<app-header></app-header>
<router-outlet></router-outlet>
<app-footer></app-footer>
route
{
path: 'home', component:HomeComponent
},
{
path: '' , redirectTo: 'home', pathMatch: 'full'
},
Problem Statement
I have implemented the Angular Material date picker but it doesn't allow for me to view it when I navigate to the page with it in there. Instead, it just redirects me back to the home page for some extremely strange reason even though this has nothing to do with routing!
Code
This is the code that redirects me to the home page whenever I try to view it. This does not work, even though it compiles:
<mat-form-field class="dobContain">
<input matInput [matDatepicker]="datePicker" class="field" maxlength="30" type="text"
placeholder="Date of Birth">
<mat-datepicker-toggle matSuffix [for]="datePicker"></mat-datepicker-toggle>
<mat-datepicker #datePicker></mat-datepicker>
</mat-form-field>
This is the code that works (the same input field but without the date picker):
<mat-form-field class="dobContain">
<input matInput class="field" maxlength="30" type="text" placeholder="Date of Birth">
</mat-form-field>
Explanation of Code
The first block of code blocks my access to view it. Whenever I use that piece of code and try to navigate to the page containing that code, it just redirects me back to the home page. Weird!
The second block of code works perfectly and is the same code, but it excludes the date picker. Nothing more to it.
Expected Results
I want to use the Angular Material datepicker for the date of birth field in the form.
Actual Results
The datepicker (for some reason) redirects me to the home page whenever I route to the page containing the datepicker code.
Addtional Information
My routing module
const routes: Routes = [
{ path: "home", pathMatch: "full", component: HomeComponent },
{ path: "products", pathMatch: "full", component: ProductsComponent },
{ path: "productX", pathMatch: "full", component: ProductXComponent },
{ path: "account", pathMatch: "full", component: AccountComponent },
{ path: "login", pathMatch: "full", component: AccountLoginComponent },
{ path: "signUp", pathMatch: "full", component: AccountSignUpComponent }, // The component with the datepicker
];
#NgModule({
imports: [RouterModule.forRoot(routes, { anchorScrolling: 'enabled' })
],
exports: [RouterModule]
})
export class AppRoutingModule { }
Imports
import { BrowserModule } from '#angular/platform-browser';
import { NgModule } from '#angular/core';
import { MatButtonModule } from '#angular/material/button';
import { MatDialogModule } from '#angular/material/dialog';
import { MatCardModule } from '#angular/material/card';
import { MatStepperModule } from '#angular/material/stepper';
import { MatInputModule } from '#angular/material/input';
import { MatDatepickerModule } from '#angular/material/datepicker';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '#angular/platform-browser/animations';
import { HomeComponent, GoldenCircleComponent } from './home/home.component';
import { ProductsComponent } from './products/products.component';
import { ProductXComponent } from './productX/productX.component';
import { AccountComponent } from './account/account.component';
import { AccountLoginComponent } from './account-login/account-login.component';
import { AccountSignUpComponent } from './account-sign-up/account-sign-up.component';
#NgModule({
declarations: [
AppComponent,
HomeComponent,
ProductsComponent,
ProductXComponent,
GoldenCircleComponent,
AccountComponent,
AccountLoginComponent,
AccountSignUpComponent
],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
MatButtonModule,
MatDialogModule,
MatCardModule,
MatStepperModule,
MatInputModule,
MatDatepickerModule
],
providers: [],
bootstrap: [AppComponent],
// Needed for mat dialog module
exports: [
GoldenCircleComponent
],
entryComponents: [
GoldenCircleComponent
]
})
export class AppModule { }
The component where I am navigating to the page with the datepicker.
<body>
<h1 class="title">Account</h1>
<button mat-raised-button id="login" class="accountBtns" routerLink="/login">Login</button>
<!-- The button that takes me to the datepicker page -->
<button mat-raised-button class="accountBtns" routerLink="/signUp">Sign Up</button>
</body>
After looking in my console, I found that there is an error message saying: ERROR Error: MatDatepicker: No provider found for DateAdapter. Then, after some research on Google, I found that you have to import the following:
import { MatDatepickerModule } from '#angular/material/datepicker';
import { MatNativeDateModule } from '#angular/material/core';
...
imports: [
...
MatDatepickerModule,
MatNativeDateModule
],
And place MatDatepickerModule in providers:
...
providers: [
MatDatepickerModule
],
...
This fixed my issue. I was stuck on this problem for 3 days because I didn't look in my console for an error. Now I know to look at my console whenever I have difficulties. Need to get in the habit of looking in my console...
Have a good day everyone!
I am migrating my application from ionic 3 to ionic 4 and I am wondering how to keep showing the navtabs when navigating between pages. When I do this:
[routerLink]="'/offers/'" routerDirection="forward"
The tabs disappear, what do I have to do when I almost always want to show the navtabs?
Define the routes in tabs.router.module.ts or in the pages?
Hope somebody could help me out on this.
Something like this is doing it for me. So basically yes, define the routes in your router's routing module.
app.module.ts
import { NgModule } from '#angular/core';
import { RouteReuseStrategy } from '#angular/router';
import { IonicModule, IonicRouteStrategy } from '#ionic/angular';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
#NgModule({
declarations: [AppComponent],
entryComponents: [],
imports: [
IonicModule.forRoot(),
AppRoutingModule,
],
providers: [
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
],
bootstrap: [AppComponent],
})
export class AppModule {
}
app-routing.module.ts
import { NgModule } from '#angular/core';
import { RouterModule, Routes } from '#angular/router';
const routes: Routes = [
{ path: '', redirectTo: 'start/tabs', pathMatch: 'full' },
{ path: 'start', loadChildren: './pages/tabs/tabs.module#TabsModule' },
];
#NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export class AppRoutingModule {
}
tabs.module.ts
import { CommonModule } from '#angular/common';
import { NgModule } from '#angular/core';
import { IonicModule } from '#ionic/angular';
import { PageAPageModule } from '../pageA/PageA.module';
import { PageBPageModule } from '../pageB/PageB.module';
import { TabsPage } from './tabs.page';
import { TabsPageRoutingModule } from './tabs.page-routing.module';
#NgModule({
imports: [
CommonModule,
IonicModule,
PageAPageModule,
PageBPageModule,
TabsPageRoutingModule,
],
declarations: [
TabsPage,
],
})
export class TabsModule {
}
tabs.page-routing.module.ts
import { NgModule } from '#angular/core';
import { RouterModule, Routes } from '#angular/router';
import { PageAPage } from '../pageA/pageA.page';
import { TabsPage } from './tabs.page';
const routes: Routes = [
{
path: 'tabs',
component: TabsPage,
children: [
{
path: 'pageA',
children: [
{ path: '', component: PageAPage },
{ path: 'subPageA1', loadChildren: '../subPageA1/subPageA1.module#SubPageA1PageModule' },
{ path: 'subPageA2', loadChildren: '../subPageA2/subPageA2.module#SubPageA2PageModule' },
],
},
{
path: 'pageB',
children: [
{ path: '', loadChildren: '../pageB/pageB.module#PageBPageModule' },
{ path: ':id', loadChildren: '../pageB-detail/pageB-detail.module#PageBDetailModule' },
],
},
{
path: 'pageX',
...
},
{
path: '', redirectTo: '/start/tabs/pageA', pathMatch: 'full',
},
],
},
];
#NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
})
export class TabsPageRoutingModule {
}
tabs.page.html
<ion-tabs>
<ion-tab-bar color="dark" slot="bottom">
<ion-tab-button tab="pageA">
<ion-icon name="calendar"></ion-icon>
<ion-label>pageA</ion-label>
</ion-tab-button>
<ion-tab-button tab="pageB">
<ion-icon name="contacts"></ion-icon>
<ion-label>pageB</ion-label>
</ion-tab-button>
<ion-tab-button tab="pageX">
<ion-icon name="contacts"></ion-icon>
<ion-label>pageX</ion-label>
</ion-tab-button>
</ion-tab-bar>
</ion-tabs>
app.component.html
<ion-list>
<ion-menu-toggle auto-hide="false">
<ion-item routerLink="/start/tabs/pageA">
<ion-icon slot="start" name="person"></ion-icon>
<ion-label>pageA</ion-label>
</ion-item>
</ion-menu-toggle>
<ion-menu-toggle auto-hide="false">
<ion-item routerLink="/start/tabs/pageA/subPageA1">
<ion-icon slot="start" name="person"></ion-icon>
<ion-label>subPageA1</ion-label>
</ion-item>
</ion-menu-toggle>
<ion-menu-toggle auto-hide="false">
<ion-item routerLink="/start/tabs/pageB">
<ion-icon slot="start" name="person"></ion-icon>
<ion-label>pageB</ion-label>
</ion-item>
</ion-menu-toggle>
<ion-menu-toggle auto-hide="false">
<ion-item routerLink="/start/tabs/pageX">
<ion-icon slot="start" name="person"></ion-icon>
<ion-label>pageX</ion-label>
</ion-item>
</ion-menu-toggle>
</ion-list>