1

我是 vue 3 类组件中单元测试的新手。我收到错误说商店未定义。这是我在 Home.vue 文件中的代码。

<script lang="ts">
import { Options, Vue } from "vue-class-component";
import HelloWorld from "@/components/HelloWorld.vue";
import { useStore } from "vuex";
import { key } from "@/store";
import { ProductType } from "@/types/commonTypeDefinitions";

@Options({
  components: {
    HelloWorld,
  },
})
export default class Home extends Vue {
  store = useStore(key);
  get getAllProducts() {
    return this.store.state.products ? this.store.state.products.groups : null;
  }
  mounted() {
    if (!this.store.state.products) {
      this.store.dispatch("getJsonData");
    }

  }
  goToDetail(item: ProductType) {
    localStorage.setItem("productDetail", JSON.stringify(item));
    this.store.commit("setProductDetail", item);
    const name = item.name.replaceAll(" ", "-");
    console.log(name);
    this.$router.push("/detail/" + name);
  }
}
</script>

我正在使用 jest 进行测试。请帮我解决这个问题。谢谢。

4

0 回答 0