tnblog
首页
视频
资源
登录
不帅~~但是很暖心.....
排名
14
文章
75
粉丝
21
评论
43
申请别的接口数据(网络接口)
是伍尚金哇 : 敲一夜代码,流下两三行泪水,掏空四肢五体,六杯白开水七桶泡面
mui框架-移动端跳转以及传值的简单方法(修改解决方法)
是伍尚金哇 : 测试了 可以直接在 extras: { userid:'10' //自定义扩展...
数据库的varchar和nvarchar的区别
是伍尚金哇 : 没人看 自己看一个 温习一下
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术

angular路由导航配置

4624人阅读 2020/3/23 15:35 总访问:372838 评论:2 收藏:0 手机
分类: Angular

开发工具:VSCode

打开终端输入命令创建crisis-list和hero-list组件

  1. ng generate component crisis-list
  1. ng generate component hero-list

再app文件夹下面自动创建了两个组件

要使用路由器,必须先注册来自@angular/route包中的RouterModule,定义一个路由数组appRoutes并且把它传给RouterModule.forRoot()方法。他会返回一个模块,其中包含配置好的Router服务,以及路由库里所需要的提供包这些。一旦启动了应用,Router就会根据当前的浏览器URL地址进行首次导航访问。在app.module.ts文件里

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

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { CrisisListComponent } from './crisis-list/crisis-list.component';
import { HeroListComponent } from './hero-list/hero-list.component';
import {RouterModuleRoutesfrom '@angular/router';
import { NotFoundComponent } from './not-found/not-found.component';


const appRoutesRoutes = [
  { path: 'crisis-center'component: CrisisListComponent },
  { path: 'heroes'component: HeroListComponent },
  { path: '',   redirectTo: '/heroes'pathMatch: 'full' },
  { path: '**'component: NotFoundComponent },
];

@NgModule({
  declarations: [
    AppComponent,
    CrisisListComponent,
    HeroListComponent,
    NotFoundComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    FormsModule,
    RouterModule.forRoot(
      appRoutes,
      { enableTracing: true } // <-- debugging purposes only
    )
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

路由出口,angular的路由出口需要依赖于


<router-outlet></router-outlet>      

在app.component.html编写相关的html代码 并且超链接的点击事件

<h1>父页面</h1>
<nav>
  <a routerLink="/crisis-center" routerLinkActive="active">Crisis Center</a>
  <a routerLink="/heroes" routerLinkActive="active">Heroes</a>
</nav>
<!--路由出口-->
<router-outlet></router-outlet>   

routerLink属性的后面值是和app.module.ts里得路由规则进行匹配跳转相应的路径页面,routerLinkActive属性在这篇文章不做解释  不影响


现在来说说配置路由规则,回到app.module.ts文件里面

path: 'crisis-center'component: CrisisListComponent

path表示url显示的路由,例如:http://localhost:4200/crisis-center,component表示组件名称,指到那个组件就跳转哪个组件模板(页面)


path: '',   redirectTo: '/heroes'pathMatch: 'full'

path为'' 表示不输入规则进行访问时会匹配到此规则,例如http://localhost:4200,但hi为了避免没有组件可访问,加上redirceTo重定向路由,意思是如果在地址栏直接输入http://localhost:4200他会默认访问http://localhost:4200/heroes


path: '**'component: NotFoundComponent

这个路由规则比较特殊,当你输入错的路由,全部没有匹配到时就会访问到此规则,通常拿来做404-not-find-page页面


注意三种路由规则依次排序顺序,不能乱,

const appRoutesRoutes = [
  { path: 'crisis-center'component: CrisisListComponent },
  { path: 'heroes'component: HeroListComponent },
  { path: '',   redirectTo: '/heroes'pathMatch: 'full' },
  { path: '**'component: NotFoundComponent },
];

看效果:

评价

剑轩

2020/3/23 21:39:18

angular有点帅

2020/3/24 15:39:44 回复

angular使用组件时遇到的一些错误整理

先看看代码exportclassSidenavComponentimplementsOnInit{ privatemediaMatcher:MediaQueryList=matchMedia(`(max-width...

angular使用第三方资源(举例面向插件)

问题Example :我们需要一张svg到页面中1.把图片放在assets文件夹下面以move.svg为例2.需要对单个资源进行模板注册import{C...

angular使用Proxy

Multiple entriesIf you need to proxy multiple entries to the same target define the configuration inproxy.conf.jsin...

angular环境搭建

开发工具:vscode命令执行:快捷键( Ctrl+Shift+` )或者打开终端近期一直写有关前端的东西,其实我是学后端C#.NET的,但是在...

angular常用的创建命令

Angular开发在Angular开发中常见的一些命令创建命令ng generate component example 生成组件带有模版ng generate component...

关于angular常用的创建命令。

在Angular开发中常见的一些命令创建命令ng generate component example生成组件带有模版ng generate component example -it...

基于angular的HttpClient封装_HttpClient

基于angular的HttpClient封装_HttpClient,直接复制源码即可用包含常用的get、post、patch、delete和put请求;import{HttpC...

angular使用加载指示符loading的ngx-loading

前言欢迎使用tnblog如果你的项目是纯angular需要用到请求或者其他方式时在在界面上通过loading的方式来展示请求进度或其他...

angular与Layui框架集成使用

目录" class="reference-link">目录 目录前言初使用 下载layui资源包创建angular项目 搭建angular环境(已搭建忽略)创...

angular与Layui框架集成使用(优化)

在使用后很多入坑 全局引入所有模块 Layui的所有模块可以全部一次性加载 所有模块可通过layui/lay/modules,这里可...

angular路由复用策略 (切换tabs页面内容不丢失,保持原来状态)

一、引言 路由在执行过程中对组件无状态操作,即路由离退时组件状态也一并被删除;当然在绝大多数场景下这是合理的。进入...

angular路由复用策略

路由在执行过程中对组件无状态操作,即路由离退时组件状态也一并被删除;当然在绝大多数场景下这是合理的。进入组件时会执...

vs core 运行报An unhandled exception occurred: spawn EPERM See "C:\Users\ADMINI~1\AppData\Local\Temp\ng-HWFidp\angular-errors.log"

前言:hello,大家好 I’m小付 好久不见,老实巴交的我又出现了, 今天给大家分享一下vs core运行报错遇到的一个坑。正文:...

angular 基本常用语法

常用语法//引导 import { platformBrowserDynamic } from &#39;@angular/platform-browser-dynamic&#39;;platformBrowserDy...

angular项目导入到本地运行报错

前 言:hello,大家好 我是你们熟悉的小付,就在咋天那个风高秋月的夜晚我又遭遇了项目的报错,看起来原因很简单以为只是简...