在我的Nuxt3应用程序中,我试图将邮箱输入字段中输入的值从子组件(SignForm.vue)发出到父组件(Signup.vue),以便我可以使用它来注册新用户

signup.vue

<script setup>
    const props = {
        fields: [
            "email",
            "password",
            "confirm",
        ],
        header: "Register a new account",
        button: [
            {
                text: "Sign Up",
                color: "green-500",
            }
        ]
    };

    const email = ref('');
    const password = ref('');
    const confirm = ref('');

    function signUp(email) {
        console.log(email)
    
    }
</script>

<template>
    <SignForm :props="props" @method="signUp"/>
</template>

SignForm.vue

<script setup>
    const email = ref('');
    const password = ref('');
    const { props } = defineProps(['props']);
</script>

<template>
    <div class="container">
        <form class="flex flex-col items-center gap-2">
            <p class="text-2xl pb-2">{{ props.header }}</p>
            <input v-for="(pf, index) in props.fields" :key="index"
            :type="[`${pf}` === 'confirm' ? 'password' : `${pf}`]"
            :placeholder="`${pf}`"
            :v-model="`${pf}`"
            class="bg-gray-300 rounded p-2 w-2/5"
            >
            <button type="submit" v-for="(btn, index) in props.button" :key="index"
                class="p-2 text-white rounded w-2/5 text-sm opacity-85 hover:font-bold hover:opacity-100"
                :class="`bg-${ btn.color }`"
                @click.prevent="$emit('method', email)"
                >
                <span>{{ btn.text }}</span>
            </button>
        </form>    
    </div>
</template>

邮箱值仍然没有按预期发出-要么为空,要么为undefined

更新日期 : https://play.vuejs.org/

推荐答案

上述代码的问题在于,您正在try 绑定字符串值,而不是react 式值,因为emailSignForm.vue中没有显示任何更改.

简单的解决方案是创建一个存储key: field namevalue: input value的react 对象.可以使用refreactive功能来完成

Vue Playground

SOLUTION

<script setup>
  import { ref } from 'vue'
  const form = ref({
    email: '',
    password: '',
  });
  const { props } = defineProps(['props']);
</script>

<template>
  <div>
    <form>
      <p>{{ props.header }}</p>
      <input v-for="pf in props.fields" 
      :type="`${pf}`"
      :placeholder="`${pf}`"
      v-model="form[pf]"
      >
      <button type="submit" v-for="btn in props.button" 
        @click.prevent="$emit('method', form.email)"
        >
        {{ btn.text }}
      </button>
    </form>    
  </div>
</template>

Vue.js相关问答推荐

如何使用Nuxt 3动态处理无限嵌套路由?

Vue路由路径匹配行为

为什么元素会从Vue 3中的TransitionGroup左上角出现?

如果单元格不可见,则以编程方式打开行的编辑模式. Ag 网格,Vue.js

在 dom 内移动 Vue 组件?

将props传递给设置时的VueJS 3组合API和TypeScript类型问题:类型上不存在属性 user用户

有条件地在 Vue 中添加一个 CSS 类

未捕获的类型错误:无法读取未定义的属性initializeApp

Laravel Vue - 如何让 vue 在公共文件夹中查找图像

Express.js + lint 出错

Vuetify - 如何保持第一个扩展面板默认打开,如果我打开另一个面板,其他面板应该关闭?

在textbox文本框 VueJS 2 中键入时搜索列表

W3C 验证和 Vue 的 HTML 绑定语法

如何将参数传递给使用 ...mapActions(...) 映射的函数?

包含 Vue 组件的 HTML 字符串的 Nuxt 渲染函数

阻止 Vue 积极重用 dom 元素

使用 nuxt,如何将路由名称放在页面标题中?

使用 vuejs 设置响应式屏幕宽度

Vuetify 过渡:如何设置过渡速度

Vue.js 中的条件 依赖于props值?