Angular元素-查询参数一直为空
创始人
2024-10-30 18:00:19
0

在使用 Angular 元素时,查询参数可能会一直为空。解决方法是使用 Renderer2 类的 createText 方法创建带有查询参数的 URL,然后将其添加到属性中。

示例代码:

import { Component, Renderer2, ElementRef, Injector } from '@angular/core';
import { createCustomElement } from '@angular/elements';

@Component({
  selector: 'app-query-params',
  template: `
    

Query Params: {{ queryParams }}

`, }) export class QueryParamsComponent { queryParams: string; constructor( private renderer: Renderer2, private elementRef: ElementRef, private injector: Injector, ) { const queryParams = new URLSearchParams(window.location.search); // Create URL with query params using Renderer2 const href = this.renderer.createText( `/my-element?${queryParams.toString()}`, ); // Add href attribute with created URL to this component's element this.renderer.setAttribute( this.elementRef.nativeElement, 'href', href.textContent, ); // Get query params from window location this.queryParams = queryParams.get('param'); } } // Create custom element const QueryParamsElement = createCustomElement(QueryParamsComponent, { injector: Injector, }); // Register custom element customElements.define('my-element', QueryParamsElement);

注意:在使用 Renderer2 时,需要将其注入到组件中。而 createCustomElement 方法必须传递 Injector 对象以进行注入。

相关内容

热门资讯

透视挂!aapoker辅助怎么... 透视挂!aapoker辅助怎么用(透视)开挂辅助挂(都是真的有挂)-哔哩哔哩1、让任何用户在无需aa...
透视透视!wepoker开脚本... 透视透视!wepoker开脚本视频,wepoker永久免费脚本,操作教程(本来存在有挂)-哔哩哔哩1...
透视规律!德扑之心免费透视(透... 透视规律!德扑之心免费透视(透视)开挂辅助app(原来是真的挂)-哔哩哔哩透视规律!德扑之心免费透视...
透视好牌!wepoker透视脚... 透视好牌!wepoker透视脚本免费下载,wpk俱乐部怎么辅助,阶段教程(一贯是有挂)-哔哩哔哩1、...
透视辅助!wpk模拟器是什么(... 透视辅助!wpk模拟器是什么(透视)开挂辅助app(好像真的是有挂)-哔哩哔哩1、下载好wpk模拟器...
透视能赢!aapoker免费透... 透视能赢!aapoker免费透视脚本,wepoker怎么开辅助,窍要教程(原来真的有挂)-哔哩哔哩1...
透视靠谱!wpk系统是否存在辅... 透视靠谱!wpk系统是否存在辅助行为(透视)开挂辅助攻略(一贯有挂)-哔哩哔哩1、让任何用户在无需w...
透视实锤!约局吧德州可以透视吗... 透视实锤!约局吧德州可以透视吗,黑侠破解wepoker,攻略教程(本来有挂)-哔哩哔哩1、约局吧德州...
透视胜率!wepoker可以透... 透视胜率!wepoker可以透视码(透视)开挂辅助软件(切实是有挂)-哔哩哔哩在进入wepoker可...
透视透视!hhpoker德州真... 透视透视!hhpoker德州真的假的,拱趴大菠萝万能挂,举措教程(切实有挂)-哔哩哔哩1、操作简单,...