【发布时间】:2020-12-19 03:46:39
【问题描述】:
我正在构建一个通知系统,它可以正常工作,但不能正常工作。我有以下合成功能
const data = reactive({
notifications: []
});
let notificationKey = 0;
export const useNotification = () => {
const visibleNotifications = computed(() => {
return data.notifications.slice().reverse();
});
const add = (notification: INotification) => {
notification.key = notificationKey++;
notification.type = notification.type ?? 'success';
notification.icon = iconObject[notification.type];
notification.iconColor = iconColorObject[notification.type];
data.notifications.push(notification);
notificationTimer[notification.key] = new Timer(() => {
remove(notification.key);
}, notificationTimeout);
};
const remove = (notificationKey: number) => {
const notificationIndex = data.notifications.findIndex(notification => notification?.key === notificationKey);
if (notificationTimer[notificationKey] !== undefined) {
notificationTimer[notificationKey].stop();
}
if (notificationIndex > -1) {
data.notifications.splice(notificationIndex, 1);
}
};
const click = (notification: INotification) => {
/// ... click code
};
return {
visibleNotifications,
add,
remove,
click
};
};
这是有效的(它被简化了一点)。现在,我在 Webpack 中有两个入口点。在一个入口点 (auth) 中,我有以下代码来加载 Vue 组件以显示通知
Promise.all([
import(/* webpackChunkName: "Vue"*/ 'vue'),
import(/* webpackChunkName: "@vue/composition-api"*/ '@vue/composition-api'),
import(/* webpackChunkName: "Notifications"*/'components/Notifications.vue')
]).then((
[
{ default: Vue },
{ default: VueCompositionAPI },
{ default: Notifications },
]) => {
Vue.use(VueCompositionAPI);
new Vue({
render: h => h(Notifications)
}).$mount('.notification-outer);
});
现在,一切正常,我在其中添加以下代码
import { useNotification } from 'modules/compositionFunctions/notifications';
useNotification().add({
title : 'Error',
message: 'This is an error notification',
type : 'error',
});
然后通知显示如预期这一切都发生在“auth”入口点内部,以上都是打字稿。
现在,如果我转到我的第二个入口点(编辑器),并在现有的 JS 文件中输入以下代码
import(/* webpackChunkName: "useNotification"*/ 'modules/compositionFunctions/notifications').then(({ useNotification }) => {
useNotification().add({
title : 'Block Deleted',
message : 'The block has been deleted',
buttonText: 'Undo',
buttonFunction () {
undoDelete();
}
});
});
然后它“工作”,我的意思是,useNotification 函数中的所有代码都工作。 add 方法将添加它,(如果我控制台注销响应属性),并且在 15000 毫秒后,删除方法发生,我可以添加日志来显示这一点。但是,Vue 组件永远不会看到这种变化。如果我在 Vue 组件中添加一个手表,然后退出,第一个通知(上图)将使 JS 登录到控制台,但是,当从“编辑器”入口点添加它时,它不会做任何东西。
Vue 组件 JS
import { useNotification } from 'modules/compositionFunctions/notifications';
import { defineComponent } from '@vue/composition-api';
export default defineComponent({
name : 'Notifications',
setup () {
const { visibleNotifications, remove, click: notificationClick } = useNotification();
return {
visibleNotifications,
remove,
notificationClick,
};
},
watch: {
visibleNotifications: (v) => {
console.log(v);
}
}
});
请有人告诉我他们可以帮忙吗?这开始让我头疼了……
TIA
【问题讨论】:
-
能否提供codesandbox中的demo??
-
你能显示
remove方法的代码吗? -
@tuhin47 - 我不这么认为?这是一个具有多个端点的完整 Webpack 设置,我认为这在 Codepen 中是不可能的。
-
@KrzysztofKaczyński 我不认为删除方法是问题,因为当我注销时,
data包含所有内容。无论如何都在上面添加了它 -
您能否确认
Notifications组件在您使用编辑器入口点时实际呈现在某处?此外,您能否尝试将控制台日志记录放在您的notifications.ts的顶部,以确认该文件只被拉入一次(以确认data.notifications是唯一的)。假设这些都不是问题,您是否可以在 GitHub 上放一个精简、可构建、可运行的应用程序版本,以便我们自己尝试?
标签: javascript vue.js webpack vuejs2 vue-composition-api