1. 程式人生 > >angular2路由傳值

angular2路由傳值

兄弟元件之間傳值,多用在呼叫方法裡面。

1.在傳值元件component.ts的constructor函式裡面先宣告router:

constructor( public router: Router, ) {}
在傳值元件component.ts的點選函式裡面寫:

this. router. navigate([ '/content'],{ queryParams:{ 'uid' : "000", 'uname' : "aaa"
}});

然後通過點選函式把值傳過去,第一個是跳轉的路由,第二個是傳遞的引數(多個)。

2.在接受引數的元件裡面的constructor函式裡面宣告:

constructor( private activeRoute: ActivatedRoute, ) { }

然後在ngOnInit函式裡面獲取傳過來的引數:

uid;     uname;

ngOnInit() {

    //兩種方法
console
. log( this. activeRoute. queryParams[ 'value']);//1     //2
this. activeRoute. queryParams. subscribe( params =>{ this. uid = params[ 'uid']; this. uname = params[ 'uname']; console. log
( this. uid) ; console. log( this. uname); })

uid和uname就是傳過來的引數,根據需要獲取即可!