{ "id": "guide/migration-renderer", "title": "Renderer to Renderer2 migration", "contents": "\n\n\n
Renderer
to Renderer2
migrationlinkThe Renderer
class has been marked as deprecated since Angular version 4.\nThis section provides guidance on migrating from this deprecated API to the newer Renderer2
API and what it means for your app.
The deprecated Renderer
class has been removed in version 9 of Angular, so it's necessary to migrate to a supported API.\nUsing Renderer2
is the recommended strategy because it supports a similar set of functionality to Renderer
.\nThe API surface is quite large (with 19 methods), but the schematic should simplify this process for your applications.
No.\nThe schematic should handle most cases with the exception of Renderer.animate()
and Renderer.setDebugInfo()
, which already aren't supported.
__ngRendererX
methods? Why are they necessary?linkSome methods either don't have exact equivalents in Renderer2
, or they correspond to more than one expression.\nFor example, both renderers have a createElement()
method, but they're not equal because a call such as renderer.createElement(parentNode, namespaceAndName)
in the Renderer
corresponds to the following block of code in Renderer2
:
Migration has to guarantee that the return values of functions and types of variables stay the same.\nTo handle the majority of cases safely, the schematic declares helper functions at the bottom of the user's file.\nThese helpers encapsulate your own logic and keep the replacements inside your code down to a single function call.\nHere's an example of how the createElement()
migration looks:
Before:
\nAfter:
\nWhen implementing these helper functions, the schematic ensures that they're only declared once per file and that their names are unique enough that there's a small chance of colliding with pre-existing functions in your code. The schematic also keeps their parameter types as any
so that it doesn't have to insert extra logic that ensures that their values have the correct type.
Library authors should definitely use this migration to move away from the Renderer
. Otherwise, the libraries won't work with applications built with version 9.
The following table shows all methods that the migration maps from Renderer
to Renderer2
.
Renderer | \nRenderer2 | \n
---|---|
listen(renderElement, name, callback) | \nlisten(renderElement, name, callback) | \n
setElementProperty(renderElement, propertyName, propertyValue) | \nsetProperty(renderElement, propertyName, propertyValue) | \n
setText(renderNode, text) | \nsetValue(renderNode, text) | \n
listenGlobal(target, name, callback) | \nlisten(target, name, callback) | \n
selectRootElement(selectorOrNode, debugInfo?) | \nselectRootElement(selectorOrNode) | \n
createElement(parentElement, name, debugInfo?) | \nappendChild(parentElement, createElement(name)) | \n
setElementStyle(el, style, value?) | \nvalue == null ? removeStyle(el, style) : setStyle(el, style, value) | \n
setElementAttribute(el, name, value?) | \nattributeValue == null ? removeAttribute(el, name) : setAttribute(el, name, value) | \n
createText(parentElement, value, debugInfo?) | \nappendChild(parentElement, createText(value)) | \n
createTemplateAnchor(parentElement) | \nappendChild(parentElement, createComment('')) | \n
setElementClass(renderElement, className, isAdd) | \nisAdd ? addClass(renderElement, className) : removeClass(renderElement, className) | \n
projectNodes(parentElement, nodes) | \nfor (let i = 0; i < nodes.length; i++) { appendChild(parentElement, nodes[i]); } | \n
attachViewAfter(node, viewRootNodes) | \nconst parentElement = parentNode(node); const nextSibling = nextSibling(node); for (let i = 0; i < viewRootNodes.length; i++) { insertBefore(parentElement, viewRootNodes[i], nextSibling);} | \n
detachView(viewRootNodes) | \nfor (let i = 0; i < viewRootNodes.length; i++) {const node = viewRootNodes[i]; const parentElement = parentNode(node); removeChild(parentElement, node);} | \n
destroyView(hostElement, viewAllNodes) | \nfor (let i = 0; i < viewAllNodes.length; i++) { destroyNode(viewAllNodes[i]); } | \n
setBindingDebugInfo() | \nThis function is a noop in Renderer2 . | \n
createViewRoot(hostElement) | \nShould be replaced with a reference to hostElement | \n
invokeElementMethod(renderElement, methodName, args?) | \n(renderElement as any)[methodName].apply(renderElement, args); | \n
animate(element, startingStyles, keyframes, duration, delay, easing, previousPlayers?) | \nThrows an error (same behavior as Renderer.animate() ) | \n