首页 > 解决方案 > JSX Spread Attributes on Vue

问题描述

I'm trying to use JSX Spread Attributes on Vue component like below.

<script>

const
Square = p => <button class="square" v-on:click={ () => p.props.click( p.props.index ) }>{ p.props.squares[ p.props.index ] }</button>

const
Board = p => (
    <div>
        <Square squares={ p.props.squares } click={ p.props.click } index='0' />
        <Square { ...p.props } index='1' />
    </div>
)

export default {
    data    : () => ( { squares: [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ] } )
,   render  ( h )   { return <Board squares={ this.squares } click={ this.handleClick } /> }
,   methods : {
        handleClick: i => console.log( i )
    }
}
</script>

This line is OK:

<Square squares={ p.props.squares } click={ p.props.click } index='0' />

But this line seems to fail to pass properties from 'Board' to 'Square'

<Square { ...p.props } index='1' />

Please help me. Thanks in advance.

标签: javascriptvue.jsattributesjsxspread-syntax

解决方案


也许你可以这样做

<Square { ...{props: p.props} } index='1' />

阅读https://github.com/vuejs/jsx#attributesprops后,我认为它可以工作。


推荐阅读