【发布时间】:2021-01-10 09:09:14
【问题描述】:
我正在尝试使用 vue 3 中的组合 API 和以下文件创建某种状态:
// useNotifications.ts
const state = reactive<Array<Notification>>([]);
export function useNotifications() {
return {
state,
add: (notification: Notification) => {
state.push(notification);
},
};
}
此通知状态是从以下内容中读取并以 html 呈现的:
// TheNotifications.vue
setup() {
const notifications = useNotifications();
let first = notifications.state[0];
return {
first,
};
},
并在某些表单提交事件中添加通知,如下所示:
// SomeForm.vue
notifications.add({ type: "success", text: "Food added" });
但是,TheNotifications 在推送时永远不会看到更改。我尝试了一些不同的方法,例如toRef,但没有成功。我是这个组合 api 的新手,我想知道我错过了什么。
【问题讨论】:
标签: typescript vue.js vue-composition-api