【发布时间】:2021-02-27 21:52:15
【问题描述】:
我正在开发用于显示 vue 3 的通知/toast 的小型库。 我的想法是在插件注册期间为我的通知附加一个不可见的容器。所以最终用户不应该关心渲染这个区域。有可能吗?
我目前的插件是这样的:
export const plugin = {
install: (app: App, options?) => {
options = reactive(options || defaultOptions);
app.provide(symbol, instance);
app.component('vue3-notification', Notification);
app.component('vue3-notifications', Overlay);
console.log('app', app); // app._component is null at this point
var test = Overlay.render({ notifications: instance });
console.log('test', test); // how to attach Overlay component to app?
}
};
似乎在安装插件时 vue 根容器尚不可用。我设法渲染我的组件以提供所需的依赖项(至少我希望如此,它已在最后一行记录到控制台)但我不知道如何安装它并与主应用程序集成。
我想从插件自动渲染的叠加组件如下所示:
<div class="notifications-overlay">
<Teleport to="body">
<vue3-notification
v-for="(n, index) in notifications.stack.value"
:key="n.id"
v-bind="n"
v-bind:hide="() => hide(n.id)"
></vue3-notification>
</Teleport>
</div>
而且它有固定的位置:
.notifications-overlay {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
pointer-events: none;
}
所以它在哪里渲染并不重要,我只想在使用我的插件后自动在 vue 应用程序中可用。
有什么想法吗?
【问题讨论】:
-
github.com/yariksav/vuetify-dialog 创建一个特定的 div 用于显示通知和祝酒词。查看他们的代码以了解他们是如何做到的,我们只需使用他们的包....