javascript - 第一次调用 Nuxt.js 时无法获得某些 $refs
问题描述
在对话框中创建表单的组件中,第一次显示表单时(即使设置或添加了 SSR),无法通过 $refs(不包含在对象中)获取输入。第一次显示时(无论是使用 SSR 设置还是使用 ),都无法通过 $refs 检索输入(它们不包含在对象中)。
第二次及以后显示对话框时,您将能够获得它们。(当对话框关闭时,您将能够将它们作为 refs 获取。
您不能使用 $emit ,因为您想执行子组件的验证过程并获得返回。我不能使用 $emit 因为我想执行子组件的验证过程并获取返回值。如果我可以通过引用传递父级中的 $emit 参数,我认为它会很好。
源代码
父组件
<base-dialog-with-close-button
ref="dialog"
max-width="900px"
:title="title"
>
<auto-create-inputs
:validate="validate"
@validate="v => this.valid = v"
v-model="data"
:inputs="inputs"
ref="inputs" // ← here
/>
<template v-slot:action>
<v-row dense justify="center">
<save-button
:form_data="data"
url="/api/company"
:valid="valid"
@validate="$refs.inputs.validate()"
@saved="v => saved(v)"
>Save</save-button>
<button-grey class="ml-2" @click="hide()">Cancel</button-grey>
</v-row>
</template>
</base-dialog-with-close-button>
子组件
<template>
<v-form ref="form" v-model="valid" lazy-validation>
<v-flex>
<v-row dense >
<v-col
v-for="(input,key) in inputs"
:cols="input.col?input.col:'12'"
:key="key"
class="form_col"
>
<v-row dense justify="start">
<v-col>
<v-subheader class="form_label">
<span class="label">
{{ input.label }}
</span>
<span v-if="input.required" class="required_label" style="text-align: end">
※Required
</span>
</v-subheader>
</v-col>
<v-col class="input_col">
<component
:value="getValue(input.value,data)"
@input="setValue"
:is="getInputComponent(input)"
:items="input.items"
:prepend-inner-icon="input.prepend_inner_icon"
:rules="getRules(input)"
:disabled="input.disabled"
:clearable="input.clearable"
:hint="input.hint"
:persistent-hint="input.persistent_hint"
:url="input.url"
:type="input.val_type"
@address="v => setAddr(v,input.addr_col)"
/>
<p class="outer_suffix" v-if="input.outer_suffix">
{{ input.outer_suffix }}
</p>
</v-col>
</v-row>
</v-col>
</v-row>
</v-flex>
</v-form>
</template>
解决方案
推荐阅读
- java - Set.of() 在 Intellij 中无法识别,但在 Android Studio 中无法识别
- python - 如何将 python 版本从 3.8 降级到 3.7 (mac)
- java - org.springframework.beans.factory.BeanCreationException:创建名为“projectTaskRepository”的bean时出错
- python - Python mypy 静态类型:仅接受 Dict(带键 str)、Lists(但不包括自定义类)和嵌套 Dicts 和 Lists 的参数
- gunicorn - 在 ubuntu 服务器上使用 uvicorn 运行 fastapi 应用程序
- php - 将数组传递给 Vue JS
- c - SDL2 > PNG IMAGE > 无法加载图像
- bash - 在 for 循环 bash 中使用 yq
- node.js - 如何在不使用 OFFSET 的情况下从特定文档编号开始 Firestore 查询?
- c++ - 如何找到锁定 Linux futex 的 C++ 行?