您好,登錄后才能下訂單哦!
本文記錄自己在學(xué)習(xí)angular中對(duì)路由的理解
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中導(dǎo)入模塊
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就是路由的出口, 表示路由對(duì)應(yīng)的組件應(yīng)該在這里顯示.
<h2>Angular Router</h2> <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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。