我创建了一个简单的页面构建器,遵循GrapeJS,它运行良好. 我认为需要有两个函数来保存和加载生成的页面. 我该怎么办?

const editor = grapesjs.init({
  // Indicate where to init the editor. You can also pass an HTMLElement
  container: '#gjs',
  // Get the content for the canvas directly from the element
  // As an alternative we could use: `components: '<h1>Hello World Component!</h1>'`,
  fromElement: true,
  // Size of the editor
  height: '300px',
  width: 'auto',
  // Disable the storage manager for the moment
  storageManager: false,
  // Avoid any default panel
  panels: { defaults: [] },
  blockManager: { },
  layerManager: { },
  deviceManager: { },
  selectorManager: { },
  styleManager: { },
  storageManager: { }
});

推荐答案

  1. 保存模板的当前阶段

             let components = editor.getComponents();
             let style = editor.getStyle()
             let templateData = {
                 components: components,
                 style: style
             };
             localStorage.setItem("template", JSON.stringify(templateData));
    
  2. 加载保存的数据

             let templateData = JSON.parse(localStorage.getItem("template"));
             editor.setComponents(templateData.components);
             editor.setStyle(templateData.style);
    

您可以保存在数据库中,而不是localStorage.这对我有效!

Javascript相关问答推荐

如何避免使用ajax在Vue 3合成API中重定向

如何编辑代码FlipDown.js倒计时?

react 路由加载程序行为

vanillajs-datepicker未设置输入值,日期单击时未触发更改事件

为什么我的includes声明需要整个字符串?

Plotly热图:在矩形上zoom 后将zoom 区域居中

从WooCommerce Checkout Country字段重新排序国家,保持国家同步

react—router v6:路由没有路径

Promise Chain中的第二个useState不更新

将核心模块导入另一个组件模块时存在多个主题

使用Google API无法进行Web抓取

用于在路径之间移动图像的查询

使用VUE和SCSS的数字滚动动画(&;内容生成)

如何在.NET Core中将chtml文件链接到Java脚本文件?

如何将zoom 变换应用到我的d3力有向图?

如何使用<;Link>;执行与JS Reaction中的";window.Location=/GameOverDied;";类似的操作?

无法设置RazorPay订阅API项目价格

在单击按钮时生成多个表单时的处理状态

重新渲染过多(&Q).REACT限制渲染次数以防止无限循环.使用REACT下拉菜单时

如果查询为空,则MongoDB将所有文档与$in匹配