Angular creating a to-do app

Source: Internet
Author: User

Establish a route

After the previous section, remove the login component from the src/app/app.component.html

The first step:

Specify a datum path in src/index.html

The browser will download CSS, images, JS files according to this path, so the statement should be placed at the top of the header

<base href= "/" >

Step Two:

Introduction of Routermodule in Src/app/app.module.ts

Import {Routermodule} from "@angular/router";

Step Three:

Defining and Configuring routing arrays

Forroot is a static factory method

Routing sockets

Routed socket Load Logincomponent

Add a routed socket (outlet) to the app.component.html

Ok

After saving, the login component is missing.

Browser input Required: Http://localhost:4200/login

/login This is the routing

Another way is to use Redirectto route redirection to direct the home page to the login route

In the route definition array, add a route definition, as follows

Then the browser input: http://localhost:4200/, enter, browser address into: Http://localhost:4200/login

PS: Note that ANGULAR2 uses the "first match priority" principle, if a path can match multiple routes, the first matching rule will prevail, so the order of routing configuration is very important.

Detach route

If all the routes are maintained in app.module.ts, bad management, preferably alone

New File Src/app/app.routes.ts

Import {Routes,routermodule} from "@angular/router", import {logincomponent} from "./login/login.component"; import {Mo Dulewithproviders} from "@angular/core"; export const routes:routes=[    {        path: ' Login ',        component: Logincomponent    },    {        path: ',        redirectto: ' Login ',        pathmatch: ' Full '    }];export const Routing:modulewithproviders=routermodule.forroot (routes);

  

and introduce routing in app.module.ts.

Save, open Browser, view, effect and the same as before, just in the background we will separate the route separately out, the separation succeeds!

OK, here's the route section.

Angular creating a to-do app

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.