Angular7.2.7路由使用初體驗(yàn)
本文記錄自己在學(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)該在這里顯示.
<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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Angularjs 自定義指令中的數(shù)據(jù)綁定
這篇文章主要介紹了Angularjs 自定義指令中的數(shù)據(jù)綁定,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
AngularJS 自定義過(guò)濾器詳解及實(shí)例代碼
這篇文章主要介紹了AngularJS 自定義過(guò)濾器,這里整理了相關(guān)資料及示例代碼,有興趣的小伙伴可以參考下2016-09-09
詳解Angular組件數(shù)據(jù)不能實(shí)時(shí)更新到視圖上的問(wèn)題
這篇文章主要為大家介紹了Angular組件數(shù)據(jù)不能實(shí)時(shí)更新到視圖上的問(wèn)題詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
angular ng-repeat數(shù)組中的數(shù)組實(shí)例
下面小編就為大家?guī)?lái)一篇angular ng-repeat數(shù)組中的數(shù)組實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
在Angular中實(shí)現(xiàn)懶加載的示例代碼
在Angular中,懶加載技術(shù)通過(guò)路由配置實(shí)現(xiàn)模塊的按需加載,可優(yōu)化應(yīng)用啟動(dòng)時(shí)間和減少初始加載代碼量,首先創(chuàng)建獨(dú)立模塊,在模板中使用<router-outlet>插入懶加載組件,并可采用預(yù)加載策略如PreloadAllModules,以提前加載所有懶加載模塊,優(yōu)化加載性能2024-10-10
AngularJS實(shí)現(xiàn)根據(jù)變量改變動(dòng)態(tài)加載模板的方法
這篇文章主要介紹了AngularJS實(shí)現(xiàn)根據(jù)變量改變動(dòng)態(tài)加載模板的方法,結(jié)合實(shí)例形式簡(jiǎn)單分析了AngularJS動(dòng)態(tài)加載模板的主要操作技巧與模板實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-11-11
angular 實(shí)時(shí)監(jiān)聽(tīng)input框value值的變化觸發(fā)函數(shù)方法
今天小編就為大家分享一篇angular 實(shí)時(shí)監(jiān)聽(tīng)input框value值的變化觸發(fā)函數(shù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
如何以Angular的姿勢(shì)打開(kāi)Font-Awesome詳解
這篇文章主要給大家介紹了關(guān)于如何以Angular的姿勢(shì)打開(kāi)Font-Awesome的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Angular具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04
詳解如何在Angular優(yōu)雅編寫HTTP請(qǐng)求
這篇文章主要介紹了詳解如何在Angular優(yōu)雅編寫HTTP請(qǐng)求,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
angularjs實(shí)現(xiàn)table增加tr的方法
下面小編就為大家分享一篇angularjs實(shí)現(xiàn)table增加tr的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02

