本文介绍了在 Angular 中,什么是“路径匹配:完整",它有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里它使用完整的路径匹配,当我删除这个路径匹配时,它甚至不加载应用程序或运行项目

In here it is use pathmatch as full and when i delete this pathmatch it doesn't even load the app or run the project

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';
import { RouterModule } from '@angular/router';

import { AppComponent }  from './app.component';
import { WelcomeComponent } from './home/welcome.component';

/* Feature Modules */
import { ProductModule } from './products/product.module';

@NgModule({
  imports: [
    BrowserModule,
    HttpModule,
    RouterModule.forRoot([
      { path: 'welcome', component: WelcomeComponent },
      { path: '', redirectTo: 'welcome', pathMatch: 'full' },
      { path: '**', redirectTo: 'welcome', pathMatch: 'full' }
    ]),
    ProductModule
  ],
  declarations: [
    AppComponent,
    WelcomeComponent
  ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

推荐答案

RouterModule.forRoot([
      { path: 'welcome', component: WelcomeComponent },
      { path: '', redirectTo: 'welcome', pathMatch: 'full' },
      { path: '**', component: 'pageNotFoundComponent' }
    ])

案例 1 pathMatch:'full':在这种情况下,当应用程序在 localhost:4200(或某个服务器)上启动时,默认页面将是欢迎屏幕,因为 url 将是 https://localhost:4200/代码>

Case 1 pathMatch:'full':In this case, when app is launched on localhost:4200 (or some server) the default page will be welcome screen, since the url will be https://localhost:4200/

如果 https://localhost:4200/gibberish 这将重定向到 pageNotFound 屏幕,因为 path:'**' 通配符

If https://localhost:4200/gibberish this will redirect to pageNotFound screen because of path:'**' wildcard

案例 2pathMatch:'prefix':

如果路由有 { path: '', redirectTo: 'welcome', pathMatch: 'prefix' },现在这将永远不会到达通配符路由,因为每个 url 都会匹配 path:'' 已定义.

If the routes have { path: '', redirectTo: 'welcome', pathMatch: 'prefix' }, now this will never reach the wildcard route since every url would match path:'' defined.

这篇关于在 Angular 中,什么是“路径匹配:完整",它有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 15:14