In Magento 2 what is the difference between a component and a widget? For example there is a modal component and a modal widget, they both appear to do the same thing.

Component

The Modal UI component implements a secondary window that opens on top of the main window. It uses the modal widget.

Widget

The Magento modal widget implements a secondary window that opens on top of the main window.

The widget can be initialised with a few lines of JS, where as the component requires many lines of XML and still uses the widget. So why would anyone use the XML method?

有帮助吗?

解决方案

The widget can be initialized with a few lines of JS, where as the component requires many lines of XML and still uses the widget. So why would anyone use the XML method?

The UI component system allows a developer to package up a number of different RequireJS modules as KnockoutJS view model constructors, (with associated "remote" Knockout.js templates), and then use those view models in the HTML source of the Magento page with a special Knockout.js scope binding. Every UI component is also associated with a set of server side data. The end goal is to create a complex piece of UI functionality that's not just one giant messy javascript file, but that end users can include with a simple <uiCompnent name="foo"/> in their layout XML.

So, you're correct that the modal widget is easier to get started with. However, if you're building a complex UI Component, and want your complex UI Component to include modal functionality, you'd pull in Modal UI component.

At least -- that's my current understanding of the situation/system. More context in my UI Component series (self link) if you're interested

许可以下: CC-BY-SA归因
scroll top