Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 属性不存在于类型';类型';有棱角的_Angular_Typescript - Fatal编程技术网

Angular 属性不存在于类型';类型';有棱角的

Angular 属性不存在于类型';类型';有棱角的,angular,typescript,Angular,Typescript,我试图在angular中向我的组件导入一个方法 但我得到了以下错误: Property 'alerta' does not exist on type 'typeof PasswordResetService'. any 如果你看代码,一切都是正确的 这是一个名为password-reset.component.ts的组件,我在其中调用该方法 import { PasswordResetService } from './password-reset.service'; @Component

我试图在angular中向我的组件导入一个方法

但我得到了以下错误:

Property 'alerta' does not exist on type 'typeof PasswordResetService'. any
如果你看代码,一切都是正确的

这是一个名为password-reset.component.ts的组件,我在其中调用该方法

import { PasswordResetService } from './password-reset.service';

@Component({
  selector: 'app-password-reset',
  templateUrl: './password-reset.component.html',
  styleUrls: ['./password-reset.component.css']
})
export class PasswordResetComponent implements OnInit {
  modalRef: BsModalRef; 
  constructor(private modalService: BsModalService) { }

  openAlertModal() {
    PasswordResetService.alerta("Título", "Mensaje").subscribe((answer) => {});
  }
错误在openAlertModal()方法中

如果您查看PasswordReset服务,一切正常:

export class PasswordResetService {
  BsModalRef: BsModalRef;

  constructor(private BsModalService: BsModalService) { }

  public alerta(title: string, message: string) : Observable<string> {
    const initialState = {
        title,
        message,
    };
    this.BsModalRef = this.BsModalService.show(AlertModalComponent, { initialState });
  
    return new Observable<string>(this.getAlertaSubscriber());
  }
导出类密码重置服务{
BsModalRef:BsModalRef;
构造函数(专用BsModalService:BsModalService){}
公共警报(标题:字符串,消息:字符串):可观察{
常量初始状态={
标题
消息
};
this.BsModalRef=this.BsModalService.show(AlertModalComponent,{initialState});
返回新的可观察对象(this.getAlertAsSubscriber());
}
方法alerta()就在那里

有人能帮我吗


谢谢!

如果您想使用该服务,应该将其添加到构造函数中

  • 在模块中注册服务(
    app.module.ts
  • import{PasswordResetService}from./password reset.service';//提供相关路径
    @NGD模块({
    声明:[
    //您的组件
    ],
    供应商:[
    //您的密码重置服务
    密码重置服务
    ]
    })
    
  • 导入所需组件中所需的PasswordRestService
  • 从“/password reset.service”导入{PasswordResetService};
    @组成部分({
    选择器:“应用程序密码重置”,
    templateUrl:“./password reset.component.html”,
    样式URL:['./密码重置.component.css']
    })
    导出类PasswordResetComponent实现OnInit{
    modalRef:BsModalRef;
    建造师(
    专用modalService:BsModalService,
    私人密码重置服务:密码重置服务
    ) { }
    openAlertModal(){
    这个.passwordResetService.alerta(“Título”,“Mensaje”).subscribe((答案)=>{});
    }
    }
    
    如果要使用服务,应将其添加到构造函数中

  • 在模块中注册服务(
    app.module.ts
  • import{PasswordResetService}from./password reset.service';//提供相关路径
    @NGD模块({
    声明:[
    //您的组件
    ],
    供应商:[
    //您的密码重置服务
    密码重置服务
    ]
    })
    
  • 导入所需组件中所需的PasswordRestService
  • 从“/password reset.service”导入{PasswordResetService};
    @组成部分({
    选择器:“应用程序密码重置”,
    templateUrl:“./password reset.component.html”,
    样式URL:['./密码重置.component.css']
    })
    导出类PasswordResetComponent实现OnInit{
    modalRef:BsModalRef;
    建造师(
    专用modalService:BsModalService,
    私人密码重置服务:密码重置服务
    ) { }
    openAlertModal(){
    这个.passwordResetService.alerta(“Título”,“Mensaje”).subscribe((答案)=>{});
    }
    }
    
    1。)如果PasswordResetService只是一个类,则必须首先使用new关键字构造它

    2.)如果它是可注入服务(带有@injectable decorator),则必须在构造函数中提供它以进行依赖项注入

    谢谢。

    1。)如果PasswordResetService只是一个类,则必须首先使用new关键字构造它

    2.)如果它是可注入服务(带有@injectable decorator),则必须在构造函数中提供它以进行依赖项注入


    谢谢。

    除非您的问题与编辑器本身特别相关,否则请不要标记IDE或代码编辑器。很抱歉..@mattdmoy您没有实例化您的类。因此,请创建它的实例(使用new)或者将您的方法设置为静态。另外,为什么不为此使用实际的Angular服务,并像使用modalService一样注入它?请不要标记IDE或代码编辑器,除非您的问题与编辑器本身特别相关。很抱歉,..@MattDMoYou没有实例化您的类。因此,要么创建一个实例(使用new)或者让你的方法是静态的。另外,为什么不使用一个实际的角度服务来实现这一点,并像使用modalService一样注入它呢?