I am trying to implement a simple messaging system where a div resides at the top of the page and it has one viewmodel called messages.
I followed THIS (Eric L. Anderson’s tutorial on the eventAggregator) tutorial but mine is a little different and is error-ing.
The viewmodel is called messages - first the template:
<template>
message: ${message}
alertType: ${alertType}
</template>
Here is the typescript file’s contents:
import { Aurelia, PLATFORM, autoinject } from "aurelia-framework";
import { EventAggregator } from 'aurelia-event-aggregator';
@autoinject
export class Messages {
public alertType: string;
public message: string;
constructor(private eventAggregator: EventAggregator) {
this.eventAggregator.subscribe(MessagePayload, (publishedMessage, alertType) => {
this.message = publishedMessage;
this.alertType = alertType;
});
};
As you can see the constructor has the subscribe function. “MessagePayload” is the class that can be instantiated. It goes to a function that takes two parameters.
This is “MessagePayload” - has two variables in its constructor.
export class MessagePayload {
alertType: string;
message: string;
constructor(message: string, alertType: string) {
this.message = message;
this.alertType = alertType;
}
}
I am publishing from a manual button for the moment and the function bound to that button is this:
secondTestAggregator() {
this.eventAggregator.publish(new MessagePayload("Updated test message", "success"));
}
I am getting an error on this publish line:
scheduler.ts:35 Uncaught TypeError: Cannot set property ‘message’ of undefined
at Scheduler.app/components/scheduler/scheduler.Scheduler.secondTestAggregator (scheduler.ts:35)
I dont know why this is not working and wonder if someone might point out the reason why?