- Hands-On Data Structures and Algorithms with JavaScript
- Kashyap Mukkamala
- 422字
- 2021-06-30 19:12:09
Setting up the application and its routing
Now that we have the base code set up, let's list the steps for us to build an app that will enable us to create a custom Back button in a browser:
- Creating states for the application.
- Recording when the state of the application changes.
- Detecting a click on our custom Back button.
- Updating the list of the states that are being tracked.
Let's quickly add a few states to the application, which are also known as routes in Angular. All SPA frameworks have some form of routing module, which you can use to set up a few routes for your application.
Once we have the routes and the routing set up, we will end up with a directory structure, as follows:

Now let's set up the navigation in such a way that we can switch between the routes. To set up routing in an Angular application, you will need to create the component to which you want to route and the declaration of that particular route. So, for instance, your home.component.ts would look as follows:
import { Component } from '@angular/core';
@Component({
selector: 'home',
template: 'home page'
})
export class HomeComponent {
}
The home.routing.ts file would be as follows:
import { HomeComponent } from './home.component';
export const HomeRoutes = [
{ path: 'home', component: HomeComponent },
];
export const HomeComponents = [
HomeComponent
];
We can set up a similar configuration for as many routes as needed, and once it's set up, we will create an app-level file for application routing and inject all the routes and the navigatableComponents in that file so that we don't have to touch our main module over and over.
So, your file app.routing.ts would look like the following:
import { Routes } from '@angular/router';
import {AboutComponents, AboutRoutes} from "./pages/about/about.routing";
import {DashboardComponents, DashboardRoutes} from "./pages/dashboard/dashboard.routing";
import {HomeComponents, HomeRoutes} from "./pages/home/home.routing";
import {ProfileComponents, ProfileRoutes} from "./pages/profile/profile.routing";
export const routes: Routes = [
{
path: '',
redirectTo: '/home',
pathMatch: 'full'
},
...AboutRoutes,
...DashboardRoutes,
...HomeRoutes,
...ProfileRoutes
];
export const navigatableComponents = [
...AboutComponents,
...DashboardComponents,
...HomeComponents,
...ProfileComponents
];
You will note that we are doing something particularly interesting here:
{
path: '',
redirectTo: '/home',
pathMatch: 'full'
}
This is Angular's way of setting default route redirects, so that, when the app loads, it's taken directly to the /home path, and we no longer have to set up the redirects manually.
- DB2 V9權威指南
- FuelPHP Application Development Blueprints
- Microsoft Exchange Server PowerShell Cookbook(Third Edition)
- Power Up Your PowToon Studio Project
- 騰訊iOS測試實踐
- 構建移動網站與APP:HTML 5移動開發入門與實戰(跨平臺移動開發叢書)
- Animate CC二維動畫設計與制作(微課版)
- 精通API架構:設計、運維與演進
- 編譯系統透視:圖解編譯原理
- 青少年Python編程入門
- Oracle Exadata專家手冊
- 劍指大數據:企業級數據倉庫項目實戰(在線教育版)
- Java網絡編程實戰
- Scala Data Analysis Cookbook
- 移動互聯網軟件開發實驗指導