Aurelia - EventAggregator 出现问题。看不懂 属性 "publish"

Aurelia - Trouble with EventAggregator. Cant read property "publish"

我的项目的所有其他部分都有 eventAggregator。我有两个根,我正在使用 eventAggregator 将消息发布到消息 div 并且它有效。

在我的 public 根目录中,我有 "public.ts",其中有我的路由配置。我在那里有一个 PostRenderStep,我在其中发布了一条消息。我正在发布一条空消息以强制消息消失。

这是我的 PostRenderStep class:

class PostRenderStep {

    constructor(private eventAggregator: EventAggregator) { }

    run(navigationInstruction: NavigationInstruction, next: Next): Promise<any> {
        console.log("I'm inside the POST activate step!")
        return Promise.resolve()
            .then(() => this.eventAggregator.publish('messages', new MessagePayload("", "", "")))
            .then(result => next());
    }
}

当我 运行 这样做时,我遇到了两个错误 - 第一个是事件聚合器:

aurelia-logging-console.js:47 ERROR [app-router] TypeError: Cannot read property 'publish' of undefined
at public.ts:87
at <anonymous>

在此错误之后是:

ERROR [app-router] Router navigation failed, and no previous location or fallbackRoute could be restored.

我怀疑带有导航的 postRenderStep 因 eventAggregator 行而失败并导致第二个错误。

我对此进行了调查,以查明是否是我的事件聚合器在 public 根目录中没有工作,但事实上我在更改根目录后发布了一条消息,并且该显示表明它正在工作。我还放置了一个按钮,单击该按钮时其中有一个发布,该按钮也有效表明它正在工作-只是不在此 class.

有人可以更详细地解释这个错误的意思,进一步解释它发生的原因以及如何解决它...

它在我的另一个根目录中完美运行,但在这个根目录中不行。

完整的public.ts文件

import { Aurelia, PLATFORM, autoinject } from 'aurelia-framework';
import {
    Redirect,
    NavigationInstruction,
    Router,
    RouterConfiguration,
    Next,
    PipelineProvider
} from "aurelia-router";
import { EventAggregator } from 'aurelia-event-aggregator';


import { Messages, MessagePayload } from '../../services/messages/messages'

@autoinject
export class Public {
    public router: Router;

    configureRouter(config: RouterConfiguration, router: Router): void {
        this.router = router;
        config.title = "Aurelia";
        config.addPostRenderStep(PostRenderStep);


        config.map([
            {
                route: ["", "home"],
                name: "home",
                settings: { icon: "home" },
                moduleId: PLATFORM.moduleName("../components/home/home"),
                nav: true,
                title: "Home"
            },
            {
                route: "counter",
                name: "counter",
                settings: { icon: "education", auth: false },
                moduleId: PLATFORM.moduleName("../components/counter/counter"),
                nav: true,
                title: "Counter"
            },
            {
                route: "fetch-data",
                name: "fetchdata",
                settings: { icon: "th-list", auth: false },
                moduleId: PLATFORM.moduleName("../components/fetchdata/fetchdata"),
                nav: true,
                title: "Fetch data"
            },
            {
                route: "login",
                name: "login",
                settings: { icon: "user", auth: false, },
                moduleId: PLATFORM.moduleName("../components/login/login"),
                nav: true,
                title: "Login"
            },
            {
                route: "notFound",
                name: "notFound",
                settings: { auth: false, },
                moduleId: PLATFORM.moduleName("../components/notFound/notFound"),
                nav: false,
                title: "Not Found"
            }
        ]);

        config.mapUnknownRoutes('../components/notFound/notFound');

    }
}

    class PostRenderStep {

        constructor(private eventAggregator: EventAggregator) { }

        run(navigationInstruction: NavigationInstruction, next: Next): Promise<any> {
            console.log("I'm inside the POST activate step!")
            return Promise.resolve()
                .then(() => this.eventAggregator.publish('messages', new MessagePayload("", "", "")))
                .then(result => next());
        }
    }

你没有 autoinject 在你的 PostRenderStep

@autoinject
class PostRenderStep {

}