首页 > 解决方案 > Vue Ruter 4 没有重载匹配这个调用。重载 1 of 3,给出以下错误

问题描述

我在 vuex 中创建动作:

export interface Actions {
  getProductsByCategory({ commit }: { commit: Commit }, slug: string): Promise<Product[]>
}

export const actions: ActionTree<State, RootState> & Actions = {
  async getProductsByCategory ({ commit }, slug): Promise<Product[]> {
    const products = await getProductsByCategory(slug)
    commit('setProductList', products)
    return products
  }
}

和路线一样

{
    path: '/category/:slug',
    name: 'category',
    component: Category,
},

我要做的最后一件事是调用名为“getProductsByCategory”的调度。为此,我使用 Onmounted Hook 制作了小组件

setup () {
    const store = useStore()
    const route = useRoute()
    const products: Ref<Product[]> = ref([])

    onMounted(async () => {
      const slug = route.params.slug
      products.value = await store.dispatch('getProductsByCategory', slug)
    })

    return {
      products
    }
  }

但是打字稿告诉我错误

Type 'string | string[]' is not assignable to type 'string'.
  Type 'string[]' is not assignable to type 'string'.

为什么 TS 不明白 slug 是一个字符串,我该如何纠正这个问题?

标签: typescriptvuejs3vue-router4

解决方案


推荐阅读