Observable.of 不是正确导入的函数
Observable.of is not a function with correct import
我在 angular 6 中为 observable 导入 "of" 方法时遇到问题,即使是全新的项目安装,所有更新的包,我仍然会收到错误消息。
所以,我做了一个小项目
这是提供的服务
import { Injectable } from '@angular/core';
import { TableRow } from './table-row';
import { Observable, of } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class TestTableService {
private testData : TableRow[] = [
{id:1, name:'pi', value:3.14},
{id:2, name:'euler', value:2.71},
{id:3, name:'Feigenbaum constant', value:2.50290},
{id:4, name:'Mills constant', value:1.30637},
{id:5, name:'one', value:1},
{id:6, name:'square root of 2', value:1.41421}
];
constructor() { }
public getData() : Observable<TableRow[]>
{
return Observable.of(this.testData);
}
}
这是使用它的组件:
import { Component, OnInit } from '@angular/core';
import { TestTableService } from './../test-table.service';
import { TableRow } from './../table-row';
@Component({
selector: 'app-test-table',
templateUrl: './test-table.component.html',
styleUrls: ['./test-table.component.css']
})
export class TestTableComponent implements OnInit {
tableData : TableRow[] = [];
constructor(private testTable : TestTableService) { }
ngOnInit() {
this.testTable.getData()
.subscribe(table => this.tableData = table);
}
}
注意导入与其他问题完全相同
项目package.json:
{
"name": "angular-table-test",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "^6.0.2",
"@angular/cdk": "^6.1.0",
"@angular/common": "^6.0.2",
"@angular/compiler": "^6.0.2",
"@angular/core": "^6.0.2",
"@angular/forms": "^6.0.2",
"@angular/http": "^6.0.2",
"@angular/material": "^6.1.0",
"@angular/platform-browser": "^6.0.2",
"@angular/platform-browser-dynamic": "^6.0.2",
"@angular/router": "^6.0.2",
"core-js": "^2.5.4",
"hammerjs": "^2.0.8",
"rxjs": "^6.0.0",
"zone.js": "^0.8.26"
},
"devDependencies": {
"@angular/compiler-cli": "^6.0.2",
"@angular-devkit/build-angular": "~0.6.3",
"typescript": "~2.7.2",
"@angular/cli": "~6.0.3",
"@angular/language-service": "^6.0.2",
"@types/jasmine": "~2.8.6",
"@types/jasminewd2": "~2.0.3",
"@types/node": "~8.9.4",
"codelyzer": "~4.2.1",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~1.7.1",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~1.4.2",
"karma-jasmine": "~1.1.1",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.3.0",
"ts-node": "~5.0.1",
"tslint": "~5.9.1"
}
}
可能是什么问题?
在 rxjs
6.x 中,of
不再是 Observable
上的静态函数。
因此,您只需调用 of(…)
.
而不是调用 Observable.of(…)
有关从 rxjs
5.x 到 6+ 的迁移信息,请参阅 here。
在 Rxjs 6(或 angular6)中,导入路径和 Observable 创建方法已经改变。例如-
Observable.fromEvent
现在变成了 fromEvent
。
Observable.of
已变为 of
,与 interval
类似(之前是 Observable.interval
)。它们不再是 Observable class 的静态方法。
另一个重大变化是现在必须使用 pipe
运算符。
例如,为了在 Observable 上使用 map
运算符然后在其上使用 subscribe
我们现在必须遵循以下语法 -
Observable.pipe(map((val) => ...)).subscribe();
我们可以在pipe
里面添加多个逗号分隔的运算符,从左到右依次执行。
查看这些链接以获得更多参考:
https://github.com/ReactiveX/rxjs/blob/master/CHANGELOG.md
https://www.academind.com/learn/javascript/rxjs-6-what-changed/
我在 angular 6 中为 observable 导入 "of" 方法时遇到问题,即使是全新的项目安装,所有更新的包,我仍然会收到错误消息。 所以,我做了一个小项目
这是提供的服务
import { Injectable } from '@angular/core';
import { TableRow } from './table-row';
import { Observable, of } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class TestTableService {
private testData : TableRow[] = [
{id:1, name:'pi', value:3.14},
{id:2, name:'euler', value:2.71},
{id:3, name:'Feigenbaum constant', value:2.50290},
{id:4, name:'Mills constant', value:1.30637},
{id:5, name:'one', value:1},
{id:6, name:'square root of 2', value:1.41421}
];
constructor() { }
public getData() : Observable<TableRow[]>
{
return Observable.of(this.testData);
}
}
这是使用它的组件:
import { Component, OnInit } from '@angular/core';
import { TestTableService } from './../test-table.service';
import { TableRow } from './../table-row';
@Component({
selector: 'app-test-table',
templateUrl: './test-table.component.html',
styleUrls: ['./test-table.component.css']
})
export class TestTableComponent implements OnInit {
tableData : TableRow[] = [];
constructor(private testTable : TestTableService) { }
ngOnInit() {
this.testTable.getData()
.subscribe(table => this.tableData = table);
}
}
注意导入与其他问题完全相同
项目package.json:
{
"name": "angular-table-test",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "^6.0.2",
"@angular/cdk": "^6.1.0",
"@angular/common": "^6.0.2",
"@angular/compiler": "^6.0.2",
"@angular/core": "^6.0.2",
"@angular/forms": "^6.0.2",
"@angular/http": "^6.0.2",
"@angular/material": "^6.1.0",
"@angular/platform-browser": "^6.0.2",
"@angular/platform-browser-dynamic": "^6.0.2",
"@angular/router": "^6.0.2",
"core-js": "^2.5.4",
"hammerjs": "^2.0.8",
"rxjs": "^6.0.0",
"zone.js": "^0.8.26"
},
"devDependencies": {
"@angular/compiler-cli": "^6.0.2",
"@angular-devkit/build-angular": "~0.6.3",
"typescript": "~2.7.2",
"@angular/cli": "~6.0.3",
"@angular/language-service": "^6.0.2",
"@types/jasmine": "~2.8.6",
"@types/jasminewd2": "~2.0.3",
"@types/node": "~8.9.4",
"codelyzer": "~4.2.1",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~1.7.1",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~1.4.2",
"karma-jasmine": "~1.1.1",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.3.0",
"ts-node": "~5.0.1",
"tslint": "~5.9.1"
}
}
可能是什么问题?
在 rxjs
6.x 中,of
不再是 Observable
上的静态函数。
因此,您只需调用 of(…)
.
Observable.of(…)
有关从 rxjs
5.x 到 6+ 的迁移信息,请参阅 here。
在 Rxjs 6(或 angular6)中,导入路径和 Observable 创建方法已经改变。例如-
Observable.fromEvent
现在变成了 fromEvent
。
Observable.of
已变为 of
,与 interval
类似(之前是 Observable.interval
)。它们不再是 Observable class 的静态方法。
另一个重大变化是现在必须使用 pipe
运算符。
例如,为了在 Observable 上使用 map
运算符然后在其上使用 subscribe
我们现在必须遵循以下语法 -
Observable.pipe(map((val) => ...)).subscribe();
我们可以在pipe
里面添加多个逗号分隔的运算符,从左到右依次执行。
查看这些链接以获得更多参考:
https://github.com/ReactiveX/rxjs/blob/master/CHANGELOG.md
https://www.academind.com/learn/javascript/rxjs-6-what-changed/