Issue #2690✓ SolvedOpened March 31, 2020by adamwpe12 reactions

Custom Component disappear on Save (Vue.js)

快速解答by adamwpe8

@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: Thanks for the help everyone!

Read full answer below ↓

Question

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

代码片段TEXT
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?

Answers (3)

👍 Most helpfuladamwpeApril 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!

pouyamiralayiApril 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!

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Free option

Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.

Browse free plugins →
Premium option

Premium plugins ship with support, regular updates, and production-ready features — save days of integration work.

Browse premium plugins →

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.