vue wrap another component, passing props and events

后端 未结 1 1724
野趣味
野趣味 2020-12-15 07:14

How can I write my component to wrap another vue component, while my wrapper component get some extra props? My wrapper template component should be:



        
1条回答
  •  死守一世寂寞
    2020-12-15 07:25

    Place the component you wish to wrap into the template of the wrapper component, add v-bind="$attrs" v-on="$listeners" to that component tag, then add the inner component (and, optionally, inheritAttrs: false) to the wrapper component's config object.

    Vue's documentation doesn't seem to cover this in a guide or anything, but docs for $attrs, $listeners, and inheritAttrs can be found in Vue's API documentation. Also, a term that may help you when searching for this topic in the future is "Higher-Order Component" (HOC) - which is basically the same as your use of "wrapper component". (This term is how I originally found $attrs)

    For example...

    
    
    
    
    

    Edit: Alternatively, you may want to use dynamic components via the is attribute so you can pass in the component to be wrapped as a prop (closer to the higher-order component idea) instead of it always being the same inner component. For example:

    
    
    
    
    

    Edit 2: The part of OP's original question that I missed was passing all props EXCEPT one or two. This is handled by explicitly defining the prop on the wrapper. To quote the documentation for $attrs:

    Contains parent-scope attribute bindings (except for class and style) that are not recognized (and extracted) as props

    For example, example1 is recognized and extracted as a prop in the snippet below, so it doesn't get included as part of the $attrs being passed down.

    Vue.component('wrapper-component', { 
      template: `
        
    `, // NOTE: "example1" is explicitly defined on wrapper, not passed down to nested component via $attrs props: ['wraps', 'example1'] }) Vue.component('posts', { template: `
    Posts component
    `, props: ['example1', 'example2', 'example3'], }) new Vue({ el: '#app', template: ` `, })
    
    

    0 讨论(0)
提交回复
热议问题