2017-06-14 42 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

非常感谢..它确实像一个魅力工作。 –