Angular7.2.7路由使用初体验

 更新时间:2019年03月01日 11:12:24   作者:默默无闻  
这篇文章主要介绍了Angular7.2.7路由使用初体验,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

本文记录自己在学习angular中对路由的理解

angular路由不在angular的包中, 如果要使用angular的路由, 需要从@angular/router中引入

路由的使用:

子路由使用:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

const route: Routes = [
{path: 'browse-product', component: BrowseProductComponent},
{path: 'buy-product', component: BuyProductComponent}
]

@NgModule({
imports: [RouterModule.ferChild(route)], // 子路由使用ferChild方法
exports: [RouterModule]
})

export class ChildRoutingModule {}

父组件中:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

const route: Route = [
{
  path: '',
  children: [
   {path: '', redirectTo: '/user/browse-product', pathMatch: 'full'},
   {path: 'user', loadChildren: './user/user.module#UserModule'},
   {path: 'admin', loadChildren: './admin/admin.module#AdminModule'},
  ]
 }
]

@NgModule({
imports: [RouterModule.forRoot(root, {useHash: false})],
exports: [RouterModule]
})

export class ParentRoutingModule {}

在app.module.ts中导入模块

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { ParentRoutingModule } from 'parentRoutingModule';
@NgModule({
declarations: [
  AppComponent,
 ],
 imports: [
  BrowserModule,
  RouterModule,
  ParentRoutingModule
 ]
})

然后在app.component.html中加入router-outlet即可, router-outlet就是路由的出口, 表示路由对应的组件应该在这里显示.

<h1>Angular Router</h1>
<nav>
 <a routerLink="/user/browse-product">browse-product</a> &nbsp;
 <a routerLink="/user/buy-product">buy-product</a>&nbsp;
 <a routerLink="/admin/manage-product">manage-product</a>&nbsp;
 <a routerLink="/admin/operator-record">operator-record</a>&nbsp;
</nav>
<router-outlet></router-outlet>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 详解Angularjs 自定义指令中的数据绑定

    详解Angularjs 自定义指令中的数据绑定

    这篇文章主要介绍了Angularjs 自定义指令中的数据绑定,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • AngularJS 自定义过滤器详解及实例代码

    AngularJS 自定义过滤器详解及实例代码

    这篇文章主要介绍了AngularJS 自定义过滤器,这里整理了相关资料及示例代码,有兴趣的小伙伴可以参考下
    2016-09-09
  • 详解Angular组件数据不能实时更新到视图上的问题

    详解Angular组件数据不能实时更新到视图上的问题

    这篇文章主要为大家介绍了Angular组件数据不能实时更新到视图上的问题详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • angular ng-repeat数组中的数组实例

    angular ng-repeat数组中的数组实例

    下面小编就为大家带来一篇angular ng-repeat数组中的数组实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02
  • 在Angular中实现懒加载的示例代码

    在Angular中实现懒加载的示例代码

    在Angular中,懒加载技术通过路由配置实现模块的按需加载,可优化应用启动时间和减少初始加载代码量,首先创建独立模块,在模板中使用<router-outlet>插入懒加载组件,并可采用预加载策略如PreloadAllModules,以提前加载所有懒加载模块,优化加载性能
    2024-10-10
  • AngularJS实现根据变量改变动态加载模板的方法

    AngularJS实现根据变量改变动态加载模板的方法

    这篇文章主要介绍了AngularJS实现根据变量改变动态加载模板的方法,结合实例形式简单分析了AngularJS动态加载模板的主要操作技巧与模板实现代码,需要的朋友可以参考下
    2016-11-11
  • angular 实时监听input框value值的变化触发函数方法

    angular 实时监听input框value值的变化触发函数方法

    今天小编就为大家分享一篇angular 实时监听input框value值的变化触发函数方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • 如何以Angular的姿势打开Font-Awesome详解

    如何以Angular的姿势打开Font-Awesome详解

    这篇文章主要给大家介绍了关于如何以Angular的姿势打开Font-Awesome的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Angular具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2018-04-04
  • 详解如何在Angular优雅编写HTTP请求

    详解如何在Angular优雅编写HTTP请求

    这篇文章主要介绍了详解如何在Angular优雅编写HTTP请求,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-12-12
  • angularjs实现table增加tr的方法

    angularjs实现table增加tr的方法

    下面小编就为大家分享一篇angularjs实现table增加tr的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-02-02

最新评论