【Angular】路由跳转(代码跳转)
【WHAT】 【HOW】 【示例:(文件如下)】
export const AppRoutes=[
{
path: '',redirectTo: 'showmain',//项目启动的第一个路由为showmain
pathMatch: 'full'
},{
path: 'showmain',loadChildren: './showmain/showmain.module#ShowmainModule'
},{
path: 'login',loadChildren: './login/login.module#LoginModule'
}
]
2.创建根路由(app.module.ts),主要设置有
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import {RouterModule} from '@angular/router'; //导入路由
import { AppComponent } from './app.component';
import {AppRoutes} from './app.routes';
@NgModule({
declarations: [
AppComponent
],imports: [
BrowserModule,FormsModule,HttpModule,RouterModule,//设置主要路由
RouterModule.forRoot(AppRoutes)
],providers: [],bootstrap: [AppComponent]
})
export class AppModule { }
3.定义插座(在app.component.html) <router-outlet></router-outlet>
4.定义子路由showmain.routes.ts import { ShowmainComponent } from './showmain.component';
export const ShowmainRoutes= [
{
path: '',component: ShowmainComponent
}
]
5.设置子路由ShowmainRoutes import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import {RouterModule} from '@angular/router';
import {ShowmainRoutes} from './showmain.routes';
import { ShowmainComponent } from './showmain.component';
@NgModule({
imports: [
CommonModule,//设置ShowmainRoutes为子路由
RouterModule.forChild(<any>ShowmainRoutes)
],declarations: [
ShowmainComponent
]
})
export class ShowmainModule { }
6.设置跳转的下一个子路由login import { Component,OnInit } from '@angular/core';
import { Router} from '@angular/router'; //导入router服务
@Component({
selector: 'app-showmain',templateUrl: './showmain.component.html',styleUrls: ['./showmain.component.css']
})
export class ShowmainComponent implements OnInit {
constructor(private router: Router) { }
ngOnInit() {
}
doIn(): void {
//跳转到login路由(绝对路径)
this.router.navigateByUrl("login")
}
}
(之所以第二个页面要设置跳转,而跳转到第一个子路由没用到该句,是因为在根路由下已设置了项目启动后跳转的第一个路由)
<h1>欢迎加入大米时代!</h1>
<button (click)="doIn()">进入系统</button>
7.定义第二个子路由LoginRoutes import { LoginComponent } from './login.component';
export const LoginRoutes= [
{
path: '',component: LoginComponent
}
]
8.设置LoginRoutes为子路由 import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import {RouterModule} from '@angular/router';
import{FormsModule}from '@angular/forms';
import { LoginRoutes } from './login.routes';
import { LoginComponent } from './login.component';
@NgModule({
imports: [
CommonModule,//必须导入FormsMoudule,因为此页面用到了数据双向绑定
FormsModule,//将LoginRoutes设置为子路由
RouterModule.forChild(<any>LoginRoutes)
],declarations: [
LoginComponent
]
})
export class LoginModule { }
9.设置第二个路由即页面的主要内容 import { Component,OnInit } from '@angular/core';
@Component({
selector: 'app-login',templateUrl: './login.component.html',styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
public username:string;
public password:string;
doLogin() {
//必须写this
let username = this.username;
let password = this.password;
if (username == "1" && password == "1"){
alert("登录成功")
}
else{
alert("登录失败")
}
}
}
login.component.html文件 <div class="login">
<h1>前台登录</h1>
</div>
<div>
<label for="">用户名:</label>
<input name ="username" type="text" [(ngModel)]="username">
</div>
<div>
<label for="">密 码:</label>
<input name ="password" type="text" [(ngModel)]="password">
</div>
<button (click)="doLogin()">登录</button>
程序执行,访问文件的顺序依次是: 思路是这样的: 【扩展】
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |