当我点击"编辑"我的RouteParams不能正常工作按钮。
在第一个视图中,我点击 bob行上的编辑btn 这是第二个视图
现在我点击浏览器中的后退按钮,然后返回到第一个视图。然后我点击弹出行上的编辑btn,这是第三个视图(与第二个视图相同,这是错误的)
这是第一个视图的代码,当我点击编辑按钮时,它会触发func并在console.log图片中记录1和3条记录
array = array.sort(function (x, y) {
return days.indexOf(x.slice(0,3)) - days.indexOf(y.slice(0,3));
});
这是第二个视图的代码,在我单击编辑按钮并跳转到编辑视图后,它会触发构造函数并在console.log图片中记录2和4条记录
import {Component} from 'angular2/core';
import {CORE_DIRECTIVES} from 'angular2/common';
import { Router, RouteParams, ROUTER_DIRECTIVES } from 'angular2/router';
import {MailingListService} from'../../services/mailing-list/mailing-lists.service'
import {Subscriber} from '../../models/subscriber/subscriber';
import 'rxjs/Rx';
import {Observable} from 'rxjs/Rx';
@Component({
selector: 'subscriber-list-edit-view',
templateUrl: './app/components/subscriber-list-editor-view/subscriber-list-editor-view.html',
directives: [ROUTER_DIRECTIVES]
})
export class EditSubscriberListViewComponent {
subscribers: Subscriber[];
id: string;
constructor(private mailingListsService: MailingListService, params: RouteParams, private router: Router) {
this.init(this.id = params.get("id"));
}
init(id: string) {
this.mailingListsService.getSubscribers(this.id).subscribe(x => {
this.subscribers = x;
});
}
//this is fired when click the edit button
editSubscriber(subscriber: Subscriber) {
this.router.navigate(['Subscribers.Edit', { id: this.id, subscriber: subscriber }]);
console.log(subscriber);//this print the first and third record in console.log
}
deleteSubscriber(subscriber: Subscriber) {
this.mailingListsService.deleteSubscriber(this.id, subscriber)
.subscribe(() => { this.init(this.id) });
}
}
}
如你所见,console.log中的第四条记录不正确所以我认为这是params问题。但我不知道为什么以及如何解决问题。
这是routeconfig
import {Component} from 'angular2/core';
import { Router, RouteParams } from 'angular2/router';
import {CORE_DIRECTIVES} from 'angular2/common';
import {Subscriber}from '../../models/subscriber/subscriber';
import {MailingListService} from'../../services/mailing-list/mailing-lists.service'
@Component({
selector: 'create-subscriber-view',
templateUrl: './app/components/subscriber-creator-view/subscriber-creator-view.html'
})
export class CreateSubscriberComponent {
subscriber: Subscriber;
id: string;
subscriberId: string;
name: string;
phoneNumber: string;
email: string;
isActivated: boolean;
isBusy: boolean;
isEdit: boolean;
constructor(private mailingListService: MailingListService,private params: RouteParams, private router: Router) {
this.id = params.get("id");
this.subscriber = params.get("subscriber");
console.log(params.get("subscriber")); //this doesn't work well,print the second and forth record in the console.log
if (this.subscriber != null) { this.isEdit = true; this.name = this.subscriber.name; this.phoneNumber = this.subscriber.phoneNumber; this.email = this.subscriber.email; this.subscriberId = this.subscriber.id; this.isActivated = this.subscriber.isActivated;};
}
create() {
if (this.phoneNumber == null && this.email == null) { alert("either phone or email is required ! "); return; }
this.isBusy = true;
this.mailingListService.createSubscriber(this.id, {
name: this.name,
phoneNumber: this.phoneNumber,
email: this.email
})
.subscribe(() => { this.router.navigate(['MailingLists.View', { id: this.id }]) });
}
edit() {
if (this.phoneNumber == null && this.email == null) { alert("either phone or email is required ! "); return; }
this.isBusy = true;
this.mailingListService.editSubscriber(this.id, {
name: this.name,
phoneNumber: this.phoneNumber,
email: this.email
})
.subscribe(() => { this.router.navigate(['MailingLists.View', { id: this.id }]) });
}
创建和编辑使用相同的组件,但使用不同的URL。
答案 0 :(得分:0)
因为之前我将订阅者作为对象传递,而routeparams通过引用找到该对象。这也许是它没有刷新数据的原因。
我这里有一个有效的解决方案。只需执行JSON.stringify(订阅者),然后传递传递字符串的对象。
editSubscriber(subscriber: Subscriber) {
this.router.navigate(['Subscribers.Edit', { id: this.id, subscriberString: JSON.stringify(subscriber) }])//here
}
现在,在第二个组件中,您将获得字符串并执行JSON.parse()
constructor(private mailingListService: MailingListService, private params: RouteParams, private router: Router) {
this.id = params.get("id");
this.subscriberString = params.get("subscriberString");
if (this.subscriberString != null) {
this.isEdit = true;
this.subscriber = JSON.parse(this.subscriberString);//Here
this.name = this.subscriber.name;
this.phoneNumber = this.subscriber.phoneNumber;
this.email = this.subscriber.email;
this.subscriberId = this.subscriber.id;
this.isActivated = this.subscriber.isActivated;
};
}
永远记住你的routeparams只能是字符串数字boolean(原始类型?)
的类型