Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vue 3 support #16

Open
harry-73 opened this issue Sep 7, 2022 · 1 comment
Open

Vue 3 support #16

harry-73 opened this issue Sep 7, 2022 · 1 comment

Comments

@harry-73
Copy link

harry-73 commented Sep 7, 2022

Hi,

Is there any plan to support Vue 3?

Thanks.

@Akryum
Copy link

Akryum commented Sep 7, 2022

I'm currently using this (typescript):

import { defineComponent, h, onMounted, onUnmounted, ref, watch } from 'vue'
import { ReactiveVar } from 'meteor/reactive-var'
import { Blaze } from 'meteor/blaze'

export const VBlaze = defineComponent({
  props: {
    template: {
      type: String,
      required: true,
    },

    data: {
      type: [Object, Function],
      default: () => ({}),
    },
  },

  setup (props) {
    const el = ref<HTMLDivElement>()
    let blazeView: Blaze.View

    const meteorData = new ReactiveVar(props.data)
    watch(() => props.data, (data) => {
      meteorData.set(data)
    })

    function createView () {
      // @ts-expect-error private api
      const template = Blaze._getTemplate(props.template, null)
      if (!template) {
        throw new Error(`Blaze template '${props.template}' not found.`)
      }
      destroyView()
      blazeView = Blaze.renderWithData(template, () => meteorData.get(), el.value!)
    }

    function destroyView () {
      if (blazeView) {
        Blaze.remove(blazeView)
      }
    }

    onMounted(() => {
      createView()

      watch(() => props.template, () => {
        createView()
      })
    })

    onUnmounted(() => {
      destroyView()
    })

    return {
      el,
    }
  },

  render () {
    return h('div', {
      ref: 'el',
    })
  },
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants