티스토리 뷰

Angular

[Angular] 라우터모듈

이채야채 2022. 5. 31. 14:22

라우터는 앵귤러의 핵심 라이브러리의 하나이기에

앵귤러의 라우터에서 불러오고있다. import { RouterModule, Routes } from '@angular/router';

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

const routes: Routes = [];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

forRoot같은 경우는 라우터모듈이 루트모듈에 등록이 된다. 정도로 이해하면된다. forChild, forRoot => 앵귤러내의 컨벤션

exports에서 다시 RouterModule을 export하고있다.

 

const routes: Routes = [
  {
path: '',
redirectTo: 'index',
pathMatch: 'full'
  },
];

http://developeritchae.com/ 이면? => 리다이렉트를 http://developeritchae.com/index로 Redirect시키는것을 의미한다. 

 

pathMatch에는 prefix라는 것도 있다. 이것은 path:'123'이라면 http://developeritchae.com/123jfskdjfk 이어도123을 포함하기에 index로 redirect를 시켜준다는 pathMatch속성이다.

 

const routes: Routes = [
  {
    path: '',
    redirectTo: 'index',
    pathMatch: 'full',
  },
  {
    path: 'index',
    component: AppComponent,
  },
];

이런 속성을 주면 실제프로젝트를 확인해보면 index로 간것을 확인 할 수 있다. 

 

 

라우터가 정확하게 적용되려면

라우터 아울렛이 쓰여야된다.

 

어느곳이 적당한 구조일까? 정답은 섹션

 

섹션의 모듈로 가서 수정을 해줘야한다.

 

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { SectionComponent } from './section.component';
import { StopwatchModule } from './stopwatch/stopwatch.module';
import { RouterModule, Routes } from '@angular/router';
import { StopwatchComponent } from './stopwatch/stopwatch.component';

const routes : Routes = [
{
  path : 'stopwatch',
  component : StopwatchComponent
}
]

@NgModule({
  declarations: [SectionComponent],
  exports: [SectionComponent, RouterModule],
  imports: [CommonModule, StopwatchModule, RouterModule.forChild(routes)],
})
export class SectionModule {}

const routes를 만들고 import시 forChlid를 통해서 매칭시킨 후  export해준다.

 


페이지간의 이동


ng g c section/clock 

섹션안에 clock컴포넌트를 만들어주자

 

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { SectionComponent } from './section.component';
import { StopwatchModule } from './stopwatch/stopwatch.module';
import { RouterModule, Routes } from '@angular/router';
import { StopwatchComponent } from './stopwatch/stopwatch.component';
import { ClockComponent } from './clock/clock.component';

const routes: Routes = [
  {
    path: 'stopwatch',
    component: StopwatchComponent,
  },
];

@NgModule({
  declarations: [SectionComponent, ClockComponent],
  exports: [SectionComponent, RouterModule],
  imports: [CommonModule, StopwatchModule, RouterModule.forChild(routes)],
})
export class SectionModule {}

Clock이 생겻음을 확인 할 수 있다.

 

path도 추가해준다.

 

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { SectionComponent } from './section.component';
import { StopwatchModule } from './stopwatch/stopwatch.module';
import { RouterModule, Routes } from '@angular/router';
import { StopwatchComponent } from './stopwatch/stopwatch.component';
import { ClockComponent } from './clock/clock.component';

const routes: Routes = [
  {
    path: 'stopwatch',
    component: StopwatchComponent,
  },
  {
    path: 'clock',
    component: ClockComponent,
  },
];

@NgModule({
  declarations: [SectionComponent, ClockComponent],
  exports: [SectionComponent, RouterModule],
  imports: [CommonModule, StopwatchModule, RouterModule.forChild(routes)],
})
export class SectionModule {}

 

<div class="title">
  <div class="display">
    <app-time-display></app-time-display>
    <!-- <app-buttons (clickEvent)="startTime($event)"></app-buttons> -->
  </div>
  <a [routerLink]="'/clock'">시계로 이동</a>
</div>

'Angular' 카테고리의 다른 글

앵귤러 사이트  (0) 2022.06.09
[Angular]서비스, Dependency Injection, 생명주기  (0) 2022.05.31
[Angular] 모듈의 이해  (0) 2022.05.31
[Angular] 데이터바인딩3-리팩토링  (0) 2022.05.30
[Angular] 데이터바인딩2  (0) 2022.05.30
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG more
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함