2017-02-11 119 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

这些可选参数在服务中根本没有意义。有必要澄清课程实际使用的方式。 –