【问题标题】:How to call prop function from setup in vue 3如何在 vue 3 中从 setup 中调用 prop 函数
【发布时间】:2021-05-30 12:40:14
【问题描述】:

我有下一个代码:

HTML:

<p @click="changeForm">Iniciar sesion</p>

JS

export default {
   name: "Register",
   props: {
      changeForm: Function,
   },
   setup() {
      //How can i call props changeForm here???
   }
}

如何从 JS 调用我的 props 函数?另一种方法是触发点击我的 p 元素,这可能吗?

我正在使用 vue 3。我已经用 this.changeForm 和 props.changeForm 进行了测试,但没有运气。

【问题讨论】:

标签: javascript vue.js vue-component vuejs3 vue-props


【解决方案1】:

setup 函数接收props 作为其第一个参数:

export default {
   name: "Register",
   props: {
     changeForm: Function,
   },
   setup(props) {            // receive `props` argument
     props.changeForm();     // use it to access `changeForm`
   }
}

【讨论】:

  • 如何在 Vue3 中从 method 访问 props? thisprops$propsthis.$props 工作。
  • @ElectRocnic - 如果您使用选项 API(即 methods 块),它与 Vue 2 相同:this.myPropName。否则,您的方法将在 setup 中,并且您将使用上述格式。
  • 是的,后来我找到了。没有人告诉我的是,我现在必须在setup() 方法内定义methods,然后在setup() 内的{ method1, method2, prop1, prop2, etc. } 块中返回它们。这完全让我感到困惑。
猜你喜欢
  • 2021-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-18
  • 1970-01-01
  • 1970-01-01
  • 2021-01-14
相关资源
最近更新 更多