5 lines
10 KiB
JSON
5 lines
10 KiB
JSON
{
|
||
"id": "guide/migration-renderer",
|
||
"title": "Renderer to Renderer2 migration",
|
||
"contents": "\n\n\n<div class=\"github-links\">\n <a href=\"https://github.com/angular/angular/edit/master/aio/content/guide/migration-renderer.md?message=docs%3A%20describe%20your%20change...\" aria-label=\"Suggest Edits\" title=\"Suggest Edits\"><i class=\"material-icons\" aria-hidden=\"true\" role=\"img\">mode_edit</i></a>\n</div>\n\n\n<div class=\"content\">\n <h1 id=\"renderer-to-renderer2-migration\"><code>Renderer</code> to <code><a href=\"api/core/Renderer2\" class=\"code-anchor\">Renderer2</a></code> migration<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/migration-renderer#renderer-to-renderer2-migration\"><i class=\"material-icons\">link</i></a></h1>\n<h2 id=\"migration-overview\">Migration Overview<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/migration-renderer#migration-overview\"><i class=\"material-icons\">link</i></a></h2>\n<p>The <code>Renderer</code> class has been marked as deprecated since Angular version 4.\nThis section provides guidance on migrating from this deprecated API to the newer <code><a href=\"api/core/Renderer2\" class=\"code-anchor\">Renderer2</a></code> API and what it means for your app.</p>\n<h2 id=\"why-should-i-migrate-to-renderer2\">Why should I migrate to Renderer2?<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/migration-renderer#why-should-i-migrate-to-renderer2\"><i class=\"material-icons\">link</i></a></h2>\n<p>The deprecated <code>Renderer</code> class has been removed in version 9 of Angular, so it's necessary to migrate to a supported API.\nUsing <code><a href=\"api/core/Renderer2\" class=\"code-anchor\">Renderer2</a></code> is the recommended strategy because it supports a similar set of functionality to <code>Renderer</code>.\nThe API surface is quite large (with 19 methods), but the schematic should simplify this process for your applications.</p>\n<h2 id=\"is-there-action-required-on-my-end\">Is there action required on my end?<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/migration-renderer#is-there-action-required-on-my-end\"><i class=\"material-icons\">link</i></a></h2>\n<p>No.\nThe schematic should handle most cases with the exception of <code>Renderer.animate()</code> and <code>Renderer.setDebugInfo()</code>, which already aren't supported.</p>\n<h2 id=\"what-are-the-__ngrendererx-methods-why-are-they-necessary\">What are the <code>__ngRendererX</code> methods? Why are they necessary?<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/migration-renderer#what-are-the-__ngrendererx-methods-why-are-they-necessary\"><i class=\"material-icons\">link</i></a></h2>\n<p>Some methods either don't have exact equivalents in <code><a href=\"api/core/Renderer2\" class=\"code-anchor\">Renderer2</a></code>, or they correspond to more than one expression.\nFor example, both renderers have a <code>createElement()</code> method, but they're not equal because a call such as <code>renderer.createElement(parentNode, namespaceAndName)</code> in the <code>Renderer</code> corresponds to the following block of code in <code><a href=\"api/core/Renderer2\" class=\"code-anchor\">Renderer2</a></code>:</p>\n<code-example language=\"ts\">\nconst [namespace, name] = splitNamespace(namespaceAndName);\nconst el = renderer.createElement(name, namespace);\nif (parentNode) {\n renderer.appendChild(parentNode, el);\n}\nreturn el;\n</code-example>\n<p>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 <code>createElement()</code> migration looks:</p>\n<p><strong>Before:</strong></p>\n<code-example language=\"ts\">\npublic createAndAppendElement() {\n const el = this.renderer.createElement('span');\n el.textContent = 'hello world';\n return el;\n}\n</code-example>\n<p><strong>After:</strong></p>\n<code-example>\n\npublic createAndAppendElement() {\n const el = __ngRendererCreateElement(this.renderer, this.element, 'span');\n el.textContent = 'hello world';\n return el;\n}\n// Generated code at the bottom of the file\n__ngRendererCreateElement(renderer: any, parentNode: any, nameAndNamespace: any) {\n const [namespace, name] = __ngRendererSplitNamespace(namespaceAndName);\n const el = renderer.createElement(name, namespace);\n if (parentNode) {\n renderer.appendChild(parentNode, el);\n }\n return el;\n}\n__ngRendererSplitNamespace(nameAndNamespace: any) {\n // returns the split name and namespace\n}\n\n</code-example>\n<p>When 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 <code>any</code> so that it doesn't have to insert extra logic that ensures that their values have the correct type.</p>\n<h3 id=\"im-a-library-author-should-i-run-this-migration\">I’m a library author. Should I run this migration?<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/migration-renderer#im-a-library-author-should-i-run-this-migration\"><i class=\"material-icons\">link</i></a></h3>\n<p><strong>Library authors should definitely use this migration to move away from the <code>Renderer</code>. Otherwise, the libraries won't work with applications built with version 9.</strong></p>\n<h3 id=\"full-list-of-method-migrations\">Full list of method migrations<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/migration-renderer#full-list-of-method-migrations\"><i class=\"material-icons\">link</i></a></h3>\n<p>The following table shows all methods that the migration maps from <code>Renderer</code> to <code><a href=\"api/core/Renderer2\" class=\"code-anchor\">Renderer2</a></code>.</p>\n<table>\n<thead>\n<tr>\n<th>Renderer</th>\n<th>Renderer2</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>listen(renderElement, name, callback)</code></td>\n<td><code>listen(renderElement, name, callback)</code></td>\n</tr>\n<tr>\n<td><code>setElementProperty(renderElement, propertyName, propertyValue)</code></td>\n<td><code>setProperty(renderElement, propertyName, propertyValue)</code></td>\n</tr>\n<tr>\n<td><code>setText(renderNode, text)</code></td>\n<td><code>setValue(renderNode, text)</code></td>\n</tr>\n<tr>\n<td><code>listenGlobal(target, name, callback)</code></td>\n<td><code>listen(target, name, callback)</code></td>\n</tr>\n<tr>\n<td><code>selectRootElement(selectorOrNode, debugInfo?)</code></td>\n<td><code>selectRootElement(selectorOrNode)</code></td>\n</tr>\n<tr>\n<td><code>createElement(parentElement, name, debugInfo?)</code></td>\n<td><code>appendChild(parentElement, createElement(name))</code></td>\n</tr>\n<tr>\n<td><code>setElementStyle(el, <a href=\"api/animations/style\" class=\"code-anchor\">style</a>, value?)</code></td>\n<td><code>value == null ? removeStyle(el, <a href=\"api/animations/style\" class=\"code-anchor\">style</a>) : setStyle(el, <a href=\"api/animations/style\" class=\"code-anchor\">style</a>, value)</code></td>\n</tr>\n<tr>\n<td><code>setElementAttribute(el, name, value?)</code></td>\n<td><code>attributeValue == null ? removeAttribute(el, name) : setAttribute(el, name, value)</code></td>\n</tr>\n<tr>\n<td><code>createText(parentElement, value, debugInfo?)</code></td>\n<td><code>appendChild(parentElement, createText(value))</code></td>\n</tr>\n<tr>\n<td><code>createTemplateAnchor(parentElement)</code></td>\n<td><code>appendChild(parentElement, createComment(''))</code></td>\n</tr>\n<tr>\n<td><code>setElementClass(renderElement, className, isAdd)</code></td>\n<td><code>isAdd ? addClass(renderElement, className) : removeClass(renderElement, className)</code></td>\n</tr>\n<tr>\n<td><code>projectNodes(parentElement, nodes)</code></td>\n<td><code>for (let i = 0; i < nodes.length; i++) { appendChild(parentElement, nodes[i]); }</code></td>\n</tr>\n<tr>\n<td><code>attachViewAfter(node, viewRootNodes)</code></td>\n<td><code>const parentElement = parentNode(node); const nextSibling = nextSibling(node); for (let i = 0; i < viewRootNodes.length; i++) { insertBefore(parentElement, viewRootNodes[i], nextSibling);}</code></td>\n</tr>\n<tr>\n<td><code>detachView(viewRootNodes)</code></td>\n<td><code>for (let i = 0; i < viewRootNodes.length; i++) {const node = viewRootNodes[i]; const parentElement = parentNode(node); removeChild(parentElement, node);}</code></td>\n</tr>\n<tr>\n<td><code>destroyView(hostElement, viewAllNodes)</code></td>\n<td><code>for (let i = 0; i < viewAllNodes.length; i++) { destroyNode(viewAllNodes[i]); }</code></td>\n</tr>\n<tr>\n<td><code>setBindingDebugInfo()</code></td>\n<td>This function is a noop in <code><a href=\"api/core/Renderer2\" class=\"code-anchor\">Renderer2</a></code>.</td>\n</tr>\n<tr>\n<td><code>createViewRoot(hostElement)</code></td>\n<td>Should be replaced with a reference to <code>hostElement</code></td>\n</tr>\n<tr>\n<td><code>invokeElementMethod(renderElement, methodName, args?)</code></td>\n<td><code>(renderElement as any)[methodName].apply(renderElement, args);</code></td>\n</tr>\n<tr>\n<td><code><a href=\"api/animations/animate\" class=\"code-anchor\">animate</a>(element, startingStyles, <a href=\"api/animations/keyframes\" class=\"code-anchor\">keyframes</a>, duration, delay, easing, previousPlayers?)</code></td>\n<td>Throws an error (same behavior as <code>Renderer.animate()</code>)</td>\n</tr>\n</tbody>\n</table>\n\n \n</div>\n\n<!-- links to this doc:\n-->\n<!-- links from this doc:\n - api/animations/animate\n - api/animations/keyframes\n - api/animations/style\n - api/core/Renderer2\n - guide/migration-renderer#full-list-of-method-migrations\n - guide/migration-renderer#im-a-library-author-should-i-run-this-migration\n - guide/migration-renderer#is-there-action-required-on-my-end\n - guide/migration-renderer#migration-overview\n - guide/migration-renderer#renderer-to-renderer2-migration\n - guide/migration-renderer#what-are-the-__ngrendererx-methods-why-are-they-necessary\n - guide/migration-renderer#why-should-i-migrate-to-renderer2\n - https://github.com/angular/angular/edit/master/aio/content/guide/migration-renderer.md?message=docs%3A%20describe%20your%20change...\n-->"
|
||
} |