Vue.js 达到子组件的子组件从父组件

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

Vue js reach child components child from parent

问题

我有3个组件:FormCardButton

首先,是我的 Button.vue 组件:

<template>
  <div>
    <slot v-bind="{ text }">
      <button>{{ text }}</button>
    </slot>
  </div>
</template>

<script setup>
const props = defineProps({
  text: {
    type: String,
    required: true
  }
});
</script>

这是我的 Card.vue 组件:

<template>
  <Button text="{ text }">
    <template #default="{ text }">
      <button>{{ text }}</button>
    </template>
  </Button>
</template>

<script setup>
import Button from './Button.vue'

const props = defineProps({
  text: {
    type: String,
    required: true
  }
});
</script>

最后,这是我的 Form.vue 组件:

<template>
  <div>
    <Card text="Some text">
      <template #default="{ text }">
      </template>
    </Card>
  </div>
</template>

<script setup>
import Card from './Card.vue'
</script>

我的问题是,我该如何将文本从 Form.vue 传递给 Button.vue 子组件?

英文:

I have 3 components: Form, Card and Button.

First, my Button.vue component:

<template>
  <div>
    <slot v-bind="{ text }">
      <button>{{ text }}</button>
    </slot>
  </div>
</template>

<script setup>
const props = defineProps({
  text: {
    type: String,
    required: true
  }
});
</script>

Here is my Card.vue component:

<template>
  <Button text="{ text }">
    <template #default="{ text }">
      <button>{{ text }}</button>
    </template>
  </Button>
</template>

<script setup>
import Button from './Button.vue'

const props = defineProps({
  text: {
    type: String,
    required: true
  }
});
</script>

And last, here is my Form.vue component:

<template>
  <div>
    <Card text="Some text">
      <template #default="{ text }">
      </template>
    </Card>
  </div>
</template>

<script setup>
import Card from './Card.vue'
</script>

My question is, how could I pass the text from the Form.vue to the Button.vue child component?

答案1

得分: 1

你的代码实际上是正确的。然而,**如果你想要将一个变量传递给组件的HTML属性,**而不是使用text=, 你应该使用:text=

而且,Card.vueButton.vue的props都需要一个字符串,而不是一个对象。所以,不要传递对象,而是按照以下方式传递特定的字符串::text="myVariableWithStringValue"

Card.vue

<template>
  <Button :text="buttonText"> <!-- 这里需要使用":text"而不是"text" -->
    <template #default="{ text }">
      <button>{{ text }}</button>
    </template>
  </Button>
</template>

<script setup>
import Button from './Button.vue'

const props = defineProps({
  // 我将你的prop从'props.text'重命名为'props.buttonText',这样我们就不会因为将一切都称为'text'而感到困惑
  buttonText: {
    type: String,
    required: true
  }
});
</script>

Form.vue

<template>
  <div>
    <!-- 我将你的prop从'props.text'重命名为'props.buttonText',这样我们就不会因为将一切都称为'text'而感到困惑 -->
    <!-- 如果你想要从变量中获取文本,可以使用 :buttonText="variableName" -->
    <!-- 现在,只需将 "Some text" 字符串作为 buttonText -->
    <Card buttonText="Some text"></Card>
    <!-- 你不能在<Card>中使用<template>,因为Card.vue的模板中没有定义<slot> -->
  </div>
</template>

<script setup>
import Card from './Card.vue'
</script>
英文:

Your code would actually be correct. However, if you want to pass a variable to the component in an HTML attribute, instead of using text=, you should use :text=.

And instead of passing the { } object, both the Card.vue and Button.vue props expect a String. So, instead of an object, pass the specific string as follows: :text=&quot;myVariableWithStringValue&quot;

Card.vue

&lt;template&gt;
  &lt;Button :text=&quot;buttonText&quot;&gt; &lt;!-- here need use &quot;:text&quot; instead of &quot;text&quot; --&gt;
    &lt;template #default=&quot;{ text }&quot;&gt;
      &lt;button&gt;{{ text }}&lt;/button&gt;
    &lt;/template&gt;
  &lt;/Button&gt;
&lt;/template&gt;

&lt;script setup&gt;
import Button from &#39;./Button.vue&#39;

const props = defineProps({
  // I renamed your prop from &#39;props.text&#39; to &#39;props.buttonText&#39; so that we don&#39;t get confused by calling everything &#39;text&#39;
  buttonText: {
    type: String,
    required: true
  }
});
&lt;/script&gt;

Form.vue

&lt;template&gt;
  &lt;div&gt;
    &lt;!-- I renamed your prop from &#39;props.text&#39; to &#39;props.buttonText&#39; so that we don&#39;t get confused by calling everything &#39;text&#39; --&gt; 
    &lt;!-- If you want give text from variable then can use :buttonText=&quot;variableName&quot; --&gt;
    &lt;!-- Now, just give to &quot;Some text&quot; string as buttonText --&gt;
    &lt;Card buttonText=&quot;Some text&quot;&gt;&lt;/Card&gt;
    &lt;!-- And you can&#39;t use &lt;template&gt; in &lt;Card&gt; because don&#39;t have got defined &lt;slot&gt; in Card.vue&#39;s template --&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;script setup&gt;
import Card from &#39;./Card.vue&#39;
&lt;/script&gt;

答案2

得分: 1

[Vue3 SFC 演练场][1]

似乎你在过度使用组件和插槽属性,你可以只使用具名插槽:

App.vue
```html
&lt;template&gt;
  &lt;div&gt;
    &lt;Card&gt;
      &lt;template #title&gt;卡片标题&lt;/template&gt;
      一些卡片内容
      &lt;template #button&gt;按钮文字&lt;/template&gt;
    &lt;/Card&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;script setup&gt;
import Card from &#39;./Card.vue&#39;

&lt;/script&gt;

Card.vue:

&lt;template&gt;
    &lt;h2&gt;&lt;slot name=&quot;title&quot;&gt;&lt;/slot&gt;&lt;/h2&gt;
    &lt;p&gt;
    &lt;slot&gt;&lt;/slot&gt;
    &lt;/p&gt;
    &lt;Button&gt;
        &lt;slot name=&quot;button&quot;&gt;&lt;/slot&gt;
    &lt;/Button&gt;
&lt;/template&gt;

&lt;script setup&gt;
import Button from &#39;./Button.vue&#39;
&lt;/script&gt;

Button.vue:

&lt;template&gt;
    &lt;div&gt;
      &lt;button&gt;&lt;slot&gt;&lt;/slot&gt;&lt;/button&gt;
    &lt;/div&gt;
&lt;/template&gt;

&lt;script setup&gt;
&lt;/script&gt;

<details>
<summary>英文:</summary>

[Vue3 SFC Playground][1]

Seems you are overusing component and slot props, you could use just named slots:

App.vue

<template>
<div>
<Card>
<template #title>Card title</template>
Some card content
<template #button>Some button text</template>
</Card>
</div>
</template>

<script setup>
import Card from './Card.vue'

</script>

Card.vue:

<template>
<h2><slot name="title"></slot></h2>
<p>
<slot></slot>
</p>
<Button>
<slot name="button"></slot>
</Button>
</template>

<script setup>
import Button from './Button.vue'
</script>

Button.vue:

<template>
<div>
<button><slot></slot></button>
</div>
</template>

<script setup>
</script>



  [1]: https://play.vuejs.org/#eNqNUstqwzAQ/JVFPeRSbOixqIK2n9CrL66jtCrWA2sdCiH/ntXTNvEhvnhXmp2dGXRh784151myV8ZRajf2KEVnAPhRnWNB5Wc/HXNNXYHBEyocpQi3EEvebijC92W1hCEgBmtQGtyh+Z4RrRERmmpA+Y93bLytQnib5G0w1PphUg7BS5wdnSjt7IQQFZ4mq+HQRI7g+BAH2jQh2DP7iKt3w9jEQU1WzP1oURBH+uXTIvYhhWsBRdne+t+XtA1Mr+Vbx2LeHVuWEyBDA20s1uqKqHqZ3FZLGV7ok5WFv4zXqceCT/Aa/ZIwhb92jp5ex0n9NH/eGrLPrje6k9v9

</details>



huangapple
  • 本文由 发表于 2023年7月7日 01:09:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631123.html
匿名

发表评论

匿名网友

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

确定