[英]How to get the compiled html content of a component in vuejs
我有一個像這樣的組件<AgentCard :agent="agent" />
,其中agent
是一個具有FirstName, LastName, Recent Orders
等屬性的對象......
現在我需要在 Google Maps InfoBox 中顯示這個組件。 infoWindow.setContent()
方法(用於顯示彈出信息窗口的 Google Maps API )僅接受 HTML 字符串,因此我嘗試手動渲染<AgentCard>
,獲取渲染組件的 HTML 內容並將其傳遞到setContent()
方法。
我試過Vue.compile('<AgentCard :agent="agent" />').render()
,但這不起作用。 如何手動渲染 Vue 組件並獲取其 HTML 內容?
一種解決方案是創建一個僅包含目標組件的新 Vue 實例, $mount
它,然后獲取其$el
(根元素)的outerHTML
:
<script src="https://unpkg.com/vue@2.6.12/dist/vue.min.js"></script> <script> Vue.component('AgentCard', { template: `<div>{{agent.name}}</div>`, props: { agent: Object } }) const app = new Vue({ template: `<AgentCard :agent="agent" />`, data: () => ({ agent: { name: 'john doe' } }) }).$mount() console.log(app.$el.outerHTML) </script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.