我正在关注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
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;
}
}
这是调用getWeather()
weather.service.ts
方法的函数
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"
]
}
答案 0 :(得分:1)
对于Levi Botelho关于Packt Angular 2 Projects 以及 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"
});
});
如果您还有其他问题,请下载代码以及组件/服务与视频中的内容不同。
答案 1 :(得分:0)
原来我需要将.ts转换为.js,我假设Visual Studio Code自动为我做了这个。对于遇到相同问题的人,请转到this site并按照步骤2进行操作。
第2步:创建tasks.json
下一步是设置任务配置。要做到这一点,打开 使用⇧⌘P命令选项板并在“配置任务运行器”中键入,按 输入以选择它。 ...
选择TypeScript - tsconfig.json。这将创建一个
tasks.json
文件 在工作区.vscode
文件夹中。tasks.json文件的内容如下所示:
{ // See http://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "0.1.0", "command": "tsc", "isShellCommand": true, "args": ["-p", "."], "showOutput": "silent", "problemMatcher": "$tsc" }
在封面下,我们将
tsc
解释为外部任务运行者 只展示一个任务:将TypeScript文件编译成 JavaScript文件。我们运行的命令是:tsc -p .