VueJS将所有道具传递给子组件

Sim*_*mon 6 javascript rest vue.js

我对VueJS很新.在反应中,您可以轻松使用休息参数将道具传递给儿童.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
     }
    }
 ...  
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢.

编辑:10-06-2017

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

rol*_*oli 24

父组件

<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>
Run Code Online (Sandbox Code Playgroud)

子组件

<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>
Run Code Online (Sandbox Code Playgroud)

上面组件中的另一个Child组件

<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>
Run Code Online (Sandbox Code Playgroud)

  • 在vue 2.2。+中应该是`v-bind =“ $ attrs”`。 (2认同)
  • 不,$ attrs表示所有未声明为prop的传递属性。 (2认同)

Jay*_*ake 15

在 中Vue 2.6+,除了属性传递之外,还可以将事件/监听器传递给子组件。

父组件

<template>
    <div>
       <Button @click="onClick">Click here</Button>
    </div>
</template>

<script>
import Button from "./Button.vue";
export default {
    methods:{
        onClick(evt) {
            // handle click event here
        }
    }
}
</script>
Run Code Online (Sandbox Code Playgroud)

子组件

Button.vue

<template>
    <button v-bind="$attrs" v-on="$listeners">
        <slot />
    </button>
</template>
Run Code Online (Sandbox Code Playgroud)

更新[2021-08-14]:

在 Vue 3 中,$listeners将被删除。$attrs将有侦听器和属性传递给子组件。

子组件

Button.vue

<template>
    <button v-bind="$attrs">
        <slot />
    </button>
</template>
Run Code Online (Sandbox Code Playgroud)

迁移指南


小智 6

父组件

您可以将任意数量的道具传递给子组件

在此处输入图片说明

子组件

对所有道具满意后,便可以v-bind="$props"在子组件内部使用以检索所有道具。

在此处输入图片说明

最后结果:

在此处输入图片说明

完成:)

  • 在vue 2.2。+中应该是`v-bind =“ $ attrs”`。 (7认同)