2017-02-11 102 views
0

我已經環顧了一下,但似乎沒有關於在Angular2中的服務之間共享數據的材料。我有兩個@Injectable()服務,其中一個是單播數據廣播。在其他服務,我有一些代碼作爲Web音軌播放,更新一個名爲進度變量:在Angular2中跨服務共享數據

import { SharedService } from '../shared.service'; 

... 

@Injectable() 
export class WebAudioTrack implements IAudioTrack { 
    public _progress: number = 0; 
    ... 
    constructor(public src: string, @Optional() public preload: string = 'none', @Optional() private ctx: AudioContext = undefined) { 
    // audio context not needed for now 
    // this.ctx = this.ctx || new AudioContext(); 
    this.createAudio(); 
    } 
    private onTimeUpdate(e: Event) { 
    if (this.isPlaying && this.audio.currentTime > 0) { 
     # I want to do something like SharedService.setProgress(this.audio.currentTime) here 
     this._progress = this.audio.currentTime; 
     this._completed = this.audio.duration > 0 ? Math.trunc (this.audio.currentTime/this.audio.duration * 100)/100 : 0; 
    } 
    } 

我正在尋找一種方式來廣播以上到另一個@Injectable稱爲SharedService在代碼中_progress變量。我甚至不需要在上面的代碼中設置BehaviorSubject,因爲每次onTimeUpdate觸發時我都應該可以像SharedService.setProgress(time)那樣執行一些操作。

問題是,如何將SharedService添加到上面的代碼?當我嘗試將它添加到構造函數中時,它會抱怨,因爲有調用「新WebTrackAudio(變量,變量)」。這是其他服務代碼,這是不完整的,可能會有點混淆:

@Injectable() 
export class SharedService { 
    progress: number; 

    setProgress(progress: number) { 
     console.log(progress); 
     this.progress = progress; 
    } 
    getProgress() { 
     return this._webTrack._progress; 
    } 

} 

謝謝!

當我更新的第一個服務的構造,包括SharedService像這樣:

constructor(public src: string, @Optional() public preload: string = 'none', @Optional() private ctx: AudioContext = undefined, public sharedService: SharedService) { 
    // audio context not needed for now 
    // this.ctx = this.ctx || new AudioContext(); 
    this.createAudio(); 
    } 

我收到以下錯誤:

打字稿錯誤

Supplied parameters do not match any signature of call target.

.../src/app/ionic-audio/ionic-audio-providers.ts

create(track: ITrackConstraint) {

let audioTrack = new WebAudioTrack(track.src, track.preload);

Object.assign(audioTrack, track);

+1

我一定會深究問題的核心,可以刪除2/3的代碼。我發現這個問題實際上來自所有的噪音,令人困惑。要在服務之間共享數據,只要不引起循環依賴關係,就可以將服務注入另一個服務。 –

+0

「添加到構造函數」是什麼意思?如果它是服務,爲什麼你需要調用'新的WebTrackAudio(變量,變量)'? –

+0

我實際上已經刪除了大約90%的代碼(你可以看到... s)。 我想這不是服務?之後怎麼樣了? 我仍然讓我的腳溼了angular2 – user2476265

回答

2
import { SharedService } from '../shared.service'; 

... 

@Injectable() 
export class WebAudioTrack implements IAudioTrack { 
    public _progress: number = 0; 
    ... 

    constructor(private sharedService:SharedService) {} 

    private onTimeUpdate(e: Event) { 
    if (this.isPlaying && this.audio.currentTime > 0) { 
     # I want to do something like this.sharedService.setProgress(this.audio.currentTime) here 
     this._progress = this.audio.currentTime; 
     this._completed = this.audio.duration > 0 ? Math.trunc (this.audio.currentTime/this.audio.duration * 100)/100 : 0; 
    } 
    } 

確保提供兩項服務,如

@NgModule({ 
    ..., 
    providers: [WebAudioTrack, SharedService], 
}) 
export class AppModule {} 
+0

這種技術導致我得到以下TS錯誤:提供的參數不匹配調用目標的任何簽名。 編輯帖子以反映詳情 – user2476265

+0

@ user2476265您無法在可選參數後面聲明必需的參數。 –

+0

這些可選參數在服務中根本沒有意義。有必要澄清課程實際使用的方式。 –