在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ vue如何動態(tài)的引入組件

vue如何動態(tài)的引入組件

我想實現(xiàn)通過后臺數(shù)據(jù)來引用不同的組件,比如我拿到了id為1的數(shù)據(jù),我就想引用我寫好的1.vue文件,因為有可能有上千個這種文件,我需要后臺的數(shù)據(jù)來定位是哪一個文件,請問這種該怎么解決呀

回答
編輯回答
嫑吢丕

<component :is="你的組件名"></component>

2018年8月21日 21:12
編輯回答
硬扛

這個是我的解決方案
不知道是不是比較通用或者正確的解決方案, 用render函數(shù)去判斷要渲染哪個標(biāo)簽

//run.vue
// homeComponents 是異步組件列表.
    import homeComponents from '@/assets/js/homeComponents';

    export default {
        render: function(cElement, context) {
            return cElement(homeComponents[this.temp.type], {
                props: {
                    temp: this.temp,// 組件數(shù)據(jù)
                }
            })
        },
        props: {
            temp: {
                type: Object,
                required: true,
                default: {}
            },
        }

// home.vue
// 只要注冊這個 run 組件, 然后傳入 temp , 根據(jù)temp.type, 去渲染相應(yīng)的 component
// 這樣你只要在頁面上寫一個run組件, 傳入 type, 就會異步加載你需要的組件
<template>
    <run v-for="node in element" :temp="node" :key="node.fieldId" />
</template>

其實就是實現(xiàn)了官網(wǎng)里的 component 標(biāo)簽的功能. 感覺這樣更靈活吧

2017年3月18日 13:26
編輯回答
瘋浪

也可以這樣,先引入組件,用v-if,顯示滿足條件的組件,當(dāng)然如果組件多的話,會麻煩些

2018年3月3日 11:39
編輯回答
你好胸

同一個模板的話,直接改變參數(shù)就行了。不同模板的話,不可能有成千上萬個,你說的因該叫路由了

2017年3月10日 04:11
編輯回答
硬扛

提供一種方法或者思路

1.首先用template或者script標(biāo)簽封裝模板
<template id="com1">
    <div>組件1</div>
</template>
<template id="com2">
    <div>組件2</div>
</template>
2.構(gòu)建組件
var com1 = Vue.extend({
    template:'#com1'
});
var com2 = Vue.extend({
    template:'#com2'
});
3.注冊組件
components:{com1,com2}
4.根據(jù)數(shù)據(jù)創(chuàng)建一個組件列表
/*請求數(shù)據(jù)回調(diào)函數(shù)*/
var _this = this
function(res){
    if(res.id == 1){
        _this.comList.push('com1')
    }
    if(res.id == 2){
         _this.comList.push('com2')
    }
}
5.動態(tài)引入組件
<div v-for="d in comList">
    <component :is="d"></component>
</div>
2017年5月5日 16:28