Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular routing from app.component.ts

Hello I have angularjs app and routing module (AppRoutingModule) all works good when I am inserting <router-outlet></router-outlet> in app.component.html, but what if I want show some page registered users and not show others.

I mean this in my app.component.ts

if (something) { show example page by default } else { show example2 page }

app-routing.module.ts

import { NgModule } from '@angular/core';
// Routing
import { RouterModule, Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { LoginComponent } from './login/login.component';
import { RegistrationComponent } from './registration/registration.component';

const appRoutes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'login', component: LoginComponent },
  { path: 'registration', component: RegistrationComponent}
];

@NgModule({
  imports: [
    RouterModule.forRoot(
      appRoutes,

    )
  ],
  exports: [
    RouterModule
  ]
})
export class AppRoutingModule {}

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { AlertModule } from 'ngx-bootstrap';

// Import the AF2 Module
import { AngularFireModule } from 'angularfire2';
import { AngularFireAuthModule } from 'angularfire2/auth';
import { AngularFireDatabaseModule } from 'angularfire2/database';
import { environment } from '../environments/environment';

import { AppRoutingModule } from './app-routing.module';
import { HomeComponent } from './home/home.component';
import { LoginComponent } from './login/login.component';
import { RegistrationComponent } from './registration/registration.component';

@NgModule({
  declarations: [
    AppComponent,
    RegistrationComponent,
    HomeComponent,
    LoginComponent
  ],
  imports: [
    BrowserModule,
    AlertModule.forRoot(),
    AngularFireModule.initializeApp(environment.firebase),
    AngularFireAuthModule,
    AngularFireDatabaseModule,
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts

import { Component } from '@angular/core';
import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
  items: FirebaseListObservable<any[]>;
  constructor(db: AngularFireDatabase) {
    this.items = db.list('/sms');
  }
}

app.component.html

<router-outlet></router-outlet>
like image 765
Gocha Avatar asked Dec 05 '25 09:12

Gocha


1 Answers

To navigate between 'pages', you can either add the following in home component class:

import { Router } from '@angular/router';
....

constructor(private router: Router){}
...

And then call from somewhere (like on (click)):

if(condition){
    this.router.navigate(['/page1']);
}
else{
    this.router.navigate(['/page2']);
}

and add this to routes:

.....
  { path: 'page1', component: Page2Component },
  { path: 'page2', component: Page1Component}
....

either add this in home component HTML:

  <a class="home-app-link" routerLink="/page1">
    <h3>Page1</h3>
  </a>

  <a class="home-app-link" routerLink="/page2">
    <h3>Page2</h3>
  </a>
like image 84
Vega Avatar answered Dec 07 '25 23:12

Vega



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!