【Angular】路由跳转(代码跳转)

【WHAT】
     在Angular中,路由的作用就是建立URL路径和组件(页面,因为页面就是由组件构成)之间的对应关系,根据不同的URL路径匹配出相应的组件并渲染。


【HOW】

1. 定义路由配置
2. 创建根路由模块
3. 添加路由插座
(以上是基本且必须的三个步骤)


【示例:(文件如下)】
          这里写图片描述
     1.在app.routes定义路由,主要设置有:

  • 设置启动的第一个路由
  • 定义要在根目录下启动所有子路由
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
          在showmain.component.ts文件中设置在doIn()触发时跳转路由

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") 
  }
}

          (之所以第二个页面要设置跳转,而跳转到第一个子路由没用到该句,是因为在根路由下已设置了项目启动后跳转的第一个路由)
          

  • 在showmain.component.html设置页面
<h1>欢迎加入大米时代!</h1>
<button (click)="doIn()">进入系统</button>

     7.定义第二个子路由LoginRoutes
          login.routes.ts

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.设置第二个路由即页面的主要内容
          login.component.ts文件

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>

程序执行,访问文件的顺序依次是:
这里写图片描述

思路是这样的:
     在app.module中设置了根路由AppRoutes,于是开始执行app.module中,而app.module设置了跳转的第一个子路由showmain,在此文件中有该路由,于是去设置的路径下去找ShowmainModule,于是执行到了showmain.module,去showmain.routes找该路由,该路由被渲染的组件为ShowmainComponent,于是又执行到了showmain.component文件中,而此文件中又设置了一个绝对路径下的路由login,于是又执行到app.routes下查找有无该路由,有则跳转到指定目录下的login.module文件下找LoginModule,在login.module定义了该路由LoginRoutes,于是去login.routes找到了该路由,该路由被渲染的组件为LoginComponent,于是去login.component文件中找到该组件,执行方法,结束![之所以回去app.routes下查找路由,是根据它设置的找路径找的this.router.navigateByUrl("login")]


【扩展】

  1. declarations:声明自己写的东西(如组件)
  2. imports:通常是导入程序已封装号的东西(如router的forRoot()方法)
  3. 快捷键

    • 新建文件(指定目录下):              ng g c 文件名
    • 新建module:                            ng g module 名称
    • 启动服务的同时在浏览器中打开:ng serve --open
    • 该运行的端口:                          ng serve --port 端口号
    • 标签:                                       直接打标签名+双击Tab(不用输<)
  4. 有数值双向绑定时,无论html中用没用到表单,都要导入forms:import { FormsModule } from '@angular/forms';

  • 12
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 32
    评论
Angular是一个流行的前端框架,用于构建Web应用程序。它由Google开发并维护,采用TypeScript语言编写。Angular提供了一套完整的工具和功能,用于开发现代化的单页应用程序(SPA)和响应式Web应用程序。 Angular的主要特点包括: 1. 组件化架构:Angular使用组件化架构来构建应用程序。每个组件都有自己的模板、样式和逻辑,并可以嵌套在其他组件中,以构建复杂的用户界面。 2. 双向数据绑定:Angular支持双向数据绑定,使得数据的变化可以自动反映在视图中,同时用户的输入也可以自动更新数据模型。 3. 依赖注入:Angular使用依赖注入来管理组件之间的依赖关系。这样可以更好地组织和测试代码,并提高代码的可维护性和可扩展性。 4. 路由和导航:Angular提供了强大的路由和导航功能,可以实现页面之间的无缝切换和导航。 5. 响应式表单:Angular提供了响应式表单功能,可以轻松地处理表单的验证、状态管理和用户输入。 6. 强大的工具集:Angular配备了丰富的工具集,包括开发者工具、测试工具和构建工具,可以提高开发效率和代码质量。 下面是一个简单的Angular组件的示例: ```typescript import { Component } from '@angular/core'; @Component({ selector: 'app-hello', template: ` <h1>Hello, {{ name }}!</h1> <button (click)="changeName()">Change Name</button> `, }) export class HelloComponent { name = 'Angular'; changeName() { this.name = 'World'; } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 32
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值