Mwh*_*r91 6 vue.js vue-component vuejs3
我对 Vue 比较陌生,正在开发我的第一个项目。我正在努力创建一个包含多个子组件和孙组件的表单。我遇到了一个问题,我需要能够生成表单的多个副本。因此,我将一些数据属性上移了 1 级。目前,形式为ApplicationPage.Vue > TheApplication.Vue > PersonalInformation.Vue > BaseInput.Vue。我的问题是我需要通过 TheApplication 发出从 PersonalInformation 到 ApplicationPage 的更改。我很难弄清楚如何处理这种情况。我一直在寻找 Vue2 的解决方案,但没有找到 Vue3 的解决方案。
应用程序页面.vue
template
<TheApplication :petOptions="petOptions"
:stateOptions='stateOptions'
v-model="data.primary"
applicant="Primary"/>
script
data() {
return {
data: {
primary: {
personalInformation: {
first_name: "",
middle_name: "",
last_name: "",
date_of_birth: "",
phone: null,
email: "",
pets: "",
driver_license: null,
driver_license_state: "",
number_of_pets: null,
additional_comments: ""
},
},
},
}
},
Run Code Online (Sandbox Code Playgroud)
应用程序.Vue
<personal-information :petOptions="petOptions"
:stateOptions='stateOptions'
:personalInformation="modelValue.personalInformation"
@updateField="UpdateField"
/>
Run Code Online (Sandbox Code Playgroud)
methods: {
UpdateField(field, value) {
this.$emit('update:modelValue', {...this.modelValue, [field]: value})
},
Run Code Online (Sandbox Code Playgroud)
个人信息.vue
<base-input :value="personalInformation.first_name"
@change="onInput('personalInformation.first_name', $event.target.value)"
label="First Name*"
type="text" class=""
required/>
Run Code Online (Sandbox Code Playgroud)
methods: {
onInput(field, value) {
console.log(field + " " + value)
// this.$emit('updateField', { ...this.personalInformation, [field]: value })
this.$emit('updateField', field, value)
},
}
Run Code Online (Sandbox Code Playgroud)
小智 5
对于任何不想链接事件发出的人,子对象上都有父对象,它也可用于发出事件。请务必在父级中注册发射,以避免控制台中出现警告。
给这里的直系父母打电话$emit。
儿童.vue
<input @input="$parent.$emit('custom-event', e.target.value) />
Run Code Online (Sandbox Code Playgroud)
或者使用方法:
<input @input="handleInput" />
Run Code Online (Sandbox Code Playgroud)
export default {
methods: {
handleInput(e) {
this.$parent.$emit('custom-event', e.target.value)
}
}
}
Run Code Online (Sandbox Code Playgroud)
由于是父级向祖先发出信号,因此请在此处声明发射。对于<script setup>,只需使用该defineEmits()方法来声明发射即可。请参阅文档。
家长.vue
<Child /> <!-- No need to listen to the event here -->
Run Code Online (Sandbox Code Playgroud)
export default {
emits: ['custom-event'] // Register the emits
}
Run Code Online (Sandbox Code Playgroud)
如果使用<script setup>
<script setup>
defineEmits(['custom-event']) // Register the emits
</script>
Run Code Online (Sandbox Code Playgroud)
然后监听祖父母组件中的事件。
祖父母.vue
<Parent @custom-event="doSomething()" /> <!-- The event is being listened to in the grandparent component -->
Run Code Online (Sandbox Code Playgroud)
这就是我要做的:codesandbox
发射只接受两个参数,发射的名称和发射的值。如果发出多个值,则必须将这些值作为单个对象发出。在我的解决方案中,孙组件将字段名称和值作为单个对象发出
孙子
<input
:value="personalInformation.first_name"
@input="onInput('first_name', $event.target.value)"
...
>
Run Code Online (Sandbox Code Playgroud)
onInput(field, value) {
this.$emit("update-field", { field: field, value: value });
},
Run Code Online (Sandbox Code Playgroud)
子对象捕获并重新发出,但首先注意以父组件期望的格式发出(它期望整个data.primary对象,因为这就是设置为 v-model 的内容)
孩子
<grandchild
:personalInformation="modelValue.personalInformation"
@updateField="UpdateField"
/>
Run Code Online (Sandbox Code Playgroud)
UpdateField({ field, value }) {
const newVal = this.modelValue;
newVal.personalInformation[field] = value;
this.$emit("update:modelValue", newVal);
}
Run Code Online (Sandbox Code Playgroud)
然后父组件自动接收并更新 v-modeldata.primary对象。
另外,我必须提到,您始终可以使用Pinia,Vue 的官方状态管理库(在一个组件中保存一些状态,从任何其他组件读取相同的状态),而不是处理多个级别组件之间的任何发出/传递对象。成分)。当然有一个学习曲线,但它绝对值得学习,并且正是为了简化此类情况而设计的。
| 归档时间: |
|
| 查看次数: |
6913 次 |
| 最近记录: |