๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป Frontend/Angular

[Angular] Router์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ธฐ (feat. ํ™”๋ฉด์ด๋™)

by Fomagran ๐Ÿ’ป 2022. 3. 24.
728x90
๋ฐ˜์‘ํ˜•

 

์•ˆ๋…•ํ•˜์„ธ์š” Foma ๐Ÿ’ป ์ž…๋‹ˆ๋‹ค!

 

์˜ค๋Š˜์€ Angular์—์„œ ํ™”๋ฉด์„ ์ด๋™ํ•˜๋Š” ๋ฐฉ๋ฒ•์ธ Router์— ๋Œ€ํ•ด ๋‹ค๋ค„๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”~


ํ”„๋กœ์ ํŠธ ์„ค์ •

 

ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ ์‹œ์— "Would you like to Angular routing?(๋„ˆ ์•ต๊ทค๋Ÿฌ ๋ผ์šฐํŒ… ์‚ฌ์šฉํ• ๊ฑฐ์•ผ?)" ๋ผ๊ณ  ๋ฌป๋Š”๋ฐ,

 

์—ฌ๊ธฐ์„œ y๋ฅผ ํ•ด์ค๋‹ˆ๋‹ค.

 

 

login(๋กœ๊ทธ์ธ) ํ™”๋ฉด์„ ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค.

 

ng g c pages/login

 

register(ํšŒ์›๊ฐ€์ž…) ํ™”๋ฉด์„ ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค.

 

ng g c pages/register

 

app-routing.modules.ts

 

Routes์— ์›ํ•˜๋Š” endPoint์™€ component(ํ™”๋ฉด)๋“ค์„ ์„ธํŒ…ํ•ด์ค๋‹ˆ๋‹ค.

 

const routes: Routes = [
  {  path: 'login', component: LoginComponent },
  { path: 'register', component: RegisterComponent }
];

 

ํ™”๋ฉด์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ ์•ต๊ทค๋Ÿฌ ํ”„๋กœ์ ํŠธ๋ฅผ ์‹คํ–‰์‹œ์ผœ ๋ด…๋‹ˆ๋‹ค.

 

ng serve

๊ธฐ๋ณธ ๋ผ์šฐํŒ… ์„ค์ •

 

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๊ธฐ๋ณธ์œผ๋กœ ์„ค์ •๋œ app.component.html์˜ ํ™”๋ฉด์ด ๋‚˜์˜ฌ๊ฑฐ์—์š”.

 

 

app.component.html

 

๊ทธ๋ ‡๋‹ค๋ฉด ๊ธฐ๋ณธ ํ™”๋ฉด ๋ผ์šฐํŒ… ์„ค์ •์€ ์–ด๋–ป๊ฒŒ ํ• ๊นŒ์š”?

 

์šฐ์„  app.component.html ์ฝ”๋“œ๋ฅผ ๋ชจ๋‘ ์ง€์šฐ๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•ด ์ค๋‹ˆ๋‹ค.

 

์•„๋ž˜ ๋œป์€ ๊ธฐ๋ณธ์œผ๋กœ ๋œจ๋Š” ํ™”๋ฉด์„ router์— ๋งž๊ฒŒ ๋„์šธ๊ฑฐ์•ผ ๋ผ๋Š” ๋œป์ž…๋‹ˆ๋‹ค.

 

(๊ธฐ๋ณธ์œผ๋กœ ๋œจ๋Š” ํ™”๋ฉด์€ endPoint๊ฐ€ '' ์•„๋ฌด๊ฒƒ๋„ ์—†๋Š” ํ™”๋ฉด ex) http://localhost:4200์„ ๋œปํ•ฉ๋‹ˆ๋‹ค.)

 

<div class="container">
  <router-outlet></router-outlet>
</div>

 

app-routing.modules.ts

 

๊ทธ๋ ‡๋‹ค๋ฉด Routes์— endPoint๊ฐ€ ์—†์„ ๋•Œ ๊ธฐ๋ณธ ํ™”๋ฉด์„ ์„ค์ •ํ•ด ์ค˜์•ผ๊ฒ ์ฃ ?

 

์•„๋ž˜์™€ ๊ฐ™์ด login ํ™”๋ฉด์„ ๊ธฐ๋ณธ์œผ๋กœ ์‹œ์ž‘๋˜๋„๋ก ์„ค์ •ํ•ด ์ค๋‹ˆ๋‹ค.

 

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

 

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด http://localhost:4200๋กœ ์ด๋™ํ•˜๋ฉด ์ž๋™์œผ๋กœ login ํ™”๋ฉด์œผ๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ๋˜์–ด ๋กœ๊ทธ์ธ ํ™”๋ฉด์ด ๊ธฐ๋ณธ ํ™”๋ฉด์œผ๋กœ ๋ณด์—ฌ์ง‘๋‹ˆ๋‹ค.

 


ํ™”๋ฉด ์ด๋™ํ•˜๊ธฐ

 

login.component.html

 

register๋กœ ์ด๋™ํ•˜๊ธฐ ์œ„ํ•œ ๋ฒ„ํŠผ์„ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค.

 

<div class="container">
    <button (click)="login()" class="route-button"> Go to Register </button>
</div>

login.component.ts

 

Router๋ฅผ import ํ•ด์ฃผ๊ณ ,

 

import { Router } from '@angular/router';

 

์ƒ์„ฑ์ž์— router๋ฅผ ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค.

 

 constructor(private router:Router) { }

 

login ๋ฉ”์„œ๋“œ์— ์ด๋™ํ•  ํ™”๋ฉด(register)๋กœ navigate ํ•ด์ค๋‹ˆ๋‹ค.

 

  login() {
    this.router.navigate(['/register'])
  }

login.component.css (Optional)

 

๋ฒ„ํŠผ ์œ„์น˜๋ž‘, ๋””์ž์ธ ํ•˜๊ณ  ์‹ถ์€ ๋ถ„๋“ค์€ ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด ์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

.container {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
}

.route-button {
    position: absolute;
    display: flex;
    align-items: center;
    justify-content: center;
    left: 50%;
    top: 40%;
    transform: translateX(-50%);
    text-decoration: none;
    color: white;
    border: white solid 0.2rem;
    font-size: 1rem;
    font-weight: bold;
    text-transform: uppercase;
    width: 15rem;
    padding: 1.5rem;
    background-color: rgb(55, 55, 55);
  }

Test

 

์•„๋ž˜์™€ ๊ฐ™์ด Go to register ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด

์•„๋ž˜์™€ ๊ฐ™์ด register ํ™”๋ฉด์œผ๋กœ ์ด๋™๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 


์ด์ „ ํ™”๋ฉด์œผ๋กœ ๋˜๋Œ์•„ ๊ฐ€๊ธฐ

 

์ง€๊ธˆ ์ž‘์„ฑํ•˜๋Š” ํ”„๋กœ์ ํŠธ์˜ ๊ฒฝ์šฐ Login๊ณผ Register ๋ฐ–์— ์—†์–ด์„œ ์ด์ „ ํ™”๋ฉด์ด ์–ด๋”˜์ง€ ์ •ํ™•ํžˆ ์•Œ ์ˆ˜ ์žˆ์ง€๋งŒ,

 

ํ™”๋ฉด์ด ๋งŽ์•„์ง€๊ณ , ๊ธฐ๋Šฅ์ด ๋งŽ์•„์งˆ์ˆ˜๋ก ์ด์ „ ํ™”๋ฉด์ด ์–ด๋”˜์ง€ ์ ๋Š” ๊ฒƒ์€ ๊ฝค ๊นŒ๋‹ค๋กœ์šด ์ผ์ž…๋‹ˆ๋‹ค.

 

๊ณ ๋กœ ์ด๋™ํ•œ ๋ฐ”๋กœ ์ด์ „์˜ ํ™”๋ฉด์œผ๋กœ ๋˜๋Œ์•„ ๊ฐ€๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„์•ผ ํ•˜๋Š”๋ฐ์š”.

 

register.component.html

 

Login ํ™”๋ฉด๊ณผ ๊ฐ™์ด ๋ฒ„ํŠผ์„ ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค.

 

<div class="container">
    <button (click)="back()" class="route-button"> Back to Previous</button>
</div>

register.component.ts

 

Login ํ™”๋ฉด๊ณผ ๊ฐ™์ด router ์„ค์ •์„ ํ•ด์ค€ ๋’ค ์•„๋ž˜์™€ ๊ฐ™์ด '..' ์œผ๋กœ endPoint๋ฅผ ์„ค์ •ํ•ด ์ค๋‹ˆ๋‹ค.

 

  back() {
    this.router.navigate(['\..']);
  }

 

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด Back ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด Login ํ™”๋ฉด์œผ๋กœ ์ด๋™๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€