0

我在 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 是一个字符串,我该如何纠正这个问题?

4

0 回答 0