“StoreOptions<State>類型”Vuex 4+Vue 3+Typescript中不存在密鑰

我想用Typescript和Vue3建立一個(gè)Vuex 4商店。我?guī)缀鯖]有打字的經(jīng)驗(yàn)。

我遵循Vuex教程進(jìn)行初始設(shè)置,幾乎是復(fù)制粘貼。唯一不同的元素是,在我的State接口中,我有一個(gè)Station類型的鍵。

我得到了這個(gè)錯(cuò)誤

TS2345: Argument of type '{ station: {}; isOverlayActive: boolean; }' is not assignable to parameter of type 'StoreOptions<State>'.
  Object literal may only specify known properties, and 'station' does not exist in type 'StoreOptions<State>'.

這是我的Station接口

export default interface Station {
  uuid: string;
  name: string;
  teaser: {
    src: string;
    title: string;
  };
  event: {
    uuid: string;
    name: string;
    logo: {
      src: string;
      title: string;
    };
  };
  logo: {
    src: string;
    title: string;
  };
  theme: {
    mainColor: string;
    textColor: string;
  };
}

這是我的index.ts,在這里我定義了存儲(chǔ),在這里我得到了錯(cuò)誤

import { InjectionKey } from "vue";
import { createStore, useStore as baseUseStore, Store } from "vuex";
import Station from "@/interfaces/station";

export interface State {
  station: Station;
  isOverlayActive: boolean;
}

export const key: InjectionKey<Store<State>> = Symbol();

export const store = createStore<State>({
  station: {}, // here the compiler shows the error
  isOverlayActive: false,
});

export function useStore(): Store<State> {
  return baseUseStore(key);
}

我認(rèn)為Station并沒有造成問題,我還試圖在Store接口中設(shè)置station: number,并在store中設(shè)置station: 0,但我得到了相同的錯(cuò)誤。

我做錯(cuò)了什么?目標(biāo)是建立一個(gè)類型化商店。

? 最佳回答:

createStore函數(shù)中,需要將其包裝在屬性state中。

https://vuex.vuejs.org/guide/typescript-support.html#simplifying-usestore-usage

Instead of

export const store = createStore<State>({
  station: {}, // here the compiler shows the error
  isOverlayActive: false,
});

It's

export const store = createStore<State>({
  state: {
    station: {},
    isOverlayActive: false,
  }
});

它仍然會(huì)拋出一個(gè)錯(cuò)誤,因?yàn)?code>uuid、name等不是由{}定義的,但這是另一個(gè)錯(cuò)誤。

主站蜘蛛池模板: 一区二区视频在线观看| 一区二区精品在线观看| 精品人妻少妇一区二区三区不卡| 97精品一区二区视频在线观看| 精品一区中文字幕| 中文字幕AV无码一区二区三区| 国产福利视频一区二区| 亚洲国产精品一区二区九九| 精品国产一区二区三区在线| 亚洲一区二区三区不卡在线播放 | 亚洲码欧美码一区二区三区| 在线观看免费视频一区| 亚洲国产精品第一区二区三区| 美女免费视频一区二区| 四虎成人精品一区二区免费网站| 无码国产精品一区二区免费式影视| 色国产精品一区在线观看| 久久久精品人妻一区二区三区| 伦理一区二区三区| 久久精品国产免费一区| av无码人妻一区二区三区牛牛| 中文字幕久久久久一区| 一区二区乱子伦在线播放| 成人无码AV一区二区| 国产一区二区三区在线看| 三上悠亚一区二区观看| 国产AV午夜精品一区二区入口| 久久国产一区二区| 综合无码一区二区三区四区五区| 色窝窝无码一区二区三区 | 日本内射精品一区二区视频| 亚洲A∨无码一区二区三区| 中文字幕一区二区三区在线观看| 日本伊人精品一区二区三区| 亚洲av成人一区二区三区观看在线| 欧美日韩精品一区二区在线观看| 日韩在线视频不卡一区二区三区 | 99久久精品费精品国产一区二区| 一区二区三区影院| 精品一区二区三区无码免费视频| 亚洲色偷精品一区二区三区|