我在 main.ts 中添加了一个全局属性,并试图在组件中访问它。但是我收到了错误:
Property '$const' does not exist on type 'ComponentPublicInstance<{}, {}, {}, {}, {}, EmitsOptions, {}, {}, false, ComponentOptionsBase<{}, {}, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, EmitsOptions, string, {}>>'.Vetur(2339)
我认为我需要增加类型,typescript 现在认为 $const 是任何类型,但是我不知道如何在 Vue 3 中执行此操作,并且文档中也没有提及。
main.ts
import { createApp } from "vue";
import App from "./App.vue";
import store from "./store";
import * as constants from "@constants";
const app = createApp(App);
app.config.globalProperties.$const = Object.freeze(constants);
app.use(store);
app.mount("#app");
零件
<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
name: "Header",
computed: {
tags() {
return Object.entries(this.$const.TAGS);
}
}
});
</script>
任何帮助,将不胜感激。