开发者问题收集

app.config.globalProperties - 无法读取未定义的属性“globalProperties”

2021-06-25
5576

我尝试在 Vue3 中注册全局过滤器,但是它引发了此错误:

main.js?56d7:13 Uncaught TypeError: Cannot read property 'globalProperties' of undefined

根据 在 Vue3 中使用过滤器但无法读取 globalProperties ,它应该可以工作。

import { createApp } from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import "./assets/tailwind.css";
import axiosSetUp from "@/auth/axiosSetUp";
import {formatIsoDateTime as utils_formatIsoDateTime} from "@/utils";

axiosSetUp()
const app = createApp(App).use(store).use(router).mount("#app");


app.config.globalProperties.$filters = {
    formatIsoDateTime(isoString) {
        return utils_formatIsoDateTime(isoString)
    }
}

您知道问题出在哪里吗?

1个回答

您应该将根实例与根组件分开:

const app = createApp(App).use(store).use(router);

//use the root instance to add your config
app.config.globalProperties.$filters = {
    formatIsoDateTime(isoString) {
        return utils_formatIsoDateTime(isoString)
    }
}
// then you could mount it 
app.mount("#app")
Boussadjra Brahim
2021-06-25