首页 > 其他分享 >无涯教程-Angular7 - 路由(Routing)

无涯教程-Angular7 - 路由(Routing)

时间:2023-12-08 14:06:50浏览次数:31  
标签:contactus app component 无涯 Angular7 Routing home import angular

路由(Routing)基本上意味着在页面之间导航,现在让我们创建一个组件,看看如何在其上使用路由。如下所示-

app.module.ts

import { BrowserModule } from '@angular/platform-browser'; 
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module'; 
import { AppComponent } from './app.component'; 
import { NewCmpComponent } from './new-cmp/new-cmp.component'; 
import { ChangeTextDirective } from './change-text.directive'; 
import { SqrtPipe } from './app.sqrt';

@NgModule({ 
   declarations: [ 
      SqrtPipe, 
      AppComponent, 
      NewCmpComponent, 
      ChangeTextDirective
   ], 
   imports: [ 
      BrowserModule, 
      AppRoutingModule 
   ], 
   providers: [], 
   bootstrap: [AppComponent] 
})
export class AppModule { }

如上所示,添加了 AppRoutingModule 并将其包含在imports数组中。

app-routing.module 的文件详细信息在下面给出-

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

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

在上面的文件中,我们从@angular/router导入了Routes和RouterModule。

定义了一个常量 routes ,其类型为Routes,它是一个数组,其中包含我们项目中所需的所有路由。

如@NgModule所示,将const路由提供给RouterModule, 为了向用户显示路由详细信息,我们需要在要显示视图的位置添加<router-outlet>指令。

如下所示,在app.component.html中添加了相同的内容-

<h1>Angular 7 Routing Demo</h1> 
<router-outlet></router-outlet>

现在,让我们创建两个名为 Home 和 Contact Us 的组件,并使用路由在它们之间导航。

Component Home

以下是Component Home的语法-

ng g component home
C:\projectA7\angular7-app>ng g component home CREATE 
src/app/home/home.component.html (23 bytes) CREATE 
src/app/home/home.component.spec.ts (614 bytes) CREATE 
src/app/home/home.component.ts (261 bytes) CREATE 
src/app/home/home.component.css (0 bytes) UPDATE 
src/app/app.module.ts (692 bytes)

Component Contact Us

以下是Component Contactus的语法-

ng g component contactus
C:\projectA7\angular7-app>ng g component contactus 
CREATE src/app/contactus/contactus.component.html (28 bytes) 
CREATE src/app/contactus/contactus.component.spec.ts (649 bytes) 
CREATE src/app/contactus/contactus.component.ts (281 bytes) 
CREATE src/app/contactus/contactus.component.css (0 bytes) 
UPDATE src/app/app.module.ts (786 bytes)

我们已经完成了在components home 与 contact us 组件。以下是app.module.ts中组件的详细信息-

import { BrowserModule } from '@angular/platform-browser'; 
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module'; 
import { AppComponent } from './app.component'; 
import { NewCmpComponent } from './new-cmp/new-cmp.component'; 
import { ChangeTextDirective } from './change-text.directive'; 
import { SqrtPipe } from './app.sqrt'; 
import { HomeComponent } from './home/home.component'; 
import { ContactusComponent } from './contactus/contactus.component';

@NgModule({ 
   declarations: [ 
      SqrtPipe, 
      AppComponent, 
      NewCmpComponent, 
      ChangeTextDirective, 
      HomeComponent, 
      ContactusComponent 
   ],
   imports: [ 
      BrowserModule, 
      AppRoutingModule 
   ],
   providers: [], 
   bootstrap: [AppComponent] 
}) 
export class AppModule { }

现在,让我们在 app-routing.module .ts中添加路由详细信息,如下所示-

import { NgModule } from '@angular/core'; 
import { Routes, RouterModule } from '@angular/router'; 
import { HomeComponent } from './home/home.component'; 
import { ContactusComponent } from './contactus/contactus.component';

const routes: Routes=[ 
   {path:"home", component:HomeComponent}, 
   {path:"contactus", component:ContactusComponent} 
];
@NgModule({ 
   imports: [RouterModule.forRoot(routes)], 
   exports: [RouterModule] 
})
export class AppRoutingModule { }

routes数组具有路径和组件的组件详细信息。app-routing.module.ts -

import { NgModule } from '@angular/core'; 
import { Routes, RouterModule } from '@angular/router'; 
import { HomeComponent } from './home/home.component'; 
import { ContactusComponent } from './contactus/contactus.component'; 

const routes: Routes=[
   {path:"home", component:HomeComponent},
   {path:"contactus", component:ContactusComponent} 
];
@NgModule({
   imports: [RouterModule.forRoot(routes)], 
   exports: [RouterModule] 
})
export class AppRoutingModule { } export const 
RoutingComponent=[HomeComponent,ContactusComponent];

将RoutingComponent如下导入app.module.ts中-

import { BrowserModule } from '@angular/platform-browser'; 
import { NgModule } from '@angular/core';
import { AppRoutingModule , RoutingComponent} from './app-routing.module'; 
import { AppComponent } from './app.component'; 
import { NewCmpComponent } from './new-cmp/new-cmp.component'; 
import { ChangeTextDirective } from './change-text.directive'; 
import { SqrtPipe } from './app.sqrt';

@NgModule({ 
   declarations: [ 
      SqrtPipe,
      AppComponent, 
      NewCmpComponent, 
      ChangeTextDirective, 
      RoutingComponent 
   ], 
   imports: [ 
      BrowserModule, 
      AppRoutingModule 
   ], 
   providers: [],
   bootstrap: [AppComponent] 
})
export class AppModule { }

我们需要向用户显示内容,因此让我们在app.component.html中添加两个按钮,即“Home”和“Contact Us”,然后单击相应的按钮,它将在<router-outlet>指令中显示组件视图。 在app.component.html中创建按钮,并提供创建路径的路径。

app.component.html

<h1>Angular 7 Routing Demo</h1> 
<nav> 
   <a routerLink="/home">Home</a> 
   <a routerLink="/contactus">Contact Us </a> 
</nav> 
<router-outlet></router-outlet>

在.html中,我们添加了 anchor 链接,"Home"和"Contact us",并使用routerLink给出了我们在app-routing.module.ts中创建的路由的路径。

现在让我们在浏览器中进行相同的测试-

Routing Demo

我们在app.component.css中添加了以下CSS-

a:link, a:visited { 
   background-color: #848686; 
   color: white; 
   padding: 10px 25px; 
   text-align: center; 
   text-decoration: none; 
   display: inline-block; 
} 
a:hover, a:active {
   background-color: #BD9696;
}

这是浏览器中链接的显示-

Display Links

单击Home链接,以查看主页的组件详细信息,如下所示-

Home Link

单击Contact Us,以查看其组件详细信息,如下所示-

Contact Us

当您单击链接时,您还将看到地址栏中的页面URL发生变化。如上图所示,它将路径详细信息附加在页面的末尾。

参考链接

https://www.learnfk.com/angular7/angular7-routing.html

标签:contactus,app,component,无涯,Angular7,Routing,home,import,angular
From: https://blog.51cto.com/u_14033984/8736536

相关文章

  • 无涯教程-Angular7 - 事件绑定
    在本章中,无涯教程将讨论事件绑定在Angular7中的工作方式,当用户以键盘移动,鼠标单击或鼠标悬停的形式与应用程序交互时,它将生成一个事件。需要处理这些事件以执行某种操作,让无涯教程考虑一个示例以更好地理解这一点。app.component.html<!--Thecontentbelowisonlyaplace......
  • 无涯教程-Angular7 - 简介
    Angular7是一个开放源代码JavaScript框架,用于在JavaScript, html 和Typescript(JavaScript的超集)中构建Web应用程序和应用程序。Angular为animation,httpservice和materials提供了内置功能,这些功能又具有auto-complete,naigation,toolbar,menus等功能。代码以Typescript编写......
  • 无涯教程-Erlang - is_alive函数
    如果本地节点处于活动状态并且可以是分布式系统的一部分,则返回true。否则,它返回false。is_alive-语法is_alive()is_alive-返回值如果本地节点处于活动状态并且可以是分布式系统的一部分,则返回true。否则,它返回false。-module(helloLearnfk).-export([start/0]).sta......
  • 无涯教程-Erlang - spawn函数
    这用于创建新进程并对其进行初始化。spawn-语法spawn(Function)Function - 需要产生的功能。spawn-返回值此方法返回一个进程ID。-module(helloLearnfk).-export([start/0]).start()->spawn(fun()->server("Hello")end).server(Message)->io:f......
  • 无涯教程-Erlang - unregister函数
    这用于注销系统中的进程。unregister-语法unregister(atom)atom-这是要赋予该过程的注册名称。unregister-示例-module(helloLearnfk).-export([start/0,call/2]).call(Arg1,Arg2)->io:fwrite("~p~n",[Arg1]).start()->Pid=spawn(?MODULE,cal......
  • 无涯教程-Erlang - register函数
    这用于在系统中注册进程。register-语法register(atom,pid)atom-这是要赋予该过程的注册名称。pid  -这是需要绑定到原子的进程ID。register-示例-module(helloLearnfk).-export([start/0,call/2]).call(Arg1,Arg2)->io:fwrite("~p~n",[Arg1]).......
  • 无涯教程-Erlang - is_pid函数
    此方法用于确定进程ID是否存在。is_pid-语法Is_pid(processid)processid  - 这是需要检查的进程ID,是否存在。is_pid-返回值如果进程ID存在,则返回true,否则将返回false。-module(helloLearnfk).-export([start/0,call/2]).call(Arg1,Arg2)->io:format("......
  • 无涯教程-Erlang - binary_to_list函数
    此方法用于将二进制值转换为列表。binary_to_list-语法binary_to_list(binaryvalue)binaryvalue- 这是需要转换为列表的二进制值。binary_to_list-返回值返回列表。-module(helloLearnfk).-export([start/0]).start()->io:fwrite("~p~n",[binary_to_lis......
  • 无涯教程-Erlang - is_binary函数
    此方法用于检查位串是否确实是二进制值。is_binary-语法is_binary(bitstring)bitstring-这是需要检查其是否为二进制的位串。is_binary-返回值如果位串是二进制值,则返回true;否则返回false。-module(helloLearnfk).-export([start/0]).start()->io:fwrite("......
  • 无涯教程-Erlang - term_to_binary函数
    此方法用于将术语转换为二进制。term_to_binary-语法term_to_binary(term)term-这是需要转换为二进制值的术语值。term_to_binary-返回值根据指定的术语返回一个二进制值。-module(helloLearnfk).-export([start/0]).start()->io:fwrite("~p~n",[term_to......