为什么此vueline函数在设置中不起作用,而仅在方法内部使用?

发布于 2025-01-27 22:17:35 字数 1524 浏览 3 评论 0原文

我正在使用构图API使用vuelidate,但我不明白为什么v $ .. validate()在我放入Methods之后,在设置之后正常工作。,但不在设置中

因此,这有效:

setup() {
  // inspired from 
  // https://vuelidate-next.netlify.app/#alternative-syntax-composition-api
  const state = reactive ({
    // the values of the form that need to pass validation, like:
    name: ''
  })

  const rules = computed (() => {
    return {
      // the validation rules
    }

  const v$ = useVuelidate(rules, state)

  return {
    state,
    v$
  }
},
methods: {
  async submitForm () {
    const result = await this.v$.$validate()
    // either result: validation succeeded : axios post call
    // or failure and error messages show up.
  }
}

但是,这是不起作用的:

setup() {

  const state = reactive ({
    // the values of the form that need to pass validation, like:
    name: ''
  })

  const rules = computed (() => {
    return {
      // the validation rules
    }

  const v$ = useVuelidate(rules, state)

  const submitForm = async () {
    // **ERROR : Uncaught (in promise) TypeError: v$.$validate is not a function**
    const result = await v$.$validate()
    // either result: validation succeeded : axios post call
    // or failure and error messages show up.
  }

  return {
    state,
    v$,
    submitForm
  }
}

这有点痛苦,因为我在state是一个参数的情况下使用Axios调用的合并。将整个代码放在一个地方会更容易。

I'm using vuelidate with the composition API and I don't understand why the v$.validate() works correctly when I put within methods, after setup, but not within setup.

So this works:

setup() {
  // inspired from 
  // https://vuelidate-next.netlify.app/#alternative-syntax-composition-api
  const state = reactive ({
    // the values of the form that need to pass validation, like:
    name: ''
  })

  const rules = computed (() => {
    return {
      // the validation rules
    }

  const v$ = useVuelidate(rules, state)

  return {
    state,
    v$
  }
},
methods: {
  async submitForm () {
    const result = await this.v$.$validate()
    // either result: validation succeeded : axios post call
    // or failure and error messages show up.
  }
}

but, this doesn't work:

setup() {

  const state = reactive ({
    // the values of the form that need to pass validation, like:
    name: ''
  })

  const rules = computed (() => {
    return {
      // the validation rules
    }

  const v$ = useVuelidate(rules, state)

  const submitForm = async () {
    // **ERROR : Uncaught (in promise) TypeError: v$.$validate is not a function**
    const result = await v$.$validate()
    // either result: validation succeeded : axios post call
    // or failure and error messages show up.
  }

  return {
    state,
    v$,
    submitForm
  }
}

That's a bit of a pain, because I use a composable for the axios call where the state is an argument. Would be easier to keep the entire code in one place.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拿命拼未来 2025-02-03 22:17:35

useVuelidate返回计算,因此您需要在访问其任何属性时使用.value,例如$ error < /code>,<代码> $ validate 设置函数。

在模板中它为您解开。

Composition API

useVuelidate returns a computed, so you need to use .value when accessing any of it's properties, like $error, $validate inside the setup function.

In the template it is unwrapped for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文