previewjs icon indicating copy to clipboard operation
previewjs copied to clipboard

Add global plugin to preview's app

Open davidmeirlevy opened this issue 3 years ago • 3 comments

When my component is using a global function that should exist at the root level of the app (for example - $t('my-text')), the preview doesn't work.

Describe the solution you'd like A clear and concise description of what you want to happen. A way to inject global plugins or to manage the preview's Vue App easily.

Describe alternatives you've considered Another tab, besides "properties" and "console", that will allow me to edit the preview's app directly. this configuration should be shared across components of the same project (up to the nearest package.json, in case the root folder is a monorepo).

Additional context Add any other context or screenshots about the feature request here. image

davidmeirlevy avatar Jan 17 '22 08:01 davidmeirlevy

Good idea! Thanks for the suggestion 😁

fwouts avatar Jan 21 '22 01:01 fwouts

I managed to workaround this using the wrapper component. In vue, I injected plugins using the component's context, like:

const {app} = getCurrentInstance() app.use(mockRouter)

davidmeirlevy avatar Jan 21 '22 07:01 davidmeirlevy

For Vue2 you can also create the __previewjs__/Wrapper.vue with something like the following content:

<template>
  <div class="wrapped">
    <slot />
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import BootstrapVue from 'bootstrap-vue'
Vue.use(BootstrapVue)

import i18n from '@/i18n'
export default Vue.extend({
  i18n,
})
</script>

<style lang="sass">
@import '../node_modules/bootstrap/scss/bootstrap'
@import '../node_modules/bootstrap-vue/src/index.scss'
</style>

mheers avatar Apr 10 '22 07:04 mheers