2017-01-17 37 views

回答

1

你总是可以创建一个render函数。请参阅下面的代码。

Vue.component('anchored-heading', { 
    render: function (createElement) { 
    return createElement(
     'h' + this.level, // tag name 
     this.$slots.default // array of children 
    ) 
    }, 
    props: { 
    level: { 
     type: Number, 
     required: true 
    } 
    } 
}) 

这里createElement是一个函数,如下所示。

// @returns {VNode} 
createElement(
    // {String | Object | Function} 
    // An HTML tag name, component options, or function 
    // returning one of these. Required. 
    'div', 
    // {Object} 
    // A data object corresponding to the attributes 
    // you would use in a template. Optional. 
    { 
    // (see details in the next section below) 
    }, 
    // {String | Array} 
    // Children VNodes. Optional. 
    [ 
    createElement('h1', 'hello world'), 
    createElement(MyComponent, { 
     props: { 
     someProp: 'foo' 
     } 
    }), 
    'bar' 
    ] 
) 

欲了解更多信息,请参阅this post