如何通过路由angular2设置主页

时间:2017-09-17 12:07:43

标签: angular routing

在路由部分的Angular2中

我的代码就是这个,但为什么主页(默认页面)不是我设置的? app.module.ts:

import {RouterModule}   from '@angular/router';
@NgModule({
imports: [
RouterModule.forRoot([
  {
    path: 'addProperty',
    component: AddPropertyComponent
  },
 {
   path: 'list-property',
   component: ListPropertyComponent
 },
  {
    path: 'search-place-auto',
    component: SearchPlaceAuto
  },
  {
    path: 'home',
    component: HomeComponent
  },
 {
   path: '',
   redirectTo: 'home',
   pathMatch: 'full'
 }
]),
]
})

和app.component.ts:

 import {Component} from '@angular/core';
 import {HomeComponent} from './home/home.component';
 import {AddPropertyComponent} from './componentes/add-property.component';


@Component({
selector: 'my-app',
template: `      
  <header-component></header-component>
  <router-outlet></router-outlet>
  <login></login>
  <footer></footer>
      `
})

export class AppComponent {
}

我觉得这很简单,也许我犯了错误。 非常感谢

2 个答案:

答案 0 :(得分:0)

试试这个。将 / 添加到redirectTo:&#39; home&#39;所以它将是Traceback (most recent call last): File "/Users/tschaefer/Documents/workspace/PythonCMTSLibs/basicSSHSessionModule.py", line 17, in <module> ssh.connect(host, user, password) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/paramiko/client.py", line 301, in connect to_try = list(self._families_and_addresses(hostname, port)) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/paramiko/client.py", line 199, in _families_and_addresses hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM) socket.gaierror: [Errno 8] nodename nor servname provided, or not known ;位置无关紧要plunker将自动重定向到角色页面app.routing.module中的所有路线

redirectTo: '/home'

答案 1 :(得分:0)

您需要首先将home作为默认值,在您的路径配置中替换以下代码段。

import {RouterModule}   from '@angular/router';
@NgModule({
imports: [
RouterModule.forRoot([
  {
    path: 'addProperty',
    component: AddPropertyComponent
  },
 {
   path: 'list-property',
   component: ListPropertyComponent
 },
  {
    path: 'search-place-auto',
    component: SearchPlaceAuto
  },
   { 
    path: '',
   redirectTo: '/home',
   pathMatch: 'full'
   },

  {
   path: 'home',
   component: HomeComponent
  },
]),
]
})