在使用 Vue CLI(Vue3、Babel、Typescript)創建一個新的應用程式后,從 createApp 函式回傳的主應用程式物件上的'config'物件無法訪問。
我可以看到 Typescript 的'App'型別,以及相關的 config: AppConfig型別,通過在 VS Code 中進行深入研究。
但是VS Code將對'config'的訪問突出顯示為一個錯誤:
屬性'config'不存在于'ComponentPublicInstance<{}型別上。 {}, {}, {}, {}, {}, {}, false, ComponentOptionsBase<any, any, any, any, any, any, any, any, {}>>'.ts(2339)
。main.ts
span class="hljs-keyword">import { createApp } from 'vue'/span>
import App from ' ./App.vue'
import router from ' ./router'
import store from ' ./store'
const app = createApp(App)。 use(store).use(router).mount('#app'/span>)
app.config <--can'not do this
uj5u.com熱心網友回復:
是的,這是因為mount方法回傳ComponentPublicInstance,而不是vue實體本身。因為你已經把它鏈接到createApp方法,app常量將不包含vue實體。自然,不允許訪問配置屬性。請看下面的函式簽名:
mount(rootContainer: HostElement | string, isHydrate? : boolean, isSVG? : boolean)。ComponentPublicInstance;
你需要重新撰寫下面的代碼,以便訪問vue實體的配置屬性。
import { createApp } from 'vue'/span>
import App from ' ./App.vue'
import router from ' ./router'
import store from ' ./store'
const app = createApp(App)。
app.use(store)。
app.use(router)
app.mount('#app')。
//'You'll be able to access the config now。
app.config。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/307919.html
標籤:
