Custom Component disappear on Save (Vue.js)
@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:
After Reloading Loading:
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)
@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!
Hi @adamwpe !
Script tag importing is disabled by the default editor configuration, which explains why your scripts tags are stripped out by the HTML Parser, setting the allowScripts editor configuration option to true should solve the issue, eg:
grapesjs.init({
// [...]
allowScripts: true,
// [...]
});
Hope this helps :)
@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.
Issue #3213
How to save the dynamically added traits
Hi, @artf, I have a custom component that has a button trait. When the button is pressed, I am adding a custom trait dynamically using comp...
Issue #3713
Stored components with script can not be rendered properly
I am facing some problem when try to save some components which includes some scripts and reload the page the components are not rendered p...
Issue #1875
[QUESTION] ID changes when importing html or passing html in components prop in config.
Hi, I built a component and block that I can drag and drop in the canvas. My issue is that some styles rely on the ID. I know this might no...
Issue #1906
Components & Js - Load Template
Hey, I have a component which has a JS attached to it. Everything is created using the JS. When I drag and drop my block for the first time...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.
Browse free plugins →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.