【问题标题】:Angular2: Can't get data from service after refreshing page on child componentAngular2:在子组件上刷新页面后无法从服务中获取数据
【发布时间】:2017-05-28 17:27:12
【问题描述】:

这是我的路由器配置:

[
  {
    path:'home',
    component: HomeComponent
  },
  {
    path:'hikes',
    children: [
      {
        path: '',
        component: HikeListComponent
      },
      {
        path: 'hikes/:name',
        component: HikeDetailComponent,
        children: [
         {
           path: 'races',
           children: [
             {
               path:'',
               component:  RaceListComponent,
               resolve: { hike: 'hike'}
             },
             {
               path:':id',
               component: RaceDetailComponent
              }
           }
         ]
       }
    ]
  }
]

问题:要解析我的 RaceListComponent ('hikes/:name/races') 中的比赛列表,我需要之前在其父组件中解析的远足名称:HikeDetailComponent ('hikes/:name')。 由于“router-outlet”指令,我决定使用 Hikeservice 来传递/检索组件 init 上的数据,并且它可以工作。 但是,如果我在子组件路由('hikes/:name/races')上刷新浏览器时,hikes'name 是“未定义”,所以我无法再解析我的racesList。

解决方案?:我实现了一个简单的解析器,它应该传递从我的 HikeService 检索到的数据:

import { HikeService } from '../Services/contexte.service';

@Injectable()
export class HikeResolver implements Resolve<any> {

  constructor( private hikeService:HikeService){}

  resolve() {
   return this.hikeService.getHike();
  }

}

这是我的简单 HikeService:

@Injectable()
export class HikeService {
 constructor() { }
   public hike;
   public getHike(): void {
    return this.hike;
   }
 }

HikeService.hike 由我的父组件在 init 上提供支持:

this.hikeService.hike = this.hike;

现在在我的子组件 (RaceComponent) 中,我可以从解析器获取数据:

let hikeId = this.route.snapshot.data['hike'].properties.id;

但是,如果我在我的子组件页面上刷新浏览器,我会得到一个异常:

error_handler.js:50 EXCEPTION: Uncaught (in promise): Error: Error in :0:0 caused by: Cannot read property 'properties' of undefined
TypeError: Cannot read property 'properties' of undefined at RaceComponent.ngOnInit (http://127.0.0.1:4200/main.bundle.js:3872:62) at Wrapper_RaceComponent.ngDoCheck (/AppModule/RaceComponent/wrapper.ngfactory.js:24:53) at CompiledTemplate.proxyViewClass.View_RaceComponent_Host0.detectChangesInternal (/AppModule/RaceComponent/host.ngfactory.js:28:37) at CompiledTemplate.proxyViewClass.AppView.detectChanges (http://127.0.0.1:4200/vendor.bundle.js:90216:14) at CompiledTemplate.proxyViewClass.DebugAppView.detectChanges (http://127.0.0.1:4200/vendor.bundle.js:90411:44) at ViewRef_.detectChanges (http://127.0.0.1:4200/vendor.bundle.js:64392:20) at RouterOutlet.activate (http://127.0.0.1:4200/vendor.bundle.js:74782:42) at ActivateRoutes.placeComponentIntoOutlet (http://127.0.0.1:4200/vendor.bundle.js:25870:16) at ActivateRoutes.activateRoutes (http://127.0.0.1:4200/vendor.bundle.js:25837:26) at http://127.0.0.1:4200/vendor.bundle.js:25773:58 at Array.forEach (native) at ActivateRoutes.activateChildRoutes (http://127.0.0.1:4200/vendor.bundle.js:25773:29) at ActivateRoutes.activateRoutes (http://127.0.0.1:4200/vendor.bundle.js:25838:26) at http://127.0.0.1:4200/vendor.bundle.js:25773:58 at Array.forEach (native)

【问题讨论】:

    标签: angular angular2-routing angular2-services


    【解决方案1】:

    对于您希望子组件等待其数据准备好的这种情况,您可以使用 Angular2 Route Resolve

    以下链接可能有用:

    【讨论】:

    • 感谢您的链接。我一直在尝试实现一个解析器,但我遇到了一个错误......更新了我的第一篇文章
    • 好的。完成了错误。我使用 angular-cli webpack。
    • @NicoDv 能否请您发送console.log 以检查在您进行页面刷新时是否调用了该服务。
    • 我确实复制了我的解析器...再次抱歉,感谢您的耐心等待。
    • 不必感到抱歉!如果可以了解所有内容并进行无错误编码,则不需要调试器和类似 SO 的站点。 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-20
    相关资源
    最近更新 更多