Error in typescript class

2

Good afternoon, I have the following problem with a function typescript

[ts] Property 'notificaciones' does not exist on type 'typeof Alertas'.

I need to use the function called message in a component but when I set the function as public static I get that message, while if I remove the static does not send that error.

the code is as follows

import { NotificationService } from "../shared/utils/notification.service";

export class Alertas {

    constructor(private notificaciones:NotificationService) { }

    public static  mensaje() {
        this.notificaciones.smallBox({
            title: "Sistema Minutas",
            content: "Los datos se guardaron correctamente",
            color: "#739E73",
            timeout: 4000,
            iconSmall: "fa fa-check",
        });
    } 
}

notifications.service this is the code of my service

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

declare var $: any;

@Injectable()
export class NotificationService {

  constructor() {
  }

  smallBox(data, cb?) {
    $.smallBox(data, cb)
  }

  bigBox(data, cb?) {
    $.bigBox(data, cb)
  }

  smartMessageBox(data, cb?) {
    $.SmartMessageBox(data, cb)
  }

}
    
asked by Gonzalo Alberto 02.06.2017 в 20:24
source

1 answer

1

A static method can only access other members static of the class.

Indeed as you mention if you remove the static should not give you any error.

import { NotificationService } from "../shared/utils/notification.service";

export class Alertas {

    constructor(private notificaciones: NotificationService) { }

    public mensaje() {
        this.notificaciones.smallBox({
            title: "Sistema Minutas",
            content: "Los datos se guardaron correctamente",
            color: "#739E73",
            timeout: 4000,
            iconSmall: "fa fa-check",
        });

    } 
}

To then call this method, you must instantiate class Alertas in the same way.

// Asumiendo que ya tienes una instancia de NotificationServices
let alertas = new Alertas(notificaciones);
alertas.mensaje();
    
answered by 02.06.2017 в 21:02