Vue如何将v-model绑定到子组件?

huangapple go评论46阅读模式
英文:

How is vue attaching v-model to child component?

问题

我目前在我的项目中使用 vuetify 3,我将vuetify的v-text-field分离到一个不同的组件中(Input.vue)。代码功能完全正常,但我对v-model如何连接到Input.vue中的v-text-field感到困惑。我们将v-model作为prop传递给Input组件,但我们没有以任何方式将v-model连接到text-field。

这是vuetify库还是vue本身的行为?通常,我会使用provide/inject将我的ref状态传递给子组件,但在这里我无法弄清楚如何做到这一点。

Create.vue

<script>
//表单提交请求
const form = reactive({
'name': '',
'teachers': [],
'subcategories': [],
'tags': [],
'users': [],
'fully_public': false,
'partial_public': false,
'counter': 0,
'min_time':
'publish': 0,
'publish_date':
'reattempt': 0,
'total_reattempt': ''
'image': '',
'description': **
});
</script>
<template>
<Input 
v-model="form.min_time" 
label="输入最短时间 *"
:disabled="form.counter == 0"
:error-messages="v$.min_time.$errors[0]?.$message || errors.min_time" />
</template>

Input.vue

<script setup>
const props = defineProps({
label: String,
name: String,
});
</script>
<template>
<v-text-field label="label" variant="outlined"></v-text-field>
</template>
英文:

I'm currently using vuetify 3 in my project, I separated the vuetify v-text-field into a different component (Input.vue). The code functions completely well, but I'm confused on how the v-model is being connected to the v-text-field in Input.vue. We are passing the v-model as prop in Input component but we aren't connecting the v-model to the text-field in any way.

Is this behavior of vuetify library or vue itself? I normally pass my ref states to child component using provide/injects, but here I can't figure out how its possible.

Create.vue

&lt;script&gt;
//form submit request
const form = reactive({
&#39;name&#39;: &#39;&#39;,
&#39;teachers&#39;: [],
&#39;subcategories&#39;: [],
&#39;tags&#39;: [],
&#39;users&#39;: [],
&#39;fully_public&#39;: false,
&#39;partial_public&#39;: false,
&#39;counter&#39;: 0,
&#39;min_time&#39;:
&#39;publish&#39;: 0,
&#39;publish_date&#39;:
&#39;reattempt&#39;: 0,
&#39;total_reattempt&#39;: &#39;&#39;
&#39;image&#39;: &#39;&#39;,
&#39;description&#39;: **
});
&lt;/script&gt;
&lt;template&gt;
    &lt;Input 
    v-model=&quot;form.min_time&quot; 
    label=&quot;Enter minimum time *&quot; 
    :disabled=&quot;form.counter == 0&quot;
    :error-messages=&quot;v$.min_time.$errors[0]?.$message || errors.min_time&quot; /&gt;
&lt;/template&gt;

Input.vue

&lt;script setup&gt;
    const props = defineProps({
        label: String,
        name: String,
    });
&lt;/script&gt;
&lt;template&gt;
    &lt;v-text-field label=&quot;label&quot; variant=&quot;outlined&quot;&gt;&lt;/v-text-field&gt;
&lt;/template&gt;

答案1

得分: 0

vue中,未声明的属性会自动传递给子元素的根元素。

通过使用 Fallthrough 属性

英文:

> In vue undeclared attributes are automatically passed onto the root element of the child.

By Using Fallthrough Attributes

huangapple
  • 本文由 发表于 2023年5月29日 01:42:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352782.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定