【发布时间】:2022-01-09 00:08:42
【问题描述】:
我偶然发现了 Typescript 和 Vue3 的一些问题。我想我在这里用错了 Typescript。
我使用 Vue 的 Composition API 创建了自己的商店。
import {computed, reactive} from "vue";
const state = reactive({
accessToken: undefined,
user: {},
})
const isAuthenticated = computed(() => {
return state.accessToken !== undefined
})
export default {
state: readonly(state),
isAuthenticated
}
为它写一个类型/接口:
import {ComputedRef} from "vue";
export interface UserStore{
state: Readonly<any>;
isAuthenticated: ComputedRef<boolean>;
}
但是当我现在想在我的 vue 组件中使用它时。
例如这样:
<h1 v-if="userStore.isAuthenticated">is true</h1>
即使明显为假,它也会返回真。
我通过 inject 注入 store:
setup(){
return {
userStore: inject('userStore') as UserStore
}
}
当我想返回一个 computed() 字符串时会出现类似的问题。当我在模板中使用它时,它被包含在引号中。
这里有什么问题?
#编辑 我在 main.ts 中提供 UserStore
/* Stores */
import UserStore from './store/user-store'
const app = createApp(App)
.use(IonicVue, {
mode: 'ios'
})
.use(router)
.provide('userStore',UserStore);
router.isReady().then(() => {
app.mount('#app');
});
【问题讨论】:
-
你在哪里
provideuserStore?似乎是反应性问题,而不是打字稿问题。 -
我在 main.ts 中将它提供给 vue 应用程序。上面编辑:)
标签: typescript vue.js