[英]why my RouteParams in angular2 didn't refresh in angular2?
My RouteParams didn't work well when I click "edit" button. 当我单击“编辑”按钮时,我的RouteParams不能正常工作。
in the first view I click the edit btn on the bob row and this is the second view 在第一个视图中,我单击鲍勃行上的btn编辑,这是第二个视图
now i click the back button in the browser and it goes back to fist view. 现在,我在浏览器中单击“后退”按钮,它返回到拳头视图。 then i click the edit btn on the pop row and this is the third view(same as second view which is wrong)
然后我单击弹出行上的编辑btn,这是第三个视图(与第二个视图相同,这是错误的)
and I put some console to debug. 然后我放了一些控制台进行调试。 this is the console log
这是控制台日志
here is my code for the first view when i click the edit button, it fire the func and record the 1 and 3 records in the console.log picture 这是我单击编辑按钮时的第一个视图的代码,它会启动功能并在console.log图片中记录1和3记录。
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) });
}
}
here is my code for the second view after i click the edit button and jump to the edit view, it fire the constructor and record the 2 and 4 records in the console.log picture 这是我单击编辑按钮并跳转到编辑视图后的第二个视图的代码,它触发了构造函数并在console.log图片中记录了2条记录和4条记录
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 }]) });
}
} }
as you can see, the fourth record in console.log is not correct so i assume it is the params problem. 如您所见,console.log中的第四条记录不正确,因此我认为这是参数问题。 but i don't know why and how to solve the problem.
但是我不知道为什么以及如何解决这个问题。
and this is the routeconfig 这是routeconfig
{ path: '/MailingLists/:id/Subscribers/Create', component: CreateSubscriberComponent, as: 'Subscribers.Create' },
{ path: '/MailingLists/:id/Subscribers/Edit', component: CreateSubscriberComponent, as: 'Subscribers.Edit' },
create and edit use same component but different url. 创建和编辑使用相同的组件,但使用不同的URL。
Because previously I pass my subscriber as an object and routeparams find the object by reference. 因为以前我将订户作为对象传递,而routeparams通过引用找到了对象。 That maybe the reason why it didn't refresh the data.
那也许就是为什么它没有刷新数据的原因。
I have a working solution here. 我在这里有一个可行的解决方案。 Simply do a JSON.stringify(subscriber) instead pass an object you pass a string.
只需执行JSON.stringify(subscriber)即可,而不是传递对象,而传递字符串。
editSubscriber(subscriber: Subscriber) {
this.router.navigate(['Subscribers.Edit', { id: this.id, subscriberString: JSON.stringify(subscriber) }])//here
}
now, in the second component you get the string and do a JSON.parse() 现在,在第二个组件中,您将获得字符串并执行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;
};
}
Always remember your routeparams can be only type of string number boolean (primitive types?) 永远记住,您的routeparams只能是字符串类型boolean的类型(原始类型?)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.