加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

TypeScript / Angular2:rxjs错误TS1138,TS1005,TS1128

发布时间:2020-12-17 17:08:15 所属栏目:安全 来源:网络整理
导读:我正在关注Angular 2教程,当我运行gulp dev时,我在终端中遇到这些错误.我正在使用Typescript 2.0.3: node_modules/rxjs/operator/toPromise.d.ts(8,38): Error TS1138: Parameter declaration expected.node_modules/rxjs/operator/toPromise.d.ts(8,42): E
我正在关注Angular 2教程,当我运行gulp dev时,我在终端中遇到这些错误.我正在使用Typescript 2.0.3:

node_modules/rxjs/operator/toPromise.d.ts(8,38): Error TS1138: Parameter declaration expected.
node_modules/rxjs/operator/toPromise.d.ts(8,42): Error TS1005: ';' expected.
node_modules/rxjs/operator/toPromise.d.ts(8,57): Error TS1005: '(' expected.
node_modules/rxjs/operator/toPromise.d.ts(8,58): Error TS1005: ';' expected.
node_modules/rxjs/operator/toPromise.d.ts(8,70): Error TS1005: '(' expected.
node_modules/rxjs/operator/toPromise.d.ts(9,38): Error TS1138: Parameter declaration expected.
node_modules/rxjs/operator/toPromise.d.ts(9,42): Error TS1005: ';' expected.
node_modules/rxjs/operator/toPromise.d.ts(9,57): Error TS1109: Expression expected.
node_modules/rxjs/operator/toPromise.d.ts(9,70): Error TS1005: ';' expected.
node_modules/rxjs/operator/toPromise.d.ts(9,86): Error TS1005: ';' expected.
node_modules/rxjs/operator/toPromise.d.ts(9,87): Error TS1128: Declaration or statement expected.
node_modules/rxjs/operator/toPromise.d.ts(9,99): Error TS1005: '(' expected.

在浏览器中,角度应用程序不会加载.有人可以解释发生了什么吗?

编辑:这些是来自node_modules / rxjs / operator / toPromise.d.ts的第8行和第9行

export declare function toPromise<T>(this: Observable<T>): Promise<T>;
export declare function toPromise<T>(this: Observable<T>,PromiseCtor: typeof Promise): Promise<T>;

这是唯一使用toPromise方法的文件:

weather.service.ts

import {Injectable} from "@angular/core";
import {Http} from "@angular/http";
import "rxjs/add/operator/toPromise";

interface WeatherApiResponse {
    query: {
        count: number;
        created: string;
        lang: string;
        results: {
            channel: {
                item: {
                    condition: {
                        code: string;
                        temp: string
                    }
                }
            }
        }
    };
}

export interface WeatherInformation {
    city: string;
    code: number;
    temperature: number;
}

export interface City {
    name: string;
    imageSrc: string;
    woeId: string;
}

@Injectable()
export class WeatherService {
    cities = [
        {name: "Bogota",imageSrc: "img/bogota.jpg",woeId: "368148"}
    ];

    constructor(private http: Http) {}

    getWeather(woeId: string) {
        const url = this.generateWeatherUrl(woeId);
        return this.http.get(url).toPromise()
            .then(x => {
                const apiResponse = x.json() as WeatherApiResponse;
                const weather = apiResponse.query.results.channel.item.condition;
                return {
                    city: this.getCityName(woeId),code: Number(weather.code),temperature: Number(weather.temp)
                } as WeatherInformation;
            });
    }

    private generateWeatherUrl(woeId: string) {
        return `http://localhost:8001/api/weather/${woeId}`;
    }

    private getCityName(woeId: string) {
        const matches = this.cities.filter(x => x.woeId === woeId);
        return matches.length === 1 ? matches[0].name : undefined;
    }
}

这是在weather.service.ts中调用getWeather()方法的函数

weather.component.ts

ngOnInit() {
    this.route.params.subscribe(params => {
        const woeId = params["woeId"];
        this.weatherService.getWeather(woeId)
            .then(x => this.weather = x);
    });
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es5","module": "commonjs","moduleResolution": "node","sourceMap": true,"emitDecoratorMetadata": true,"experimentalDecorators": true,"removeComments": true,"noImplicitAny": true
  },"exclude": [
    "node_modules","typings/main","typings/main.d.ts"
  ]
}

解决方法

对于那些跟随Levi Botelho的Packt Angular 2项目并提出的gulp配置并且天气应用程序有相同问题的人,你可以通过更新你的依赖关系来解决它(@angular).

它适用于以下依赖项:

"dependencies": {
    "@angular/common": "~2.1.1","@angular/compiler": "~2.1.1","@angular/core": "~2.1.1","@angular/forms": "~2.1.1","@angular/http": "~2.1.1","@angular/platform-browser": "~2.1.1","@angular/platform-browser-dynamic": "~2.1.1","@angular/router": "~3.1.1","@angular/upgrade": "~2.1.1","angular-in-memory-web-api": "~0.1.13","core-js": "^2.4.1","reflect-metadata": "^0.1.8","systemjs": "0.19.39","zone.js": "^0.6.25","compression": "^1.6.2","es6-shim": "^0.35.1","express": "^4.14.0","rxjs": "5.0.0-beta.12"
  }

检查gulp.js文件,因为视频中的文件与您可以下载的代码中的文件不同…

const argv = require("yargs").argv;
const browserify = require("browserify");
const browserSync = require("browser-sync").create();
const buffer = require("vinyl-buffer");
const cache = require("gulp-cached");
const concat = require("gulp-concat");
const exec = require('child_process').exec;
const gulp = require("gulp");
const gulpIf = require("gulp-if");
const htmlmin = require("gulp-htmlmin");
const inject = require("gulp-inject");
const sass = require("gulp-sass");
const source = require("vinyl-source-stream");
const sourcemaps = require("gulp-sourcemaps");
const tsify = require("tsify");
const tslint = require("gulp-tslint");
const uglify = require("gulp-uglify");
const watchify = require("watchify");

const rootBuildPath = "./dist/";
const cssBundleName = "site.css";
const cssBundleBuildPath = rootBuildPath + "/css/" + cssBundleName;
const cssSource = "./scss/site.scss";
const cssBuildPath = rootBuildPath + "css";
const fontSource = "./bower_components/weather-icons/font/**";
const fontBuildPath = rootBuildPath + "font";
const htmlSource = "./index.html";
const imgSource = "./img/*.jpg";
const imgBuildPath = "./dist/img";
const jsBundleName = "bundle.js";
const jsBundleBuildDirectory = rootBuildPath + "app";
const jsBundleBuildPath = jsBundleBuildDirectory + "/bundle.js";
const jsSourceDirectory = "./app";
const jsEntryPoint = jsSourceDirectory + "/main.ts";
const jsSource = jsSourceDirectory + "/**/*.ts";
const libSource = [
    "node_modules/es6-shim/es6-shim.min.js","node_modules/es6-shim/es6-shim.map","node_modules/zone.js/dist/zone.min.js","node_modules/reflect-metadata/Reflect.js","node_modules/reflect-metadata/Reflect.js.map",];
const libBuildPath = rootBuildPath + "/lib";
const serverSource = "./server.js";
const templatesSource = "./app/**/*.html";
const templatesBuildPath = rootBuildPath + "app";
const typings = "./typings/index.d.ts";

function logError(err) {
    console.error(err.message);
    this.emit("end");
}

function shouldMinify() {
    return argv.release;
}

gulp.task("lint",function () {
    gulp.src(jsSource)
        .pipe(tslint({
            formatter: "verbose"
        }))
        .pipe(tslint.report())
        .on("error",logError);
});

var browserifyOptions = {
    debug: !shouldMinify(),entries: [jsEntryPoint,typings],plugin: [tsify]
};

if (argv.watch) {
    browserifyOptions.cache = {};
    browserifyOptions.packageCache = {};
    browserifyOptions.plugin.push(watchify);
}

var browserifyInstance = browserify(browserifyOptions);

gulp.task("js",["lint"],function () {
    return browserifyInstance
        .bundle()
        .on("error",logError)
        .pipe(source(jsBundleName))
        .pipe(buffer())
        .pipe(sourcemaps.init({loadMaps: true}))
        .pipe(gulpIf(shouldMinify(),uglify().on("error",logError)))
        .pipe(sourcemaps.write("./"))
        .pipe(gulp.dest(jsBundleBuildDirectory));
});

gulp.task("css",function () {
    return gulp.src(cssSource)
        .pipe(concat(cssBundleName))
        .pipe(sourcemaps.init())
        .pipe(sass({outputStyle: shouldMinify() ? "compressed" : "nested"}))
        .pipe(sourcemaps.write("."))
        .pipe(gulp.dest(cssBuildPath))
        .pipe(browserSync.stream());
});

gulp.task("font",function () {
    return gulp.src(fontSource)
        .pipe(gulp.dest(fontBuildPath));
})

gulp.task("img",function () {
    return gulp.src(imgSource)
        .pipe(gulp.dest(imgBuildPath));
});

gulp.task("lib",function () {
    return gulp.src(libSource)
        .pipe(gulp.dest(libBuildPath));
});

gulp.task("templates",function () {
    return gulp.src(templatesSource)
        .pipe(gulp.dest(templatesBuildPath));
});

gulp.task("html",["js","css"],function () {
    return gulp.src(htmlSource)
        .pipe(inject(gulp.src([jsBundleBuildPath,cssBundleBuildPath],{read: false}),{ignorePath: "dist"}))
        .pipe(gulpIf(shouldMinify(),htmlmin({collapseWhitespace: true})))
        .pipe(gulp.dest(rootBuildPath));
});

gulp.task("server",function () {
    return gulp.src(serverSource)
        .pipe(gulp.dest(rootBuildPath));
})

gulp.task("default",["font","html","img","lib","templates","server"]);

gulp.task("html-watch",["html"],() => browserSync.reload());
gulp.task("js-watch",["js"],() => browserSync.reload());
gulp.task("templates-watch",["templates"],() => browserSync.reload());

gulp.task("dev",["default"],function () {
    exec("node dist/server",function (err,stdout,stderr) {
        console.log(stdout);
        console.error(stderr);
    });

    gulp.watch(htmlSource,["html-watch"]);
    gulp.watch(jsSource,["js-watch"]);
    gulp.watch(templatesSource,["templates-watch"]);
    gulp.watch(cssSource,["css"]);

    browserSync.init({
        port: 8001,proxy: "http://localhost:3011"
    });
});

如果您还有其他问题,请下载代码以及组件/服务与视频中的内容不同.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读