Angular 2:将数据传递给路由?

Bhu*_*kar 35 javascript typescript angular2-routing angular

我正在研究这个angular2项目,我用它ROUTER_DIRECTIVES来从一个组件导航到另一个组件.

有2个组件.即PagesComponent&DesignerComponent.

我想从PagesComponent导航到DesignerComponent.

到目前为止它的路由正确但我需要传递pageObject,因此设计人员可以加载该页面对象本身.

我尝试使用RouteParams但它获取页面对象undefined.

下面是我的代码:

pages.component.ts

import {Component, OnInit ,Input} from 'angular2/core';
import { GlobalObjectsService} from './../../shared/services/global/global.objects.service';
import { ROUTER_DIRECTIVES, RouteConfig } from 'angular2/router';
import { DesignerComponent } from './../../designer/designer.component';
import {RouteParams} from 'angular2/router';

@Component({
    selector: 'pages',    
    directives:[ROUTER_DIRECTIVES,],
    templateUrl: 'app/project-manager/pages/pages.component.html'
})
@RouteConfig([
  { path: '/',name: 'Designer',component: DesignerComponent }      
])

export class PagesComponent implements OnInit {
@Input() pages:any;
public selectedWorkspace:any;    
constructor(private globalObjectsService:GlobalObjectsService) {
    this.selectedWorkspace=this.globalObjectsService.selectedWorkspace;                    
}
ngOnInit() { }   
}
Run Code Online (Sandbox Code Playgroud)

在html中,我正在做以下事情:

<scrollable height="300" class="list-group" style="overflow-y: auto; width: auto; height: 200px;" *ngFor="#page of pages">
    {{page.name}}<a [routerLink]="['Designer',{page: page}]" title="Page Designer"><i class="fa fa-edit"></i></a>
</scrollable>
Run Code Online (Sandbox Code Playgroud)

在DesignerComponent构造函数中,我完成了以下操作:

constructor(params: RouteParams) {
    this.page = params.get('page');
    console.log(this.page);//undefined
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,它正确地路由到设计器,但当我试图访问page设计器中的对象然后它的显示undefined.有解决方案吗

Gün*_*uer 19

您不能使用路由器参数传递对象,只能传递字符串,因为它需要反映在URL中.无论如何,使用共享服务在路由组件之间传递数据可能是更好的方法.

旧路由器允许通过data但新的(RC.1)路由器尚未通过.

更新

data重新介绍了RC.4 如何使用路由时如何在Angular 2组件中传递数据?


Vee*_*rra 18

它的角度变化2.1.0

在something.module.ts中

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { BlogComponent } from './blog.component';
import { AddComponent } from './add/add.component';
import { EditComponent } from './edit/edit.component';
import { RouterModule } from '@angular/router';
import { MaterialModule } from '@angular/material';
import { FormsModule } from '@angular/forms';
const routes = [
  {
    path: '',
    component: BlogComponent
  },
  {
    path: 'add',
    component: AddComponent
  },
  {
    path: 'edit/:id',
    component: EditComponent,
    data: {
      type: 'edit'
    }
  }

];
@NgModule({
  imports: [
    CommonModule,
    RouterModule.forChild(routes),
    MaterialModule.forRoot(),
    FormsModule
  ],
  declarations: [BlogComponent, EditComponent, AddComponent]
})
export class BlogModule { }
Run Code Online (Sandbox Code Playgroud)

获取编辑组件中的数据或参数

import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute, Params, Data } from '@angular/router';
@Component({
  selector: 'app-edit',
  templateUrl: './edit.component.html',
  styleUrls: ['./edit.component.css']
})
export class EditComponent implements OnInit {
  constructor(
    private route: ActivatedRoute,
    private router: Router

  ) { }
  ngOnInit() {

    this.route.snapshot.params['id'];
    this.route.snapshot.data['type'];

  }
}
Run Code Online (Sandbox Code Playgroud)

  • 您可以将该数据传递到导航点的路径而不是路由配置吗?例如,在详细信息组件替换主组件(因此不是子组件)的主 - 详细信息情况下,您是否可以在导航到详细信息组件时将主数据对象传递给详细信息组件,以便详细信息组件可以显示某些组件.它在加载详细数据时的数据? (10认同)
  • @Sebastien你得到了你的答案...... plzz告诉你是怎么做到的 (3认同)