简体   繁体   中英

Convert Vue Component to HTML in advance

I need to create a vuejs component and pass it into a library (mapbox) as pure html. Mapbox has a setHtml method for popups that I'm trying to populate.

https://docs.mapbox.com/mapbox-gl-js/example/popup/

var popup = new mapboxgl.Popup({ closeOnClick: false })
.setLngLat([-96, 37.8])
.setHTML('<h1>Hello World!</h1>')
.addTo(map);

I haven't been able to find any way to pre-render a specific component into html that I could then insert into the mapbox call. Sort of like v-html in reverse.

set ref attribute for your component, and then you can get rendered HTML content of component by using this.$refs.ComponentRef.$el.outerHTML , and remember don't do this when created.

<template>
  <div class="app">
    <Hello ref="hello" />
  </div>
</template>

<script>
import Hello from './Hello.vue'
export default {
  name: 'App',
  components: {
    Hello,
  },
  created() {
    // wrong, $el is not exists then
    // console.log(this.$refs.hello.$el.outerHTML)
  },
  mounted() {
    console.log(this.$refs.hello.$el.outerHTML)
  },
}
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM