国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

Angular7.2.7路由使用初體驗

2024-05-06 15:42:00
字體:
來源:轉載
供稿:網友

本文記錄自己在學習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>   <a routerLink="/user/buy-product">buy-product</a>  <a routerLink="/admin/manage-product">manage-product</a>  <a routerLink="/admin/operator-record">operator-record</a> </nav><router-outlet></router-outlet>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持錯新站長站。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 江孜县| 新巴尔虎左旗| 安平县| 四川省| 北安市| 嘉义县| 深泽县| 育儿| 鲁山县| 吴川市| 内乡县| 汨罗市| 黎川县| 息烽县| 大足县| 土默特左旗| 金沙县| 太谷县| 新干县| 五台县| 新民市| 唐海县| 巴南区| 左权县| 德江县| 广元市| 翼城县| 阿克陶县| 安顺市| 柳林县| 都江堰市| 绥滨县| 平邑县| 白水县| 赤壁市| 乳山市| 景宁| 永定县| 普宁市| 阜城县| 固安县|