我和其他人有類似的錯誤。但我真的可以解決它。錯誤是ERROR Error: ViewDestroyedError: Attempt to use a destroyed view: detectChanges.
我已經查看其他人的類似問題,但是我看不到我的應用程序的良好答案。我想檢測更改是否有5秒鐘的新數據。我希望它出現在我的瀏覽器中。所以我用改變了Detector ref,但是它給出了錯誤。ChangeDetector Ref in Angular 4中的錯誤
export class UsersListComponent implements OnInit {
closeResult: string;
users: any;
subscription: Subscription;
modalRef: NgbModalRef;
intervalHolder: any;
constructor(private modalService: NgbModal,
private usersService: UsersService,
private router: Router,
private ref: ChangeDetectorRef
) {
ref.detach();
setInterval(() => {
if (this.ref !== null &&
this.ref !== undefined &&
! (this.ref as ViewRef).destroyed) {
this.ref.detectChanges();
}
}, 5000);
}
ngOnInit() {
this.getAllUsers();
}
getAllUsers() {
this.subscription = this.usersService.getAll()
.subscribe(
(data:any) => {
this.users = data.users;
console.log(data);
},
error => {
alert("Error");
});
}
onCreateUser(form: NgForm){
const name = form.value.name;
const email = form.value.email;
const password = form.value.password;
console.log(name);
this.usersService.addUser(name, email, password)
.subscribe(
data => {
alert("User Created");
console.log(data);
this.modalRef.close();
this.usersService.clearCache();
this.getAllUsers();
},
error => {
alert("Error Adding");
console.log(error);
});
}
ngOnDestroy() {
clearInterval(this.intervalHolder);
this.subscription.unsubscribe()
}
它沒有錯誤。但它加載用戶列表太長 –
@GraySingh它應該立即加載用戶列表。 –
謝謝你。但是,當我打開兩個選項卡。我在一個選項卡中添加了一個用戶,並立即輸出。但是,第二個標籤不接收。我應該重新加載它以獲取新用戶。 –