用?进行带选项参数的路由和&

时间:2018-10-17 06:40:59

标签: angular routes angular-routing angular-components

我试图深入了解Angle的路线,但没有成功。我想做的是使用角度路由捕获URL中的所有参数。

因此,我们以以下网址为例:

Sub Ke1()

Dim twb As Workbook
Dim extwb As Workbook
Dim rng As Range
Dim i As Long
Dim lastRow As Long
lastRow = Cells(Rows.Count, 1).End(xlUp).Row + 1


Set twb = Workbooks.Open("C:\Users\faisal.abraham\Documents\Faisal\Travel\CCCPU030732017.xlsx")
Set extwb = Workbooks.Open("C:\Users\faisal.abraham\Documents\Faisal\Travel\CCCPU030732018.xlsx")
'Set twb = Application.Workbooks("CCCPU030732017.xlsx")
'Set extwb = Application.Workbooks("CCCPU030732018.xlsx")

Set rng = twb.Worksheets("PAID").Range("A1:E500")
For i = 5 To rng.Cells(Rows.Count, 2).End(xlUp).Row

    rng.Cells(i, 1).Copy extwb.Worksheets("PAID").Cells(lastRow, 1)
    rng.Cells(i, 2).Copy extwb.Worksheets("PAID").Cells(lastRow, 2)
    rng.Cells(i, 3).Copy extwb.Worksheets("PAID").Cells(lastRow, 3)
    rng.Cells(i, 4).Copy extwb.Worksheets("PAID").Cells(lastRow, 4)
    rng.Cells(i, 5).Copy extwb.Worksheets("PAID").Cells(lastRow, 5)

    lastRow = lastRow + 1

Next i

twb.Close savechanges:=False
extwb.Saved = True

End Sub

我正在尝试将参数分配给一些变量。

所以我做的(错误的)是这样的:

路线:

http://localhost:4200/PUC/#/P3?MODE=P&USER=AE13356&PROJECT=00001&PROTOCOL=00002&VERSION=1

组件:

 RouterModule.forRoot(
      [
        { path: "", component: AppComponent, pathMatch: 'full'},
      ],
    )

但是@Component({ selector: 'app-main', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit{ constructor( private router: Router, private route: ActivatedRoute ) { } ngOnInit(){ let queryParams = this.route.snapshot.queryParams; const mode = queryParams['MODE']; console.log("ID > " + mode); } } 始终是一个空对象。我想念什么?

编辑

使用queryParams我拥有完整的URL,我可以使用这种方式对获得的数据进行一些逻辑处理吗?基本上,我要做的就是如果location.href有一个值,则加载一个组件,如果PROJECT没有在URL中,则加载另一个组件。

3 个答案:

答案 0 :(得分:2)

快照在更改时不会提供更新的queryParam。但是queryParams上有一个BehaviorSubject ActivatedRoute,可以为您提供更新的查询参数。

您应该为此使用route.queryParams.subscribe

现在,我不确定如何处理您网址中的#。但是没有这个,由于您将PUC作为路由,并将P3作为其子级,因此您的Route Config看起来像这样:

RouterModule.forRoot([
  {
    path: 'PUC/:segment',
    component: PucComponent
  }
])

关于路线PIC,我们正在加载PucComponent,您应该在此处阅读queryParams

import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-puc',
  templateUrl: './puc.component.html',
  styleUrls: ['./puc.component.css']
})
export class PucComponent implements OnInit {

  constructor(
    private router: Router,
    private route: ActivatedRoute
  ) { }

  ngOnInit(){
    this.route.queryParams
      .subscribe(queryParams => console.log(queryParams));
  }

}

这是您推荐的Sample StackBlitz

答案 1 :(得分:2)

您要做的就是将查询参数的值括在“” 内。

*

如果查询参数的值为数字,则不需要用“” 括起来。

<a [routerLink]="['/PUC','P3']" [queryParams]="{key:'stringvalue'}">Click here</a>

app.moudle.ts

<a [routerLink]="['/PUC','P3']" [queryParams]="{key:number}">Click here</a>

app.component.html

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { RouterModule,Routes } from '@angular/router';
import { HelloComponent } from './hello.component';
import { AppComponent } from './app.component';

const routes:Routes = [{
       path:'PUC/P3', component:HelloComponent
    }]
@NgModule({
  imports:[ BrowserModule,FormsModule ,RouterModule.forRoot(routes,{useHash:true})],
  declarations: [ AppComponent, HelloComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

hello.component.ts

<a [routerLink]="['/PUC','P3']" [queryParams]="{ MODE: 'p', USER:'AE13356',PROJECT:00001,PROTOCOL:00002,VERSION:1}"> Click here</a>
<router-outlet></router-outlet>

您还可以看到有效的解决方案here in stackblitz

答案 2 :(得分:1)

通过订阅阅读

import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-user-detail',
  templateUrl: 'user-detail.component.html'
})
export class UserDetailComponent implements OnInit {
    constructor(private activeRoute: ActivatedRoute) {
    }

ngOnInit() {
    this.activeRoute.queryParams.subscribe(queryParams => {
        // do something with the query params
    });

    this.activeRoute.params.subscribe(routeParams => {
        this.loadUserDetail(routeParams.id);
    });
}

肮脏的方式

通过嵌套订户,如下所示:

ngOnInit() {
    // Nest them together and
    this.activeRoute.queryParams.subscribe(queryParams => {
        this.activeRoute.params.subscribe(routeParams => {
            this.loadUserDetail(routeParams.id, queryParams.type);
        });
    });
}

或者您可能会想编写此版本的变体,即将这些嵌套的回调移至辅助函数,然后再将其传递给另一个接受查询和路由参数的回调,即

ngOnInit() {
    this.readUrlParams((routeParams, queryParams) => {
        this.loadUserDetail(routeParams.id, queryParams.type);
    });
}

readUrlParams(callback) {
    // Nest them together and
    this.activeRoute.queryParams.subscribe(queryParams => {
        this.activeRoute.params.subscribe(routeParams => {
            callback(routeParams, queryParams);
        });
    });
}

使用RxJS

RxJS是一个非常强大的库,您可以用几种不同的方式来实现它,但是我最喜欢并发现自己使用得最多的一种方法是使用CombineLatest运算符,通过该运算符,我们可以合并路由和查询参数,并且只有一个可观察性地将我们两个都放在一个对象中。这是我们更新后的示例的样子

import { ActivatedRoute } from '@angular/router';

// Add the observable and combineLatest
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/combineLatest';

@Component({
  selector: 'app-user-detail',
  templateUrl: 'user-detail.component.html'
})
export class UserDetailComponent implements OnInit {
    constructor(private activeRoute: ActivatedRoute) {
    }

    ngOnInit() {
        // Combine them both into a single observable
        const urlParams = Observable.combineLatest(
          this.activatedRoute.params,
          this.activatedRoute.queryParams,
          (params, queryParams) => ({ ...params, ...queryParams})
        );

        // Subscribe to the single observable, giving us both
        urlParams.subscribe(routeParams => {
            // routeParams containing both the query and route params
            this.loadUserDetail(routeParams.id, routeParams.type);
        });
    }
}