2017-01-19 1 views

答えて

8

あなたはレンダリング機能を使用する "ラッパーコンポーネント" を作成する必要があります。

import Vue from 'vue'; 

Vue.component('wrapper-component', { 
    name: 'wrapper-component', 
    render(createElement) { 
    return createElement(
     this.tag, // tag name 
     this.$slots.default // array of children 
    ); 
    }, 

    props: { 
    tag: { 
     type: String, 
     required: true, 
    }, 
    }, 
}); 

その後、

<wrapper-component tag="div"> 
    <span>All this will be rendered to inside a div</span> 
    <p> 
    All 
    </p> 
</wrapper-component> 

をたどり、それがより知っている程度の機能をレンダリングするには、この

<div> 
    <span data-v-4fcf631e="">All this will be rendered to inside a div</span> 
    <p data-v-4fcf631e=""> 
    All 
    </p> 
</div> 

にレンダリングする必要があるとして、他のテンプレートだけで使用して参照してください。official documentation

+1

レンダリング機能を使用する必要がないソリューションはありますか? – rayrutjes

14

ます次のように組み込みのcomponent要素を使用できます。

<component is="span" class="foo" style="color:red"> 
    nested stuff 
</component> 
+0

これは、コンポーネントの最初のロジックを持たないスパンでコンポーネントを置き換えます。 – rayrutjes

+1

@rayrutjes "初期ロジック"とは何ですか? OPは「小道具に基づいて動的タグを作成する」方法を尋ねましたが、これはまさにそれです。おそらくあなたは別の場合がありますか? – krukid

+1

また、 ''を使うことができます。これを行うと、親テンプレートの中にラッパータグ名をpropで渡すことができます。ソース: 'https:// vuejs.org/v2/guide/components.html#Dynamic-Components' – sandre89

関連する問題