有没有一种方法可以对所有消息只使用一个p-oast



我正在使用priming p-oast来显示消息。

我在app.component.html 中放置了p-toast html

<div class="main-container">
<router-outlet></router-outlet>
</div>
<p-toast position="top-right" key="main"></p-toast>

并在共享模块中导入ToastModule和MessageService,以便任何组件都可以使用它

尝试在许多子组件上使用此选项显示消息,但未显示

this.messageService.add({
key: "main",
severity: "info",
detail: "Ready to scan",
});

我仍然需要在孩子的html中添加p-toast html才能使其工作。

子组件通过app-routing.module 加载

const routes: Routes = [
{ path: "", component: HomeComponent },
{
path: "catalogs",
loadChildren: () =>
import("./catalog/catalog.module").then((m) => m.CatalogModule)
}];
@NgModule({
imports: [
RouterModule.forRoot(routes)
],
exports: [RouterModule],
})
export class AppRoutingModule {}

有没有办法只声明一个,并且调用messageService.add的子组件可以在该p-oast上显示消息?

我在@yurzui 的帮助下解决了这个问题

以前,ToastModule和MessageService都是在SharedModule中导入的,这样我就不必重复将东西导入到我创建的其他模块中。

我刚刚将ToastModule和MessageService导入从SharedModule移到AppModule由于<p-toast>仅在app.component.html 中

相关内容

最新更新