【问题标题】:How to set homepage route url using angular routing?如何使用角度路由设置主页路由 url?
【发布时间】:2020-04-09 03:38:34
【问题描述】:

我的应用程序中只有一个路由,所以当应用程序启动时,由于某种原因,我想将路由显示为http://localhost:4200/specialtyQ,现在应用程序没有加载我有下面的代码来实现这一点,我怎样才能显示正确的 url?

app-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AppComponent } from './app.component';


const routes: Routes = [{
  path: '/specialtyQs',
  component: AppComponent
}];

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

【问题讨论】:

    标签: html angular typescript angular-ui-router


    【解决方案1】:

    你需要添加一个默认路由来重定向

    const routes: Routes = [{
       path: 'specialtyQs',
       component: AppComponent
     },
     {
       path: '',
       redirectTo: 'specialtyQs',
       pathMatch: 'full'
     }];
    

    这将重定向到特定的网址。

    【讨论】:

    • 工作,谢谢先生!
    • 实际上刚刚注意到页面在浏览器中显示了两次,包括导航栏
    • 你的意思是双重内容?可以加个截图吗?
    • 提出新问题并在此处添加图片stackoverflow.com/questions/61126591/…
    【解决方案2】:

    只需在您的代码中添加如下所示的路由路径,而不是您添加它的方式。

    import { NgModule } from '@angular/core';
    import { Routes, RouterModule } from '@angular/router';
    import { AppComponent } from './app.component';
    
    
    const routes: Routes = [{
      path: '',
      redirectTo: '/specialtyQs',
      pathMatch: 'full'
    }];
    
    @NgModule({
      imports: [RouterModule.forRoot(routes)],
      exports: [RouterModule]
    })
    export class AppRoutingModule { }
    

    【讨论】:

    • 这并没有改变任何东西 url 仍然是 http://localhost:4200/ 它应该更改为 http://localhost:4200/specialtyQs 并且我们没有指定组件,因为我想将 appComponent 加载为默认值
    • 让我为你创建一个 stackblitz 实例。
    猜你喜欢
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    • 2015-06-13
    • 2018-01-14
    • 2018-01-25
    • 2020-06-09
    • 1970-01-01
    相关资源
    最近更新 更多