【问题标题】:Angular 2 passing object via route params possible?Angular 2可以通过路由参数传递对象吗?
【发布时间】:2016-05-30 11:21:17
【问题描述】:

我可以使用一些建议来解决我面临的这个问题。为了尽可能为您解释这一点,我创建了一个主要组件:

@Component({
selector: 'main-component',
providers: [...FORM_PROVIDERS, MainService, MainQuoteComponent],
directives: [...ROUTER_DIRECTIVES, CORE_DIRECTIVES, RouterOutlet, MainQuoteComponent ],
styles: [`
    agent {
        display: block;
    }
`],
pipes: [],
template: `
   **Html hidden**
  `,
  bindings: [MainService],
})

@RouteConfig([
    { path: '/', name: 'Main', component: MainMenuComponent, useAsDefault: true },
    { path: '/passenger', name: 'Passenger', component: PassengerComponent },
])

@Injectable()
export class MainComponent {

bookingNumber: string;
reservation: Reservation;
profile: any;

constructor(params: RouteParams, public mainService: MainService) {

    this.bookingNumber = params.get("id");

     this.mainService.getReservation(this.bookingNumber).subscribe((reservation) => {

        this.reservation = reservation;
    });

    this.profile = this.mainService.getUserDetails();

} 

}

此组件从 api 检索预订并将其保存在您看到的预订对象中(它有一个类型为 Reservation 的类,看起来像这样)

export class Reservation {

constructor(
    public Id: number,
    public BookingNumber: string,
    public OutboundDate: Date,
    public ReturnDate: Date,
    public Route: string,
    public ReturnRoute: string,
    public Passengers: string,
    public Pet: string,
    public VehicleType: string,
    public PassengersList: Array<Passengers>

) { }
}

当我点击按钮Passenger时,它会重定向到乘客页面Main/passenger,但这里我需要发送预订对象(整个)或只是PassengerList(数组)。

知道是否可以使用路由参数或路由器插座来做到这一点?有什么建议吗?

【问题讨论】:

    标签: angular angular2-routing


    【解决方案1】:

    只需使用共享服务,并将其添加到父组件的providers: [...]

    简单的服务类

    @Injectable()
    export class ReservationService {
      reservation:Reservation;
    }
    

    在 parent 中将其添加到提供程序并注入到构造函数中

    @Component({...
       providers: [ReservationService]
    export class Parent {
      constructor(private reservationService:ReservationService) {}
    
      someFunction() {
        reservationService.reservation = someValue;
      }
    }
    

    在子组件中只注入它(不要添加到提供者)

    @Component({...
      providers: []
    export class Passenger {
      constructor(private reservationService:ReservationService) {
        console.log(reservationService.reservation);
      }
    
      someFunction() { 
        reservationService.reservation = someValue;
      }
    }
    

    更新

    bootstrap() 是所有事物的共同祖先,也是一个有效的选项。这取决于您的确切要求。如果您在组件中提供它,则该组件将成为共享单个实例的树的根。通过这种方式,您可以指定服务的范围。如果范围应该是“您的整个应用程序”,则在 bootstrap() 或根组件中提供它。 Angular2 风格指南鼓励使用根组件的providers 而不是bootstrap()。结果将是相同的。如果您只想在组件A 和添加到其&lt;router-outlet&gt; 的其他组件之间进行通信,那么将范围限制在此组件A 是有意义的。

    【讨论】:

    • 感谢您的快速回复。。晚上会看。
    • 2 小时后我终于确定了:) 预订未定义,但通过在使用 routerlink 到乘客组件之前调用将预订对象映射到服务中的对象的方法来解决这个问题。谢谢。
    • 如果我没有通过子组件中的提供者,我没有收到服务错误的提供者
    • 将服务添加到子中的提供者打破了整个服务概念,因为父子和子获取不同的服务实例。没有看到一个完整的例子很难说。您仍然需要导入定义 chils 组件的服务。
    • 问题可以在这里找到:link
    猜你喜欢
    • 2020-03-12
    • 2017-08-17
    • 2017-04-26
    • 2016-09-21
    • 1970-01-01
    • 2019-05-17
    • 1970-01-01
    • 2011-03-26
    • 2016-03-16
    相关资源
    最近更新 更多