我正在将我的应用程序从Angular2 Rc3迁移到Angular(2.0.0)最终版本。我在Rc3中有这样的代码
constructor(public router:Router, public routeData: RouteData){
this.routeData.data = { idNum: this.idNumber, message: myMessage };
this.router.navigate(['ActivityList']);
}
在当前版本中,不推荐使用RouteData并将其替换为ActivatedRoute。任何人都可以帮助我如何通过ActivatedRoute传递这些对象。
答案 0 :(得分:2)
您可以尝试JSON.stringify(your_object):
import {Component} from "@angular/core";
import {Router, NavigationExtras} from "@angular/router";
@Component({..})
export class AppComponent {
constructor(router:Router) {
this.router = router;
}
let navextras: NavigationExtras={
queryParams:{"message":JSON.stringify({"id": "1", "text":"yourmessages"})}
};
sendStringParam() {
this.router.navigate([ 'Message'], navextras);
return false;
}
}
从您的组件接收数据:
import { ActivatedRoute } from '@angular/router';
import {Component} from "@angular/core";
@Component({.....})
export class ReceiverComponent {
public message:any;
constructor(route: ActivatedRoute) {
this.route.queryParams.subscribe(params => {
this.message = params["message"];
console.log(this.message.id);
console.log(this.message.text);
});
}
}