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

相关内容

热门资讯

科技新动态!广东闲来辅助神器免... 科技新动态!广东闲来辅助神器免费版(辅助挂)wpk俱乐部辅助器(原来真的有挂);1、超多福利:超高返...
玩家必备科普!白银胡乐亲友房(... 玩家必备科普!白银胡乐亲友房(辅助挂)哈糖大菠萝怎么挂(固有有挂);1、找出白银胡乐亲友房 ai辅助...
推荐攻略!微信小程序游戏辅助(... 推荐攻略!微信小程序游戏辅助(辅助挂)wepoker透视方法(从前存在有挂)是一款可以让一直输的玩家...
一分钟揭秘!天天互娱app辅助... 一分钟揭秘!天天互娱app辅助(辅助挂)拱趴大菠萝挂(真是真的有挂);1、超多福利:超高返利,海量正...
推荐一款!情怀透明器看手机纸牌... 推荐一款!情怀透明器看手机纸牌(辅助挂)hhpoker有没有作弊辅助(本来是有挂)是一款可以让一直输...
推荐十款!四川皮皮辅助(辅助挂... 推荐十款!四川皮皮辅助(辅助挂)wepoker私人局透视(原先真的是有挂);【福星临门,好运相随】;...
研究成果!潮汕激k辅助器(辅助... 您好:潮汕激k辅助器这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特...
一起来探讨!河南微乐麻将小程序... 一起来探讨!河南微乐麻将小程序辅助器(辅助挂)智星德州辅助译码插件靠谱吗(原本真的是有挂);河南微乐...
盘点几款!小程序财神十三张辅助... 盘点几款!小程序财神十三张辅助(辅助挂)红龙poker有辅助吗(从前是有挂);小程序财神十三张辅助是...
玩家亲测!老友辅助器(辅助挂)... 玩家亲测!老友辅助器(辅助挂)wepoker脚本(从来有挂);1、真人老友辅助器辅助透视,一键组局,...