角度:路由并未正确显示我的组件

时间:2018-03-24 03:30:51

标签: angular

我的 app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';


import { AppComponent } from './app.component';
import { LoginComponent } from './login/login/login.component';
import { AppRoutingModule } from './app-routing.module';


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

我的 app-routing.module

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule, Routes } from '@angular/router';
import { LoginComponent } from './login/login/login.component';

export const appRoutes: Routes = [
  { path: '', component: LoginComponent},
  {
      path: 'ok',
      component: LoginComponent,
  },
  // otherwise redirect to home
  { 
    path: '**', 
    component: LoginComponent 
  }
];

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

我想将我的登录组件显示为主页,我选择的任何路径也将重定向到我的登录组件。我尝试在我的app.module.ts中将我的logincomponent放在bootstrap中,但它给了我一个错误。

我的登录组件是一个简单的<p>login works!</p>

2 个答案:

答案 0 :(得分:1)

您必须按如下方式配置路由

 imports: [
    RouterModule.forRoot([
      { path: '', component: LoginViewComponent },
      { path: 'ok', component: LoginViewComponent },
      { path: '**', redirectTo: 'login' }
    ])
  ]

<强> STACKBLITZ DEMO

答案 1 :(得分:0)

修改redirect添加pathMatch路由和export const appRoutes: Routes = [ { path: '', redirectTo: '/ok', pathMatch: 'full' }, { path: 'ok', component: LoginComponent, }, // otherwise redirect to home { path: '**', component: LoginComponent } ]; 属性,告诉路由器如何将网址与路径路径相匹配

{"target_type"=>"users", "devise_type"=>"users", "user_id"=>"dannytom222"}