angular分析url

如果要使用查询参数而不在路由中定义它们,则可以在构造函数中引用ActivatedRouting并订阅queryParams。

constructor(private route: ActivatedRoute) {
this.route
.queryParams
.subscribe(params => {
if (params) {
console.log(params);
}
});
}

比如请求的为:
http://localhost:4200/?89789=78&tyrt=t
那么打印的为:
{89789: "78", tyrt: "t"}

比如:一定加http://尾号
const url = 'http://127.0.0.1:4200?';
const p = 'SerialNumber=' + data.SerialNumber +'&Name='+data.Name+'&Type='+data.Type
+'&Year='+data.TestDate.Year+'&Month='+data.TestDate.Month+'&Day='+data.TestDate.Day
+'&Hour='+data.TestDate.Hour+'&Minute='+data.TestDate.Minute+'&Second='+data.TestDate.Second;
window.open(encodeURI(url+p));

参数解析,能够解析127.0.0.1:4200/indexxxxxxx.html?para='xxxx'和127.0.0.1:4200/index/heh/ttt/SN/1200/xxx等后10个确定的路径。这是两种路径,下面是解析代码:
try {
if (this.route.queryParams['value'] && this.route.queryParams['value']['para']) {//解析带参数的
const qp: string = this.route.queryParams['value']['para'];
const qps = qp.split('|');
if (qps.length >= 10) {

                const searchTypeName = qps[0];
                if (searchTypeName === 'SN') {
                    this.searchType = 0;
                } else if (searchTypeName === 'TID') {
                    this.searchType = 1;
                } else if (searchTypeName === 'SN') {
                    this.searchType = 2;
                }

                this.searchParam = qps[1];
                this.resultType = Number(qps[2]);
                this.resultName = qps[3];

                const year = Number(qps[4]);
                const month = Number(qps[5]);
                const day = Number(qps[6]);
                const hour = Number(qps[7]);
                const minute = Number(qps[8]);
                const second = Number(qps[9]);

                this.time = new rserver.DateAndTime({
                    'Year': year,
                    'Month': month,
                    'Day': day,
                    'Hour': hour,
                    'Minute': minute,
                    'Second': second
                });
            }
        } else {
            if (this.route.url['value'] && this.route.url['value']['length'] >= 10) {//解析不带参数的

                const routePathLength = +this.route.url['value']['length'];
                const searchTypeName = this.route.url['value'][routePathLength - 10]['path'];
                if (searchTypeName === 'SN') {
                    this.searchType = 0;
                } else if (searchTypeName === 'TID') {
                    this.searchType = 1;
                } else if (searchTypeName === 'SN') {
                    this.searchType = 2;
                }

                this.searchParam = this.route.url['value'][routePathLength - 9]['path'];
                this.resultType = Number(this.route.url['value'][routePathLength - 8]['path']);
                this.resultName = this.route.url['value'][routePathLength - 7]['path'];

                const year = Number(this.route.url['value'][routePathLength - 6]['path']);
                const month = Number(this.route.url['value'][routePathLength - 5]['path']);
                const day = Number(this.route.url['value'][routePathLength - 4]['path']);
                const hour = Number(this.route.url['value'][routePathLength - 3]['path']);
                const minute = Number(this.route.url['value'][routePathLength - 2]['path']);
                const second = Number(this.route.url['value'][routePathLength - 1]['path']);

                this.time = new rserver.DateAndTime({
                    'Year': year,
                    'Month': month,
                    'Day': day,
                    'Hour': hour,
                    'Minute': minute,
                    'Second': second
                });
            }
        }
    } catch { }

打开不带参数的路径:
const url = 'http://127.0.0.1:4200/';
const p = '/SN/' + data.SerialNumber +'/'+data.Type+'/'+data.Name
+'/'+data.TestDate.Year+'/'+data.TestDate.Month+'/'+data.TestDate.Day
+'/'+data.TestDate.Hour+'/'+data.TestDate.Minute+'/'+data.TestDate.Second;
window.open(encodeURI(url+p));

如果不适用路由器,即如果只有单页面的话,可以使用:
// http://127.0.0.1:4200/asdas/asda/sdsa/sadas/SN/TBEA00RD410168/40002/001-default/2018/12/4/16/51/43
// http://127.0.0.1:4200/inasdasd.html?para=SN|TBEA00RD410168|40002|001-default|2018|12|4|16|51|43
async ngOnInit() {
this.searchType = 0;
try {
if (location.search && location.search.indexOf('para') >= 0) {

    let qp: string = location.search;
    qp = qp.replace('?para=', '');
    const qps = qp.split('|');
    if (qps.length >= 10) {
      this.createParas(qps);
    } else {
      this.errorString = await this.translate.get('GLOBAL.URLE').toPromise();
    }

  } else {
    if (location.href && location.pathname) {
      const qp: string = location.pathname;
      const qps = qp.split('/');

      if (qps.length >= 10) {
        const pars: string[] = [];
        for (let i = 10; i >= 1; i--) {
          pars.push(qps[qps.length - i]);
        }
        this.createParas(pars);
      } else {
        this.errorString = await this.translate.get('GLOBAL.URLE').toPromise();
      }
    }
  }
} catch { }

}

private createParas(qps: string[]) {
const searchTypeName = qps[0];
if (searchTypeName === 'SN') {
this.searchType = 0;
} else if (searchTypeName === 'TID') {
this.searchType = 1;
} else if (searchTypeName === 'SN') {
this.searchType = 2;
}

this.searchParam = qps[1];
this.resultType = Number(qps[2]);
this.resultName = qps[3];

const year = Number(qps[4]);
const month = Number(qps[5]);
const day = Number(qps[6]);
const hour = Number(qps[7]);
const minute = Number(qps[8]);
const second = Number(qps[9]);

this.time = new rserver.DateAndTime({
  'Year': year,
  'Month': month,
  'Day': day,
  'Hour': hour,
  'Minute': minute,
  'Second': second
});

}

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Overview The ccxt library is a collection of available cr...
    郭蝈儿蝈儿阅读 4,073评论 0赞 1
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 13,080评论 6赞 13
  • 在小灶预备群就认识檀子,一个很积极,为大家奉献的姑娘,当时就赶紧勾搭了。 她是典型的行动派,偶然在群里看到她的【清...
    豆子121阅读 275评论 2赞 2
  • 适可而止,无贪心也 爱意该如此 适可而止,不为己甚 憎怨该如此 适可而止,得莫再往 距离该如此 适可而止,得之其道...
    三两Talk阅读 883评论 0赞 1
  • 【一】 那一日,她哀恸过度,遗落心之所好,忘却风华无双,纵身跃入熊熊烈火以殉先王。烟熏火烤,不省人事。 救起后,她...
    子瑈阅读 982评论 58赞 23

友情链接更多精彩内容