question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTIONS] Custom Component disappear on Save (Vue.js)

See original GitHub issue

Hi All,

I’m trying to test adding some custom Vue.js components to the block manager, and at first appearance, all is well - it renders correctly and vue components are responsive.

But if I save the template, and reload my page, and supply the storageManager with ‘gjs-style’, ‘gjs-components’, ‘gjs-html’, ‘gjs-css’.

The custom component no longer renders. It appears the the storageManager removes any script tags??? As if I check my REST GET request, ‘gjs-html’ does contain the <script> tag.

Before Saving:

before

After Reloading Loading: after

Not too sure something is wrong with my component:

`
editor.BlockManager.add(“vueApp”, { label: “Vue App”, category: ‘Basic’, id: “default-vue-app”, content: { script: function() {

                const app1El = document.createElement("div");
                app1El.id = 'app';

                const app1Script = document.createElement("script");
                app1Script.type = "text/javascript";
                app1Script.src = "http://[::1]/assets/vue/vueApp.js";

                this.appendChild(app1El);
                this.appendChild(app1Script);
            },
            traits: [
                {
                    type: 'text',
                    label: 'Nice',
                    name: 'custom_att',
                    value: 'Who knows',
                }
            ]
        }
    });

`

Have anyone tried using Vue.js components?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
adamwpecommented, Apr 2, 2020

@pouyamiralayi

Thanks for that, pointed me in the right direction - so he is an example that works for me for rending a Vue Component and it will load back in 😃, but sure may be useful for someone else:


     editor.DomComponents.addType('vue_example', {
            model: {
                init() {

                    if (typeof this.getAttributes()['custom_att'] === "undefined")
                        this.addAttributes({'custom_att': 'default'});

                    // Onload, get the latest links
                    if (this.getTrait('custom_att') === undefined)
                        this.getTrait('custom_att').set('default');

                    this.getTrait('custom_att').set('default');

                    this.listenTo(this, 'change:attributes', this.reRender);

                },
                removed() {

                },
                defaults: {
                    removable: true,
                    draggable: true,
                    droppable: true,
                    badgable: true,
                    stylable: true,
                    highlightable: true,
                    copyable: false,
                    resizable: false,
                    editable: false,
                    hoverable: true,
                    traits: [
                        {
                            type: 'text',
                            label: 'Nice',
                            name: 'custom_att'
                        }
                    ],
                    script: function(){

                        const app1El = document.createElement("div");
                        app1El.id = 'app';

                        const app1Script = document.createElement("script");
                        app1Script.type = "text/javascript";
                        app1Script.src = "http://[::1]/assets/vue/vueApp.js";

                        this.appendChild(app1El);
                        this.appendChild(app1Script);

                    }
                },
                reRender() {
                    this.view.render();
                }
            },
            isComponent: function (el) {

                if (el.tagName === 'vue_example')
                    return {type: 'vue_example'};
            }

        });

        editor.BlockManager.add('vue_example', {
            id: 'vue_example',
            label: 'Vue Test',
            category: 'Vue',
            content: {
                tagName: 'vue_test',
                type: 'vue_example',
                editable: false
            }
        });

Thanks for the help everyone!

2reactions
pouyamiralayicommented, Apr 2, 2020

@adamwpe we had something like this in the past i apologize for not seeing it through! go like this:

editor.DomComponents.addType('your-type', {
    model:{
        isComponent: el => {},
        defaults:{
            script:function(){},
        },
    }
})

there is no need to extend from dView & dModel in the latest versions. and for the vue js integration please notice that the internal structure of your vue component would not be detected by grapesjs because there is no model generated for it! if you had any chance to overcome this scenario please let us know! Cheers!

Read more comments on GitHub >

github_iconTop Results From Across the Web

When a new component is inserted all the rest disappears ...
This time I'm building a small project and I'm incluing Vue with a script tag. But I don't understand the following behavior. //...
Read more >
How To Build a Modal Component with Vue.js - DigitalOcean
In this article you will learn how to create a reusable modal component using transitions and slots in Vue.js.
Read more >
How to close model from a button using vue.js - GeeksforGeeks
To close a modal using vue.js you can use the click event i.e. ... In the component where modal is used add close...
Read more >
Transition | Vue.js
When an element in a <Transition> component is inserted or removed, this is what happens: Vue will automatically sniff whether the target element...
Read more >
Focus management with Vue refs - Learn web development
Similarly, if you save or cancel your edit, focus will disappear again as ... For custom Vue components, you can also use refs...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found