首頁  >  問答  >  主體

Vue Test Utils:如何將 Vuelidate 驗證規則傳遞給子元件?

在嘗試使用 vue test utils 編寫 component test 時,測試子元件和其他內容之間的交互,由於子元件中使用了 Vuelidate,我陷入了困境。下面是一個簡化的範例:

// parent component code

<template>
   <div>
      <childA />
   </div>
</template>
//childA code

<template>
   <input v-model="value" />
</template>

<script>
   ...
   validations: {
      value: {
         required
      }
   }
...
</script>
// parent component test
...
const wrapper = mount(MyParentComponent, {
   ...,
   components: {
      childA,
   },
   validations: {
      value: required
   },
   ...
})

我試圖找到一個可以安裝的解決方案(請注意,我還想安裝子元件,因此 shallow-mount 不是我所�%B

P粉590428357P粉590428357206 天前394

全部回覆(1)我來回復

  • P粉649990163

    P粉6499901632024-03-27 00:03:16

    為了回答您的問題,在我做了一些測試之後,我相信您錯過了 mount 內的 data 部分

    1. mount:渲染子元件
    2. shallowMount:不渲染子元件

    MyParentComponent 需要在選項中包含您子元件的結構,因此這就是他傳回錯誤的原因

    我看到您直接傳%E

    回覆
    0
  • 取消回覆