我目前有一项包含我需要的功能的角服务。如何让服务与组件相互作用?

发布于 2025-01-19 07:52:07 字数 519 浏览 0 评论 0原文

我想知道如何成功让我的服务将其数据传递到位于不同目录中的所需组件。

成分

import {Component, Inject, OnInit} from '@angular/core';

import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material/dialog';

@Component({
  selector: 'app-add-view-modal',
  templateUrl: './add-view.component.html',
  styleUrls: ['./add-view.component.css']
})
export class AddViewComponent implements OnInit {

constructor(public dialogRef: MatDialogRef<AddViewComponent>,
  @Inject(MAT_DIALOG_DATA) data) {}

  ngOnInit() {
  }
}

I would like to know how I can successfully have my service pass its data to the required component that is located in a different directory.

Component

import {Component, Inject, OnInit} from '@angular/core';

import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material/dialog';

@Component({
  selector: 'app-add-view-modal',
  templateUrl: './add-view.component.html',
  styleUrls: ['./add-view.component.css']
})
export class AddViewComponent implements OnInit {

constructor(public dialogRef: MatDialogRef<AddViewComponent>,
  @Inject(MAT_DIALOG_DATA) data) {}

  ngOnInit() {
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

肥爪爪 2025-01-26 07:52:07

你可以使用 Angular 的自动依赖注入

import {ViewManagerService} from 'path/to/view-manager.service.ts';
import {Component, Inject, OnInit} from '@angular/core';
import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material/dialog';


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

export class AddViewComponent implements OnInit {

constructor(
   private viewManagerService: ViewManagerService,
   public dialogRef: MatDialogRef<AddViewComponent>,
   @Inject(MAT_DIALOG_DATA) data
) {
}

ngOnInit() {
   this.viewManagerService.doSomething();
}
}

You can just use Angular's automatic dependency injection:

import {ViewManagerService} from 'path/to/view-manager.service.ts';
import {Component, Inject, OnInit} from '@angular/core';
import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material/dialog';


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

export class AddViewComponent implements OnInit {

constructor(
   private viewManagerService: ViewManagerService,
   public dialogRef: MatDialogRef<AddViewComponent>,
   @Inject(MAT_DIALOG_DATA) data
) {
}

ngOnInit() {
   this.viewManagerService.doSomething();
}
}
梅倚清风 2025-01-26 07:52:07

首先,将您的服务添加到 app module.ts 文件中的提供程序

然后在构造函数中添加服务,如下代码

import {Component, Inject, OnInit} from '@angular/core';

import {serviceClassName} from '../service/serviceClassName';

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

export class AddViewComponent implements OnInit {

constructor(public serviceName: serviceClassName) {}

ngOnInit() {}

使用在服务中声明的所需功能或变量,如下所示

 this.serviceName.finctionName();

First, add your service to the provider in app module.ts file

Then add the service in constructor like below code

import {Component, Inject, OnInit} from '@angular/core';

import {serviceClassName} from '../service/serviceClassName';

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

export class AddViewComponent implements OnInit {

constructor(public serviceName: serviceClassName) {}

ngOnInit() {}

Use the needed functionality or variable declared in service like below

 this.serviceName.finctionName();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文