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 对象以进行注入。

相关内容

热门资讯

据了解!聚财平台辅助(辅助挂)... 据了解!聚财平台辅助(辅助挂)原来有挂(有挂绝活)-哔哩哔哩;无需打开直接搜索加(薇:1367043...
第四分钟外挂!大当家辅助脚本设... 第四分钟外挂!大当家辅助脚本设置,黑桃a3哟西破解版,爆料教程(存在有挂)-哔哩哔哩1、首先打开大当...
开挂透视!wepoker透视教... 开挂透视!wepoker透视教程,wepoker有没有挂,(玩家必看外挂辅助神器)是一款可以让一直输...
此事迅速冲上热搜!新道游开挂(... 此事迅速冲上热搜!新道游开挂(辅助挂)果然确实有挂(有挂诀窍)-哔哩哔哩;亲,新道游开挂这款游戏原来...
第一分钟外挂!广东雀神辅助器,... 第一分钟外挂!广东雀神辅助器,新蜜瓜大厅控制器,切实教程(有挂分享)-哔哩哔哩1、新蜜瓜大厅控制器系...
开挂透视!wepoker德州一... 无需打开直接搜索;操作使用教程:开挂透视!wepoker德州一键透视购买,wepoker透视辅助挂的...
针对!新二号辅助(辅助挂)一贯... 针对!新二号辅助(辅助挂)一贯有挂(有挂步骤)-哔哩哔哩1、下载安装好新二号辅助,进入游戏主界面,点...
第二分钟外挂!新天道辅助脚本,... 第二分钟外挂!新天道辅助脚本,丽水都莱脚本辅助视频,新2025版(有挂辅助)-哔哩哔哩1、上手简单,...
开挂透视!wpk俱乐部有挂吗,... >>>您好:,软件加微信【添加136704302】确实是有挂的,很多玩家在这款游戏中打牌都会发现很多...
受玩家影响!欢乐茶馆辅助(辅助... 受玩家影响!欢乐茶馆辅助(辅助挂)一直是有挂(有挂方案)-哔哩哔哩>>您好:软件加136704302...