Angular懒加载与查询参数不起作用
创始人
2024-10-27 22:01:12
0

问题描述: 在Angular应用中,当使用懒加载加载模块时,查询参数不起作用。

解决方法:

  1. 在路由定义中添加查询参数配置: 在路由定义中,需要为懒加载的模块添加查询参数配置。例如:
{
  path: 'lazy',
  loadChildren: () => import('./lazy-module/lazy.module').then(m => m.LazyModule),
  pathMatch: 'full',
  canActivate: [AuthGuard],
  runGuardsAndResolvers: 'always',
  query: {
    param1: 'value1',
    param2: 'value2'
  }
}

在上面的例子中,懒加载的模块为LazyModule,并且为该模块添加了两个查询参数param1param2

  1. 在懒加载的模块中获取查询参数: 在懒加载的模块中,可以使用ActivatedRoute来获取查询参数。例如:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-lazy',
  templateUrl: './lazy.component.html',
  styleUrls: ['./lazy.component.css']
})
export class LazyComponent implements OnInit {
  param1: string;
  param2: string;

  constructor(private route: ActivatedRoute) { }

  ngOnInit() {
    this.param1 = this.route.snapshot.queryParamMap.get('param1');
    this.param2 = this.route.snapshot.queryParamMap.get('param2');
  }
}

在上面的例子中,通过ActivatedRoutequeryParamMap属性获取查询参数,并将其赋值给组件中的变量param1param2

  1. 在模板中使用查询参数: 在模板中,可以直接使用组件中获取到的查询参数。例如:

Param 1: {{ param1 }}

Param 2: {{ param2 }}

在上面的例子中,通过双花括号语法{{}}将查询参数显示在页面上。

通过以上步骤,就可以在懒加载的模块中获取并使用查询参数了。

相关内容

热门资讯

透视有挂!hhpoker免费透... 透视有挂!hhpoker免费透视脚本(透视)好像是真的有挂(详细辅助2025新版总结)1)hhpok...
必备科技!hhpoker是真的... 必备科技!hhpoker是真的假的,wpk作弊最怕三个东西,新2025教程(有挂透视);1分钟了解详...
透视讲解!wepoker透视器... 透视讲解!wepoker透视器免费(透视)总是是真的有挂(详细辅助wepoke教程)小薇(透视辅助)...
玩家必知教程!wpk透视辅助下... 玩家必知教程!wpk透视辅助下载,wejoker辅助器要钱玩吗,必赢教程(有挂技巧);建议优先通过w...
透视黑科技!wepokerpl... 透视黑科技!wepokerplus辅助(透视)总是是有挂(详细辅助必备教程)一、AI软件牌型概率发牌...
必知教程!约局吧辅助器,wep... 必知教程!约局吧辅助器,wepoker代打辅助,扑克教程(有挂透视);建议优先通过wepoker代打...
透视了解!aapoker发牌逻... 透视了解!aapoker发牌逻辑(透视)果然存在有挂(详细辅助攻略教程)辅助器中分为三种模型:软件透...
免费测试版!aapoker怎么... 免费测试版!aapoker怎么提高中牌率,hhpoker买挂,安装教程(有挂技巧);aapoker怎...
透视模拟器!德普之星透视辅助软... 透视模拟器!德普之星透视辅助软件激活码(透视)都是存在有挂(详细辅助AI教程)1、德普之星透视辅助软...
玩家必用!菠萝辅助器免费版的特... 玩家必用!菠萝辅助器免费版的特点,we poker辅助器下载,详细教程(有挂透视);致您一封信;亲爱...