javascript - Vue : Create a parent component that can render a child component

标签 javascript vue.js

如何创建可以接收和渲染子组件的父组件?

例如:

<Form> 
   <Input/>
   <Input/>
   <Select/>
   <Button/>
<Form/>

最佳答案

您可以使用 slots .

Parent.vue

<template>
 <form>

  <slot/> <!-- The child component will be rendered here -->

 </form>
</template>

Child.vue

<template>
 <div>

  <input/>
  <input/>

 </div>
</template>

祖 parent .vue

<template>
 <div>

  <Parent> <!-- Parent component -->
   <Child/> <!-- Pass child component to it as slot -->
  </Parent>

 </div>
</template>

<script>
import Parent from './Parent.vue'
import Child from './Child.vue'

components: {
 Parent,
 Child
}
</script>

关于javascript - Vue : Create a parent component that can render a child component,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61027940/

相关文章:

javascript - 两个玩家用 javascript 猜数字游戏

javascript - 从外部进入集合中的控件时如何淡入和淡出标签?

javascript - VueJs中如何通过路由名称获取特定路由数据?

vue.js - Apollo 客户端 - 错误 : Can't find field allLinks({}) on object (ROOT_QUERY)

javascript - 我试图在discord.js 中设置自动角色,但它不起作用

javascript - onblur() 不起作用,而 onclick() 工作正常

javascript - 递归防止函数被连续点击多次

javascript - 使用 Firebase 转换匿名用户 : Cannot read property 'getAuthResponse' of undefined

vue.js - vuetify数据表分页自定义函数

vue.js - 如何让我的基本网址在 Vue/Cli 中更改