2017-06-14 68 views
1

我試圖從api數據呈現或加載組件。爲了解釋更多,我們假設我有測試組件,我直接將其注入到父組件中,它可以工作。但是當我試圖在數據庫中保存組件標記並運行ajax調用時,我的組件標記顯示但不起作用,或者更確切地說是加載/呈現。請幫忙。從我的APIVue.js - 從Ajax調用加載組件

返回:

{ 
    "_id": "59411b05015ec22b5bcf814b", 
    "createdAt": "2017-06-14T11:16:21.662Z", 
    "updatedAt": "2017-06-14T12:41:28.069Z", 
    "name": "Home", 
    "content": "<test-comp></test-comp>", 
    "slug": "/", 
    "navName": "Home", 
    "__v": 0, 
    "landing": true, 
    "published": false 
} 

我父組件:

<template> 
    <div> 
    <test-comp></test-comp> // This works 
    <div v-html="page.content"></div> // But this doesn't :(
    </div> 
</template> 

<script> 
    import { Api as defApi } from 'shared'; 
    import test from './testComp'; 

    export default { 
    data:() => ({ 
     page: {} 
    }), 
    created() { 
     defApi.get('api/pages/landing') 
     .then((res) => { 
     this.page = res.data.body; 
     }); 
    }, 
    components: { 
     testComp: test 
    } 
    }; 
</script> 

回答

4

只能在v-html標籤指定簡單的HTML。因此,在傳遞到v-html的字符串中添加組件標記將不起作用。

如果您只是試圖指定組件類型,則可以使用dynamic component。在你的情況,它可能看起來像這樣:

​​
+0

非常感謝..它確實像一個魅力工作。 –