Javascript 角度5异步管道不';不能使用FirebaseDatabase
我正在尝试使用Javascript 角度5异步管道不';不能使用FirebaseDatabase,javascript,angular,firebase,firebase-realtime-database,angularfire2,Javascript,Angular,Firebase,Firebase Realtime Database,Angularfire2,我正在尝试使用angular5和Firebase(使用angularfire2),并尝试观察async管道的行为 据指出,每当我们将async管道连接到可观察的时,只要组件被销毁(即ngondestry()),它就会自动取消订阅 但是当我将async管道连接到FirebaseDatabase时,请参见下面的代码 // This is 'app.component.ts' import { Component, OnInit, OnDestroy } from '@angular/core'; i
angular5
和Firebase
(使用angularfire2
),并尝试观察async
管道的行为
据指出,每当我们将async
管道连接到可观察的时,只要组件被销毁(即ngondestry()
),它就会自动取消订阅
但是当我将async
管道连接到FirebaseDatabase时,请参见下面的代码
// This is 'app.component.ts'
import { Component, OnInit, OnDestroy } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit, OnDestroy {
$mycourses;
constructor(private firebaseDB: AngularFireDatabase) {
}
ngOnInit() {
this.$mycourses = this.firebaseDB.list('courses').valueChanges();
}
ngOnDestroy() {
console.log('Component Destroyed');
}
}
而
// This is 'app.component.html'
<h1>My Current Courses:</h1>
<ul>
<li *ngFor="let course of $mycourses | async">
<p class="course">{{ course.COURSE }}</p>
<span class="course-code"> - {{ course.COURSE_CODE }}</span>
<span class="au"> - {{ course.AU }}</span>
</li>
</ul>
<button (click)="ngOnDestroy()">Destroy Component</button>
//这是“app.component.html”
我目前的课程:
-
{{course.course}
-{{course.course_CODE}
-{{course.AU}}
销毁组件
预期行为没有发生,即在单击销毁组件
按钮后,每当我在Firebase控制台上修改列表
和对象
时,我的组件仍然会立即在数据库上反映更改,而应该不会反映更改
我做错了什么?调用ngondestory
不会破坏组件
:
当指令、管道或服务被销毁时调用的生命周期挂钩
Ngondestory回调通常用于任何需要
需要在实例被销毁时发生
这意味着您可以在Ngondestory函数中执行自定义取消订阅,该函数将在组件被销毁时调用
如果要测试组件销毁,请创建子组件并使用模板:
<ng-container *ngIf="existingComponent">
<child-component></child-component>
<ng-container>
<button (click)="existingComponent = !existingComponent">Create/Destroy</button>
创建/销毁
当您的组件已销毁且不在视图中时,您如何查看该组件?