【问题标题】:Angular 2 Service not functioning as Singleton in Child Component (possible duplicate)Angular 2 服务在子组件中不能作为单例运行(可能重复)
【发布时间】:2017-09-30 12:35:54
【问题描述】:

第二次更新

我的问题在于服务本身,将方法更改为箭头函数解决了问题。


这可能是重复的,但我已经对 Angular DI 进行了详尽的研究,很明显,在根级别注入服务可以使它们作为同一个实例提供给应用程序中的所有组件。

在我的 AppComponent 下,我有三个子屏幕截图、确认和预览。我从 Screenshot 组件调用服务并在该服务上保存一些数据,然后尝试稍后从 Preview 组件访问该数据,但似乎我正在访问该服务的新实例。

app.module

@NgModule({
  declarations: [
    AppComponent,
    Screenshot,
    Confirm,
    Preview,
  ],
  entryComponents: [
    Preview
  ],
  imports: [
    CommonModule,
    BrowserModule
  ],
  providers: [
    MessageService,
    ScreenshotService
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})

export class AppComponent implements OnInit, OnChanges, OnDestroy {
    @ViewChild(Preview) preview: Preview; 

    title: string;
    confirmed: boolean;
    constructor(
        private screenshotService: ScreenshotService,
        private messageService: MessageService,
    ) {
        this.title = 'Media Radar';
        this.confirmed = false;
    }

    onNotify(confirm): void {
        this.confirmed = confirm;
        if(this.confirmed) {
            this.preview.showPreview();
        }
    }
    ...
}

app.component.html

<div class='popup'>
    <h3>{{ title }}</h3>
    <screenshot [hidden]="confirmed" (showConfirm)='onNotify($event)'></screenshot>
    <confirm [hidden]="!confirmed" (hideConfirm)='onNotify($event)'></confirm>
    <preview [hidden]="!confirmed"></preview>
</div>

preview.component

import { ScreenshotService } from './screenshot.service';
@Component({
    selector: 'preview',
    template: `<canvas #preview></canvas>`
})

export class Preview {
    @ViewChild('preview') preview: ElementRef;
    private canvas: HTMLCanvasElement;
    constructor(
        private screenshotService: ScreenshotService,
        private renderer: Renderer2
    ) {}

    showPreview() {
        console.log(this.screenshotService) // shows a new instance with no data
    ...
    }
}

为避免代码过于冗长,我将省略服务和其他首先调用它但可以在需要时发布的子组件。

更新

screenshot.service

import { Injectable } from '@angular/core';

@Injectable()
export class ScreenshotService {
    public imageURL: string = '';
    public title: string  = '';
    public height: number = 0;
    public width: number = 0;

    constructor() {
    }
    convertToBlob(): Promise<any> {
        console.log(this); // returns all the initial values above when called from the Preview component the second time despite being previously assigned values when a different method was called from a different service
    }
    ...
}

【问题讨论】:

  • 你在服务中使用了@Injectable() 吗?
  • 是的。我将添加一些服务以获得更多上下文。
  • 你能建立一个 plunker 来展示你的问题吗?
  • 我有一个示例,显示了跨组件工作的服务:github.com/DeborahK/Angular-Routing 查看 APM-Final 文件夹中的 message.service.ts 文件。
  • 您在哪里注入了身份验证和消息服务?我没有看到它 main.ts、app.module 或 app.component。

标签: angular service dependency-injection angular2-services angular2-directives


【解决方案1】:

我的问题在于服务方法。我将它们更改为箭头函数,它解决了所有问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 2020-10-08
    • 2017-10-19
    • 2018-08-27
    • 2018-03-14
    相关资源
    最近更新 更多