标签: angular6

类型 'Observable<HttpEvent<>>' 不可分配给类型 'Observable<>'

我有这个代码片段:

SubmitTransaction(transNumber: string, transactionRequest: ITransactionRequestObj): Observable<TransactionResponse> {
    this.body =  JSON.stringify(transactionRequest);
    this.headers = new HttpHeaders().set('Content-Type', 'application/json');
    this.headers.append('Accept', 'application/json');
    this.options = new RequestOptions({headers: this.headers});
    return this.http.post<TransactionResponse>(this.baseUrl + '/transactions/' + transNumber + '/new',  this.body, this.options)
   .pipe(catchError((e) => this.errorHandler(e)));
  }
Run Code Online (Sandbox Code Playgroud)

我所做的就是将我的项目从 Angular 5 升级到 Angular 6 并更改.catch((e) => this.errorHandler(e));为 .pipe(catchError((e) => this.errorHandler(e)));. 但是,对于此特定方法,我收到以下 TypeScript 错误。

错误:

[ts]
Type 'Observable<HttpEvent<TransactionResponse>>' is not assignable to type 'Observable<TransactionResponse>'.
  Type 'HttpEvent<TransactionResponse>' is not assignable to type 'TransactionResponse'.
    Type 'HttpSentEvent' is not assignable to type 'TransactionResponse'. …
Run Code Online (Sandbox Code Playgroud)

typescript angular angular6 rxjs6

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

在组件测试规范中模拟 BehaviourSubject

我正在尝试模拟组件测试中的服务依赖项,该服务具有我正在尝试模拟的行为主题属性。

我的服务如下:

export class DatePickerService {
  public date: moment.Moment;
  public selectedDate: BehaviorSubject<moment.Moment> = new BehaviorSubject<moment.Moment>(moment());

  public changeDate = (date: moment.Moment) => {
    this.selectedDate.next(date);
  }
}
Run Code Online (Sandbox Code Playgroud)

然后在我的组件中订阅 selectedDate:

ngOnInit() {
    this.datePickerService.selectedDate.subscribe((selectedDate) => {
        // do stuff here...
    }
}
Run Code Online (Sandbox Code Playgroud)

在测试我的组件时,我使用 TestBed 方法并为日期选择器服务提供我自己的模拟:

const mockDatePickerService = {
    selectedDate: jasmine.createSpy().and.returnValue(of(moment('01-01-2018', 'DD-MM-YYYY')))
};

beforeEach(async(() => {
    TestBed.configureTestingModule({
        declarations: [
            MyComponent
        ],
        providers: [
            HttpClient,
            HttpHandler,
            { provide: DatePickerService, useValue: mockDatePickerService }
        ],
        imports: [
            TranslateModule.forRoot(),
            RouterTestingModule
        ]
    })
    .compileComponents();
}));
Run Code Online (Sandbox Code Playgroud)

在尝试运行我的第一个测试时,我从

TypeError: undefined is …
Run Code Online (Sandbox Code Playgroud)

typescript angular angular6

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

角度 6 元素布尔输入参数

我正在构建一个角度元素并将一些输入参数传递给它。我可以很好地传递一些字符串输入参数(ikrId 和环境工作得很好),但是很难通过角度元素将布尔值传递给 showTitle 输入属性(将 showTitle 属性从父角度组件传递给组件工作正常)。

这是组件片段:

export class MyComponent implements OnInit, OnDestroy {

  @Input() ikrId: string;
  @Input('environment') environment: IkrEnvironment = 'PROD';
  @Input('showTitle') showTitle = true;
Run Code Online (Sandbox Code Playgroud)

这是我在普通旧 html 中使用它的方式:

<my-element environment="DEV" ikr-id="889fb69f-71a5-4881-8528-0b43a07599f0" show-title="false"></my-element>
Run Code Online (Sandbox Code Playgroud)

但是节目标题没有传递到我的组件中,它总是正确的。

我在这里缺少什么?

谢谢!

angular angular6 angular-elements

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

如何使用 Angular Material 创建折叠菜单?

在 Angular Material 的文档中有一个很酷的功能。带有可以展开/折叠的子类别的菜单。

是否可以使用某些组件创建它?还是我必须自己从头开始?或者,也许您可​​以向我推荐一些可以节省我时间的软件包。

在此处输入图片说明

angular-material angular angular6

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

错误:无法在 angular7 webpack 应用程序中解析 Location: (?) 的所有参数

在本地运行我不使用 angular-cli 创建的 node.js-angular7-webpack4.21 应用程序时,我在浏览器中收到以下错误:

Error: Can't resolve all parameters for Location: (?).
Run Code Online (Sandbox Code Playgroud)

我对这个问题进行了广泛的研究。首先,我没有任何组件、服务或其他命名的“位置”,所以我不知道编译器很难解析哪个实体的参数。其次,我的整个应用程序中只有 4 个服务,没有一个指向彼此,因此这不是循环依赖问题。我也没有忘记将 '@Injectable()' 装饰器正确放置在任一服务文件中。真的,经过三重和八重检查后,这不是问题。

此外,我已经用一个空的 angular6 项目重现了这个问题,该项目由 0 个服务和只有 1 个组件组成,模板中的 div 中只有单词,在这种情况下,我只收到关于“ApplicationModule”而不是“Location”的相同错误。所以即使我没有 webpack 错误,我也认为问题出在我的 webpack 配置中,因此我将把 webpack.config.js 和 tsconfig.js 文件放在下面,希望有人能找到关于它们的一些非常愚蠢的东西。

这是我的 tsconfig.json:

{
  "compilerOptions": {
  "target": "es5",
  "lib": ["dom", "es2017", "es5", "es6", "es7"],
  "outDir": "dist",
  "module": "commonjs",
  "moduleResolution": "node",
  "sourceMap": true,
  "emitDecoratorMetadata": true,
  "experimentalDecorators": true,
  "removeComments": true,
  "noImplicitAny": false,
  "typeRoots": ["types"],
  "types": ["node"]
},
  "exclude": ["node_modules"]
}
Run Code Online (Sandbox Code Playgroud)

这是我的 webpack.config.js:

var webpack = require('webpack');
var helpers …
Run Code Online (Sandbox Code Playgroud)

webpack angular angular6 webpack-4 angular7

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

如何从角度为 6 的数组中删除重复的对象

我正在尝试删除数组中的重复值对象但不起作用...我认为重复函数正在运行但未反映在li列表中。你能找出我必须改变的地方吗?

我的服务文件:

 addComp(Names,c){   
 this.item.push({ name: Names, componentid: c});
 this.uniqueArray = this.removeDuplicates(this.item, "name"); //this line issue
 this.item=this.uniqueArray; //this line issue
 }
Run Code Online (Sandbox Code Playgroud)

typescript angular2-services angular angular5 angular6

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

doc.fromHTML 不是 angular 6 的函数

我想将模板 html 导出为 pdf,我正在尝试使用带有 angular 的 jsPDF lib,但出现此错误 doc.fromHTML is not a function。

import { Component, ViewChild, ElementRef } from '@angular/core';
import * as jsPDF from 'jspdf';
// declare var jsPDF: any;
@Component({
 selector: 'app-root',
 templateUrl: './app.component.html',
 styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'exports-pdf';
@ViewChild('exports') exports: ElementRef;

public print(): void {

const doc = new jsPDF('p', 'pt', 'letter');

const content = this.exports.nativeElement;

const margins = {
  top: 80,
  bottom: 60,
  left: 40,
  width: 522
};
console.log(doc);
setTimeout(() …
Run Code Online (Sandbox Code Playgroud)

export-to-pdf jspdf angular angular6

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

在angular5 / 6中单击按钮时如何从url下载文件

我有一个网址,例如:abc.net/files/test.ino 要求是通过角度为 5 或 6 的按钮单击事件下载 .INO 文件

angular angular5 angular6

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

npm 错误!Fiber@3.1.1 安装脚本失败

在此处输入图片说明

我收到这个错误

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! fibers@3.1.1 install: `node build.js || nodejs build.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the fibers@3.1.1 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.    
npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\yaeesh\AppData\Roaming\npm-cache\_logs\2019-05-07T10_36_05_651Z-debug.log
Run Code Online (Sandbox Code Playgroud)

angular-material angular6

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

如何将角度组件传递给 Nebular 对话服务

我正在尝试使用Nebular组件在 Agnular6 上创建一个 Web 对话框。有两种方法可以做到这一点,第一种是传递<ng-template>引用,例如:

  openAddDialog = (dialogTemplate: TemplateRef<any>) => {
    this.dialogServiceRef = this.dialogService.open(dialogTemplate);
  }
Run Code Online (Sandbox Code Playgroud)

它运行良好。但我需要的是将组件作为参数传递,例如:

dialogService.open(MyComponent);
Run Code Online (Sandbox Code Playgroud)

我的应用程序中有这个结构:

|_ pages
|   |_ pages.module.ts
|   |_ patient
|      |_ patient.module.ts
|      |_ patient.component.ts
|      |_ patient.component.html
|
|_ shared
    |_ shared.module.ts
    |_ components
    |   |_ search-bar.component.ts
    |   |_ search-bar.component.html
    |
    |_ modal-form
        |_ modal-form.component.ts
        |_ modal-from.component.html
Run Code Online (Sandbox Code Playgroud)

我已经添加了ModalFormComponent到declarations,exports和entryComponents共享的模块中。此外,我将它导入到 SearchBar 组件中,并在单击 searchBar 中的按钮时运行此函数:

|_ pages
|   |_ pages.module.ts
|   |_ patient …
Run Code Online (Sandbox Code Playgroud)

dialog typescript angular6 ngx-admin nebular

7
推荐指数
2
解决办法
9962
查看次数