怎樣解決這個TypeError: Cannot read property 'asObservable' of undefined

我在VISUAL studio code 裏是沒有出現error,但是當我執行程式,它就出現
ERROR Error: Uncaught (in promise): TypeError: Cannot read property ‘asObservable’ of undefined
core.js:6241

我的service code 如下:
import { Injectable } from ‘@angular/core’;

import {BehaviorSubject, ReplaySubject} from ‘rxjs’;

@Injectable({

providedIn: ‘root’

})

export class LibraryserivceService {

private bookrow: BehaviorSubject;

currentrow = this.bookrow.asObservable();

constructor() { }

addbookrow(row: any)

{

this.bookrow.next(row);

console.log(row);

}

}

thank you very much.

你沒有建立 bookrow 的實體,當然無法 asObservable`

private bookrow: BehaviorSubject = new BehaviorSubject([defaultValue])
1個讚