cms icon indicating copy to clipboard operation
cms copied to clipboard

Appending a Vue component not working

Open duncanmcclean opened this issue 5 years ago • 2 comments

Bug Description

I'm prototyping out an addon. Part of this addon means adding a Vue component to every page in the Control Panel. I found the documentation around appending components. However, this does not seem to work and throws console errors.

After some digging, it turns out $root is null in here, not sure why but it is.

https://github.com/statamic/cms/blob/a074abbe6f6a234e5220889a7a8ea9df46656d6e/resources/js/components/Components.js#L17

I managed to find a workaround for now though - basically just doing what the helper does but on Statamic.app instead.

Statamic.$app.data.appendedComponents.push(new Component(`appended-zippy`, 'zippy', {}))

How to Reproduce

  1. Attempt to append a component to the CP
Statamic.$components.append('zippy', {
  props: {}
})
  1. Hook up your script in your service provider
  2. In the CP, open up the console and see red errors

Extra Detail

image

Environment

Statamic 3.0.49 Pro (pretty sure I'm on a way newer version than this but my sandbox site is all messed up)
Laravel 8.43.0
PHP 8.0.3
doublethreedigital/duplicator 1.3.2
doublethreedigital/static-cache-manager dev-master
doublethreedigital/zippy dev-master

duncanmcclean avatar May 26 '21 22:05 duncanmcclean

Hi Duncan,

I was running into the same kind of issue and noticed that I was appending the component at the wrong place. You'll have to append the component in the 'booted()' function.

import MyComponent from './components/MyComponent.vue'

Statamic.booting(() => {
    Statamic.$components.register('my-component', MyComponent);
})

Statamic.booted(() => {
    const component = Statamic.$root.$components.append('my-component', {
        props: { foo: 'bar' }
    });
})

Djoul avatar Dec 10 '21 04:12 Djoul

Closing as I believe @Djoul 's explanation is correct. Let us know otherwise, Duncan.

jasonvarga avatar Dec 10 '21 14:12 jasonvarga