小编Sun*_*mar的帖子

Angular:如何使用RXJS 6调用finally()

我正在使用RXJS 5,现在当我升级到6时,我遇到了一些问题.

以前我能够使用catch和最后但是根据更新catch用catchError替换(在管道中)现在如何使用finally?

我也有一些问题:

我是否需要更改throw-> throwError(在下面的代码Observable.throw(err);)

import { Observable, Subject, EMPTY, throwError } from "rxjs";
import { catchError } from 'rxjs/operators';

return next.handle(clonedreq).pipe(
          catchError((err: HttpErrorResponse) => {
        if ((err.status == 400) || (err.status == 401)) {
            this.interceptorRedirectService.getInterceptedSource().next(err.status);
            return Observable.empty();
        } else {
            return Observable.throw(err);
        }
       }) 
        //, finally(() => {
        //  this.globalEventsManager.showLoader.emit(false);
        //});
      );
Run Code Online (Sandbox Code Playgroud)

另外如何使用publish().refCount()?

rxjs angular angular6 rxjs6

21
推荐指数
2
解决办法
1万
查看次数

如何在Angular中检测何时更改路线和替换新视图

我希望检测何时更改了路线并替换了新的view(html)。

尝试过以下代码,但不知道如何继续进行。

this._routerSub = this.router.events
  .filter(event => event instanceof NavigationEnd)
  //.do(event => console.log(event)) // 
  .subscribe((value) => { });
Run Code Online (Sandbox Code Playgroud)

还尝试了下面的代码,但出现错误

无法将事件类型的参数分配给类型'(value:Event)=> void'的参数。

   import { Router, NavigationEnd, Event  } from '@angular/router';

   this._routerSub = this.router.events  
      .subscribe(event : Event) => {
      if (event instanceof NavigationEnd) {
        console.log(event.url);
      }
    });
Run Code Online (Sandbox Code Playgroud)

下面的Vega是我的app.component.ts和app.component.ts代码。是不是应该在激活时调用onActivate。由于路由器插座将在实例化新组件时发出激活事件。

app.component.html

<router-outlet (activate)="onActivate($event)" ></router-outlet>
Run Code Online (Sandbox Code Playgroud)

app.component.ts

   onActivate(e) {
        debugger;
        console.log(1);
      }
Run Code Online (Sandbox Code Playgroud)

typescript angular-routing angular angular5

6
推荐指数
1
解决办法
4425
查看次数

Angular:无法在 SignalR HubConnection 中传递 API url

我在 Angular6 中开发了一个项目。需要为实时聊天开发部分。为此,我在我的 asp.net 核心 Web Apiproject 中使用 SignalR。现在我想在我的 Angular 项目中使用这个 Web Api 参考。

我正在使用链接。

但是在 App.Component.ts 中提供 Web Api url 时,我收到以下错误:

“HubConnection”类的构造函数是私有的,只能在类声明中访问。

应用组件.ts:

import { HubConnection } from '@aspnet/signalr';
import { Message } from 'primeng/api';

export class AppComponent implements OnInit {
  public _hubConnection: HubConnection;
  msgs: Message[] = [];
  constructor() {   
  }
  ngOnInit() {
    this._hubConnection = new HubConnection('http://localhost:1874/notify'); // getting error on this line.
Run Code Online (Sandbox Code Playgroud)

编辑:尝试以下代码:-

修改后的 App.Component.ts :

import { HubConnection } from '@aspnet/signalr';
import * as …
Run Code Online (Sandbox Code Playgroud)

signalr signalr-hub angular6 angular-cli-v6

4
推荐指数
1
解决办法
3195
查看次数

Angular:如何检查某些控件是否存在于表单中

以下是我的代码,可从服务获得响应。在这里,我得到了员工名单。

我需要根据服务的响应动态地绑定表单控件,我的服务返回的字段(表单中的员工ID,姓名,部门等)比表单控件更多。如何跳过那些不在窗体控件中使用的控件?

this._employeeService.getEmployeeById(this.employeeId).subscribe((res: Response) => {
        debugger;
        this.employeeForm.get('FileUploader').setValue(null);        
        //this.employeeForm.setValue(res);
        for (const field in res) {
          this.employeeForm.controls[field].setValue(res[field]);
        }
      }); 

this.employeeForm = this._fb.group({
      EmployeeId: 0,
      Name: ''});
Run Code Online (Sandbox Code Playgroud)

angular angular-forms angular6 angular-formbuilder

3
推荐指数
3
解决办法
4459
查看次数