0
呈现自定义标签,我有以下组成部分MyComponent.vue:从REST API响应
<template>
<div v-html="content"></div>
</template>
<script>
import Vue from 'vue'
import CustomComponent from 'CustomComponent.vue'
Vue.use(CustomComponent)
export default {
name: `my-component`,
computed: {
content() {
return this.$store.state.content
}
},
asyncData({store, route: {params: {id}}}) {
// here I fetch 'content' from REST API
return store.dispatch('FETCH_CONTENT', {id})
},
// ...
}
</script>
哪里this.$store.state.content
是HTML的字符串,我从REST API得到。例如:
<custom-component data-count="1"></custom-component><p>some text</p>
custom-component
标签没有在5月的情况下呈现。
是否有可能从我从REST API获得的html-string呈现vue组件,如果我在v-html
中使用此字符串?
见https://vuejs.org/v2/guide/components.html#Async - 组件 – Phil
@Phil添加{'custom-component':()=> import('CustomComponent.vue')}并不能解决问题。我认为它与我在v-html中使用内容相关 –
问题在于你试图绕过自定义组件的编译和挂载步骤,这会导致可变性和遵守性方面的更大问题。重新设计系统会更好,以便REST API返回纯数据,并且组件将适当地渲染它, –