【发布时间】:2023-03-15 10:08:01
【问题描述】:
我有 FormField、Form 和 App。 App 包含 Form,Form 包含 FormField 和一个按钮。单击按钮时,我需要检查 Form 内的<input> 是否为空。我该怎么做?
https://codesandbox.io/s/dark-tdd-wd2nb?file=/src/components/Form.vue
FormField.vue
<template>
<input :placeholder= "placeholder"
/>
</template>
<script>
export default {
props: ['placeholder']
}
</script>
Form.vue
<template>
<form v-on:submit.prevent="execute">
<div >{{title}}</div>
<div v-if="wasError">{{errorMessage}}</div>
<FormField
v-for="(field) in fields"
v-bind:key="field"
:placeholder="field"
:name = "field"
/>
<button @click="handleClick">Button</button>
</form>
</template>
<script>
import FormField from "@/components/FormField";
export default {
components: { FormField},
props: {
wasError: {
type: Boolean,
required: false
},
errorMessage: {
type: String,
required: false
},
title: {
type: String,
required: true
},
fields: {
type: Array,
required: true
}
},
methods: {
handleClick: function(){
alert("hello");
}
}
}
</script>
App.vue
<template>
<img alt="Vue logo" src="./assets/logo.png" />
<Form title = "Sign Up"
:fields="['Username', 'Email', 'Password']"
:wasError = "errorStuff.wasError"
:errorMessage = "errorStuff.errorMessage"/>
</template>
<script>
import Form from "@/components/Form";
export default {
name: "App",
components: {
Form
},
data() {
return {
fields: [
'Username', 'Email', 'Password'
],
username: '',
email: '',
password: '',
errorStuff: {
wasError: false,
errorMessage: ''
}
}
},
};
</script>
【问题讨论】:
标签: javascript vue.js vuejs2