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

相关内容

热门资讯

受玩家影响!拱趴大菠萝有挂吗,... 您好,拱趴大菠萝有挂吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩...
近期!约局吧德州真的有透视挂吗... 近期!约局吧德州真的有透视挂吗,微信微乐小程序修改器,其实真的有辅助教程(哔哩哔哩)1、全新机制【约...
目前!wepoker私人定制透... 目前!wepoker私人定制透视,pokeplus脚本,竟然是真的有辅助工具(哔哩哔哩)1、poke...
长期以来!we poker免费... 长期以来!we poker免费辅助器,新玉海楼茶苑脚本,切实真的有辅助教程(哔哩哔哩)1、起透看视 ...
今天下午!hhpoker有没有... 今天下午!hhpoker有没有辅助,随意玩5元流量包,好像存在有辅助工具(哔哩哔哩)1、不需要AI权...
据权威媒体报道!wepoker... 据权威媒体报道!wepoker有透视功能吗,天酷辅助器,竟然是真的有辅助技巧(哔哩哔哩)1)天酷辅助...
值得注意的是!德普之星私人局辅... 值得注意的是!德普之星私人局辅助免费,赣牌圈破解器,其实有辅助攻略(哔哩哔哩)1)德普之星私人局辅助...
受玩家影响!hhpoker真能... 受玩家影响!hhpoker真能买到挂吗,潮汕来物局游戏源码,一直是真的有辅助脚本(哔哩哔哩)潮汕来物...
有玩家发现!pokermast... 有玩家发现!pokermaster辅助器,新漫游免费辅助器,一贯是有辅助插件(哔哩哔哩)1、上手简单...
刚刚!wepoker透视脚本免... 刚刚!wepoker透视脚本免费app,衢州都莱罗松辅助软件,竟然存在有辅助app(哔哩哔哩)所有人...