相关疑难解决方法(0)

在RxJs 5中分享Angular Http网络调用结果的正确方法是什么?

通过使用Http,我们调用一个执行网络调用并返回http observable的方法:

getCustomer() {
    return this.http.get('/someUrl').map(res => res.json());
}
Run Code Online (Sandbox Code Playgroud)

如果我们采用这个可观察的并添加多个订阅者:

let network$ = getCustomer();

let subscriber1 = network$.subscribe(...);
let subscriber2 = network$.subscribe(...);
Run Code Online (Sandbox Code Playgroud)

我们想要做的是确保这不会导致多个网络请求.

这可能看起来像是一个不寻常的场景,但实际上很常见:例如,如果调用者订阅了observable以显示错误消息,并使用异步管道将其传递给模板,那么我们已经有两个订阅者.

在RxJs 5中这样做的正确方法是什么?

也就是说,这似乎工作正常:

getCustomer() {
    return this.http.get('/someUrl').map(res => res.json()).share();
}
Run Code Online (Sandbox Code Playgroud)

但这是在RxJs 5中这样做的惯用方式,还是我们应该做其他事情呢?

注意:根据Angular 5 new HttpClient,.map(res => res.json())所有示例中的部分现在都没用,因为现在默认采用JSON结果.

rxjs angular2-services rxjs5 angular

281
推荐指数
11
解决办法
8万
查看次数

在Angular中迭代对象

我正在尝试在Angular 2 Alpha 28中做一些事情,并且我遇到了字典和NgFor的问题.

我有一个TypeScript接口,如下所示:

interface Dictionary {
    [ index: string ]: string
}
Run Code Online (Sandbox Code Playgroud)

在JavaScript中,这将转换为数据可能如下所示的对象:

myDict={'key1':'value1','key2':'value2'}
Run Code Online (Sandbox Code Playgroud)

我想迭代这个并尝试这个:

<div *ngFor="(#key, #value) of myDict">{{key}}:{{value}}</div>
Run Code Online (Sandbox Code Playgroud)

但无济于事,以下都没有:

<div *ngFor="#value of myDict">{{value}}</div>
<div *ngFor="#value of myDict #key=index">{{key}}:{{value}}</div>
Run Code Online (Sandbox Code Playgroud)

在所有情况下,我都会收到"意外令牌"或"无法找到'iterableDiff'管道支持对象"之类的错误

我在这里错过了什么?这不可能了吗?(第一种语法适用于Angular 1.x)或者是迭代对象的语法是不同的?

angular

110
推荐指数
7
解决办法
16万
查看次数

多个模块中的管道重用,NG6007(由多个 NgModule 声明)或 NG6002(无法解析为 NgModule 类),

我的管道位于apps\administrator\src\app\modules\messenger\pipes\custom-message.pipe.ts

import { Pipe, PipeTransform } from '@angular/core';

/**
 * Pipe for Custom Message for boolean
 */
@Pipe({
  name: 'customMessage',
})
export class CustomMessagePipe implements PipeTransform {
  public transform(value: boolean, trueString: string, falseString: string): string {
    //The code
  }
}
Run Code Online (Sandbox Code Playgroud)

在主模块apps\administrator\src\app\app.module.ts文件中:

import { CustomMessagePipe } from './modules/messenger/pipes/custom-message.pipe';
...
@NgModule({
  declarations: [..., CustomMessagePipe],
  providers: [...],
})
export class AppModule {}
Run Code Online (Sandbox Code Playgroud)

现在,我有两个FormSmsModule模块FormSmtpModule

FormSmsModule位于apps\administrator\src\app\modules\messenger\form-sms

FormSmtpModule位于apps\administrator\src\app\modules\messenger\form-smtp

按照这个答案/sf/answers/4372816381/

在数组上使用的文件apps\administrator\src\app\modules\messenger\form-sms\form-sms.module.ts中,我有:CustomMessagePipeimports

import …
Run Code Online (Sandbox Code Playgroud)

module typescript angular-pipe angular

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

在子模块中发出相同的管道组件声明

我想在父模块中声明管道并在子模块中使用它.

@NgModule({
  // Pipe which I want to declare in all child modules
  declarations: [ ThisIsPipe ],
  imports: [ ChildModuleOne, ChildModuleTwo],  
})
Run Code Online (Sandbox Code Playgroud)

我该如何使用It子模块?

因为如果我宣布它两次我就会收到错误

未捕获错误:类型ThisIsPipe是2个模块声明的一部分:ChildModuleOne和ChildModuleTwo!请考虑将ThisIsPipe移动到导入ChildModuleOne和ChildModuleTwo的更高模块.您还可以创建一个新的NgModule,它导出并包含ThisIsPipe,然后在ChildModuleOne和ChildModuleTwo中导入NgModule.

import typescript angular

7
推荐指数
1
解决办法
2490
查看次数