我是VueJS的新手.在react中,您可以轻松使用rest参数向 children 传递props .Vue中有类似的模式吗?

考虑具有几个子组件的父组件:

<template>
<header class="primary-nav">
<search-bar :config="searchBar"><search-bar>
//          ^^^^ this becomes the key on the props for SearchBar
header>
</template>

export default {
  data(){
    return {
      ... a few components ...
      searchBar : {
        model: '',
        name: 'search-bar',
        placeholder: 'Search Away',
        required:true,
        another: true
        andanother: true,
        andonemoreanotherone:true,
        thiscouldbehundredsofprops:true
      }
    }
  }
}

<template>
    <div :class="name">
        <form action="/s" method="post">
            <input type="checkbox">
            <label :for="config.name" class="icon-search">{{config.label}}</label>
            <text-input :config="computedTextInputProps"></text-input>
                        //^^^^ and so on. I don't like this config-dot property structure.
        </form>
    </div>
</template>

  export default {
    props: {
        name: String,
        required: Boolean,
        placeholder: String,
        model: String,
    },
    computed: {
     computedtextInputProps(){
       return justThePropsNeededForTheTextInput
     }
    }
 ...  

我不喜欢的是props 是用config键或任何其他任意键命名的.文本输入组件(未显示)是一个美化的input字段,可以具有多个属性.我可以在创建组件时展平props ,但这通常是个好主意吗?

我很惊讶以前没人问过这个问题.

谢谢

Edit: 10-06-2017

相关:https://github.com/vuejs/vue/issues/4962

推荐答案

Parent component

<template>
  <div id="app">
    <child-component v-bind="propsToPass"></child-component>
  </div>
</template>

<script>
  import ChildComponent from './components/child-component/child-component'

  export default {
    name: 'app',
    components: {
      ChildComponent
    },
    data () {
      return {
        propsToPass: {
          name: 'John',
          last_name: 'Doe',
          age: '29',
        }
      }
    }
  }
</script>

Child Component

<template>
  <div>
    <p>I am {{name}} {{last_name}} and i am {{age}} old</p>
    <another-child v-bind="$props"></another-child> <!-- another child here and we pass all props -->
  </div>
</template>

<script>
  import AnotherChild from "../another-child/another-child";
  export default {
    components: {AnotherChild},
    props: ['name', 'last_name', 'age'],
  }
</script>

Another Child component inside the above component

<template>
    <h1>I am saying it again: I am {{name}} {{last_name}} and i am {{age}} old</h1>
</template>

<script>
    export default {
      props: ['name', 'last_name', 'age']
    }
</script>

Vue.js相关问答推荐

如何只提取一次用于无线电组过滤的数据,而不是针对数据库中的每个条目提取数据?

Vue Js Composition Api 未定义(读取名称)

如何通过 setup() 发出多个参数?

我可以在打印 HTML 页面时使用作用域 CSS 吗? (使用 Laravel 和 Vue.js)

Vuejs中的条件a href

Vuetify v-data-table ,如何在 HTML 中呈现标题文本?

Vuetify - 如何保持第一个扩展面板默认打开,如果我打开另一个面板,其他面板应该关闭?

在textbox文本框 VueJS 2 中键入时搜索列表

如何根据生产或开发模式读取不同的 .env 文件?

将数据传递给另一条路由上的组件

全局禁用 vuetify 组件的涟漪效应

Javascript/vue.js 接收json

Vue devServer 代理没有帮助,我仍然收到 CORS 错误

Nuxt 应用程序在刷新动态路由时返回 404(Tomcat 服务器)

错误:Node Sass 尚不支持您当前的环境:Linux 64-bit with Unsupported runtime (88)

加载时焦点文本框上的 Vue.js

使用props向组件添加类名

如何告诉 Vue-cli 我的应用程序的入口点在哪里?

如何使用 Vue Test utils 测试输入是否聚焦?

升级到 vue-cli 3 后,HTML 中的空格消失了