2

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.

1

1 Answer 1

4

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! You pointed me in the right direction. I wound up doing a Vue.extend to create the element since it was going to be created repeatedly with different props. ` const HelloCtor = Vue.extend(Hello); const vm = new HelloCtor({ propsData: { featuredImage, summary, title, url, } }).$mount('#mount');` followed by the outerHTML Still running into a few NUXT related issues but the Vue element is mounting correctly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.