你好我在app.component.html中插入<router-outlet></router-outlet>
时,我的angularjs app和路由模块(AppRoutingModule)都运行良好,
但如果我想要显示一些页面注册用户而不显示其他用户该怎么办。
我的意思是我的app.component.ts
if(something){ 默认显示示例页面 } else { 显示example2页面 }
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>
答案 0 :(得分:2)
要在&#39;页面&#39;之间导航,您可以在家庭组件类中添加以下内容:
import { Router } from '@angular/router';
....
constructor(private router: Router){}
...
然后从某个地方打电话(比如开(点击)):
if(condition){
this.router.navigate(['/page1']);
}
else{
this.router.navigate(['/page2']);
}
并将其添加到路线:
.....
{ path: 'page1', component: Page2Component },
{ path: 'page2', component: Page1Component}
....
在家庭组件HTML中添加它:
<a class="home-app-link" routerLink="/page1">
<h3>Page1</h3>
</a>
<a class="home-app-link" routerLink="/page2">
<h3>Page2</h3>
</a>
答案 1 :(得分:1)
在app.component.ts中:
import { Router, ActivatedRoute, ParamMap } from '@angular/router';
然后在构造函数中:
if (3 < 2) {
this.router.navigate(['/registration']);
} else {
this.router.navigate(['/login']);
}
答案 2 :(得分:0)
使用 this.router.navigate ([&#39; / 路径&#39;]);
此处路径指的是 app.module.ts 中输入的路径。
如需进一步参考,请查看官方Angular 文档链接 https://angular.io/guide/router