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

相关内容

热门资讯

透视神器!pokemmo脚本(... 透视神器!pokemmo脚本(透视)开挂辅助神器(原来是真的挂)-哔哩哔哩1、用户打开应用后不用登录...
透视透视!aapoker怎么控... 透视透视!aapoker怎么控制牌,wepoker怎么设置房间,办法教程(竟然是真的挂)-哔哩哔哩1...
透视软件!pokemmo手机版... 透视软件!pokemmo手机版脚本免费,麻雀智能辅助软件,总是存在有挂(揭露2025新版)-哔哩哔哩...
透视苹果版本!wepoker透... 透视苹果版本!wepoker透视脚本免费使用视频(透视)开挂辅助软件(其实是真的挂)-哔哩哔哩1、w...
透视app!hhpoker有透... 透视app!hhpoker有透视功能吗,哈糖大菠萝有挂吗,手段教程(一贯真的是有挂)-哔哩哔哩hhp...
透视工具!htx矩阵wepok... 透视工具!htx矩阵wepoker辅助,兴动互娱辅助工具,真是存在有挂(教你2025新版教程)-哔哩...
透视实锤!哈糖大菠萝有没有挂(... 透视实锤!哈糖大菠萝有没有挂(透视)开挂辅助工具(真是有挂)-哔哩哔哩哈糖大菠萝有没有挂辅助器中分为...
透视透视挂!hhpoker可以... 透视透视挂!hhpoker可以开挂吗,wepoker新号好一点吗,模块教程(本来存在有挂)-哔哩哔哩...
透视系统!wepoker破解工... 透视系统!wepoker破解工具,神途免费辅助软件,一直真的是有挂(普及详细教程)-哔哩哔哩1、玩家...
透视开挂!德普之星辅助器app... 透视开挂!德普之星辅助器app(透视)开挂辅助工具(一直是有挂)-哔哩哔哩1、这是跨平台的德普之星辅...