Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过路由angular2设置主页_Angular_Routing - Fatal编程技术网

如何通过路由angular2设置主页

如何通过路由angular2设置主页,angular,routing,Angular,Routing,在Angular2的路由部分,我的代码是这样的,但是为什么主页(默认页面)不是我设置的呢? app.module.ts: import {RouterModule} from '@angular/router'; @NgModule({ imports: [ RouterModule.forRoot([ { path: 'addProperty', component: AddPropertyComponent }, { path: 'list-propert

在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 {
}
从'@angular/core'导入{Component};
从“./home/home.component”导入{HomeComponent};
从“./Components/add property.component”导入{AddPropertyComponent};
@组成部分({
选择器:“我的应用程序”,
模板:`
`
})
导出类AppComponent{
}
我认为这很简单,也许我犯了错误。
非常感谢

试试这个。将/添加到重定向到:'home',这样它将被
重定向到:'/home'
;位置无关紧要,这将自动重定向到app.routing.module中的角色页面所有路由

    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'
 }
]),
]
})

试试这个。将/添加到重定向到:'home',这样它将被
重定向到:'/home'
;位置无关紧要,这将自动重定向到app.routing.module中的角色页面所有路由

    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'
 }
]),
]
})

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

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
  },
]),
]
})

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

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
  },
]),
]
})

您提供的代码片段似乎有效。你能发布更多的代码或者更好的插件吗?控制台(浏览器中的开发人员工具)是否显示任何错误?您提供的代码片段似乎可以正常工作。你能发布更多的代码或者更好的插件吗?控制台(浏览器中的开发人员工具)是否显示任何错误?谢谢,但我认为问题出在其他地方,因为它尚未修复:(谢谢,但我想问题出在别的地方,因为它还没有解决。)(