如何聆听组件在组件中的变化。
我也没能让这个方法正常工作。设置 listTo 时错误读取 components()。初始化时标签是空的(里面没有内容),所以这很合理。 算了......我是在模型的初始化中操作,而不是视图。 “打字稿” init() { const children = this.model.components(); this.listenTo(children, 'add remove', this.handleComponentsChange); }, ``` 在我把它放到正确的初始值:)后,这个方法就成功了
阅读下方完整回答 ↓问题
init() { this.listenTo(this.model,'change:components', this.handleComponentsChange); },
我开发一个组件,尝试监听子组件的变化,然后添加上面的代码以查看初始化,但添加/移除组件时函数没有触发。
我该如何在组件视图中监听组件的变化?
谢谢
回答(4)
我也没能让这个方法正常工作。设置 listTo 时错误读取 components()。初始化时标签是空的(里面没有内容),所以这很合理。
算了......我是在模型的初始化中操作,而不是视图。 “打字稿” init() { const children = this.model.components(); this.listenTo(children, 'add remove', this.handleComponentsChange); },
在我把它放到正确的初始值:)后,这个方法就成功了
组件收集不会改变,这也是为什么没有触发事件的原因。
存储后端是一个骨干集合,详见[这里](https://backbonejs.org/#Collection)。你必须听它的事件,特别是“添加”和“删除”。
正如Andreas已经提到的,你必须为收藏添加一个听众。 “''js init() { const children = this.model.components(); this.listenTo(children, 'add remove', this.handleComponentsChange); },
谢谢你举报,@jcsofts。 关于如何监听组件变化的好问题。推荐的组件方法是使用事件驱动的API。 从这里开始: 查看你所在模块的[GrapesJS文档](https://grapesjs.com/docs/) 寻找“on()”事件监听器方法 大多数操作可以通过监听编辑器和组件事件来实现 常见模式: “JavaScript 留意变化 editor.on('change', () => console.log('something changed')); 组件生命周期 editor.on('component:mount', (c) => console.log('component rea...
相关问答
通过相似的问题讨论继续研究。
问题 #3225
如何从复选框特性中更新风格?
你好@artf 我尝试用复选框特性更新样式,但它没有调用函数。 这是代码 非常感谢你能提供的任何见解! 谢谢
问题 #3228
组件价值没有变化
你好@artf 我添加了一个新组件 这是代码 “''js 编辑。DomComponents.addType('header', { 型号:{ 默认值:{ 标签名称:'h1', 类型:“头部”, 尺寸:“H1”, 特质:[ { 名称:“尺寸”, 标签:“尺寸”, 类型:“选择”,...
问题 #4947
撤销管理器在撤销移除操作时无法正常工作
葡萄JS版本[X] 我确认使用最新版本的GrapesJS你用的是什么浏览器? Chrome - 版本 110.0.5481.100(官方构建)(arm64)可复制演示链接 https://codepen.io/faisal-praella/pen/ExeNwxJ描述一下那个虫子...
问题 #3239
如何动态地从组件脚本更新属性/属性?
我们有一个用例,想用 [Highcharts Editor](https://github.com/highcharts/highcharts-editor) 动态插入图表。我创建了一个组件,拖拽后会打开基于模态的组件,根据用户选择返回一个SVG图像。 我想知道如何把它加到模型...
与此问题匹配的付费插件
根据问题关键词和标签相关性精选,助你更快交付。
正在加载付费插件推荐……
相关教程
关于同一主题的深入指南。
教程
GrapesJS DevTools: A Developer Panel Inside Your Editor
If you've ever debugged GrapesJS with console.log(editor.getSelected()) and a dozen throwaway event listeners — this post is for you.
教程
How to Build a Production GrapesJS Editor: The Complete Walkthrough of Brief, Preset, Plugins, and Services
A complete walkthrough of building a production GrapesJS editor: how to choose a preset, pick plugins, and scope setup services without burning a sprint.
教程
Big Updates: TinyMCE 8 and Placeholder 2.0 for GrapesJS
In May we shipped major updates to two of our most popular GrapesJS plugins — TinyMCE Inline Text Editor and Placeholder.
浏览插件分类
直接跳转到市场上的插件分类页面。