Angular#4 专案:路由 建置

Angular

[目标]

启动程序先导入Login元件
https://ithelp.ithome.com.tw/upload/images/20210501/201371342VoS7P6AKL.png


1. 新增元件、模组

  • Syntax:ng help可以查询
ng generate component [component_name]
ng generate module [module_name]

新增login元件、home元件 范例:
ng g c login
ng g c home
新增app-routing模组 范例:
ng g m app-routing

https://ithelp.ithome.com.tw/upload/images/20210501/20137134oGn6B57JXx.png


2. VSCode 撰写

  • app-routing
    app-routing.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
// 引用的模组、元件
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from '../home/home.component';
import { LoginComponent } from '../login/login.component';
// 建立路由规则
const appRoutes: Routes = [
  { path: '', redirectTo:'login',pathMatch:'full' },
  { path: 'login', component: LoginComponent },
  { path: 'home', component: HomeComponent },
  { path: '**', redirectTo:''}
]
// 模组设定
@NgModule({
  declarations: [],
  imports: [
    CommonModule,
    RouterModule.forRoot(
      appRoutes, { enableTracing: false }
    )
  ],
  exports: [
    RouterModule
  ]
})
export class AppRoutingModule { }
  • app
    app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
// 引用的模组、元件
import { LoginComponent } from './login/login.component';
import { HomeComponent } from './home/home.component';
import { AppRoutingModule } from './app-routing/app-routing.module';
// 模组设定
@NgModule({
  declarations: [
    AppComponent,
    LoginComponent,
    HomeComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.html

<router-outlet></router-outlet>

3. 启动专案

ng serve --open --port [XXXX]

范例ng serve --open --port 9985


4. 完成

https://ithelp.ithome.com.tw/upload/images/20210501/201371344j4xgO7TDq.png

上一篇
下一篇


<<:  Angular#3 资料结构 (可略过)

>>:  Angular#5 专案:路由 登入系统>>首页

Day 9 - 实现社交工程

出於书本 Chapter 5. Social Engineering 钓出资讯 (Phishing ...

从零开始的8-bit迷宫探险【Level 7】Swift 基础语法 (五)

今日目标 认识 nil 认识 Optional 认识 if let 及 guard let nil ...

Day3:进入新手村前先让我复习一下QQ-CSS-box-sizing

box-sizing这个语法可以让设计师或是写程序的人不用去加加减减的计算区块 (比如说线条宽度或是...

WSL2/ WSLg下使用snap store

更多会员限定文章可以到patreon观看 WSLg + Snap 如果清除/snap後还是不能mou...

前言与接下来的学习历程

前言 一开始是为了一些蠢事而做的,至於是什麽蠢事,这边就暂且不提,後来自己看见Meet6机器人的强大...