我的项目的所有其他部分都有eventAggregator。我有两个根,我使用eventAggregator将消息发布到消息div,它可以工作。
在我的公共根目录中有“public.ts”,其中包含我的路由配置。在那里,我有一个PostRenderStep,在其中我发布了一条消息。。我发布了一条空消息来强制消息消失。
这是我的PostRenderStep类:
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行而失败,并导致第二个错误。
我对此进行了调查,以确定是否是我的事件聚合器在公共根目录中不起作用,但事实上我在更改根目录后发布了一条消息,这表明它起作用了。我还放置了一个按钮,当点击时,其中有一个发布,也可以工作,这表明它可以工作-只是不在这个类中。
有人能更详细地解释一下这个错误在说什么,以及为什么它会发生以及如何修复它吗。。。
它在我的另一个根中工作得很好,但在这个根中没有。
全体公众。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());
}
}