TypeScript错误:(1,2):错误TS1109:预期的表达式

时间:2016-06-29 16:27:08

标签: typescript angular browserify

当让我的业力吞噬任务运行时,我正在发现这个错误。我尝试了很多,但不能让它消失。任何的想法?错误实际上是黑色的,我不明白为什么这里的第一行是错误的:

控制台输出

gulp test
[17:42:29] Using gulpfile ~/devel/saveup/gulpfile.js
[17:42:29] Starting 'test'...
29 06 2016 17:42:31.863:ERROR [framework.browserify]: bundle error
29 06 2016 17:42:31.864:ERROR [framework.browserify]: TypeScript error: http-service.ts(1,2): Error TS1109: Expression expected.
29 06 2016 17:42:31.871:INFO [karma]: Karma v0.13.22 server started at http://localhost:9876/
29 06 2016 17:42:31.875:INFO [launcher]: Starting browser Chrome
29 06 2016 17:42:33.188:INFO [Chrome 51.0.2704 (Mac OS X 10.11.4)]: Connected on socket /#6VLMDrA1BpjZDN2EAAAA with id 30571335
29 06 2016 17:42:33.303:ERROR [Chrome 51.0.2704 (Mac OS X 10.11.4)]: Uncaught Error: bundle error (see logs)
at http://localhost:9876/absolute/var/folders/vy/18sb1wqs60g734bhr75cw9_r0000gn/T/166dd27dfe559208855388fa0d9198f1.browserify?59c06c9eba4f4adc27dc229cfaebba395247ee16:1
Chrome 51.0.2704 (Mac OS X 10.11.4) ERROR
  Uncaught Error: bundle error (see logs)
  at /var/folders/vy/18sb1wqs60g734bhr75cw9_r0000gn/T/166dd27dfe559208855388fa0d9198f1.browserify:1

Suites and tests results:

Browser results:

我的服务

import {Injectable} from '@angular/core';
import { Http, Response } from '@angular/http';


@Injectable()

export class HttpService {

  responseData: any;

  constructor(private http:Http) {    
  }

  public getRequest(){
      this.http.get('http://httpbin.org/get')
      .map(res => res.text())
      .subscribe(
        data => this.responseData = data,
        err => this.logError(err),
        () => console.log('Get Request Complete. Data:' + this.responseData)
      );
  }

  public postRequest() {  

      this.http.post('http://httpbin.org/post',
      JSON.stringify({ a: 123, b: 345}))
      .map(response => response.json())
      .subscribe(
        data => this.responseData = data,
        err => this.logError(err),
        () => console.log('Post Request Complete. Data:' + this.responseData)
      );
  }

  public logError(err) {
    console.error('There was an error: ' + err);
  }
}

我的测试:

import {beforeEachProviders, beforeEach, it, describe, expect, inject} from '@angular/core/testing';
import {HttpService} from '../../providers/http-service/http-service';
import {NavController} from 'ionic-angular';
import {HTTP_PROVIDERS, Http, XHRBackend, ResponseOptions, Response} from '@angular/http';
import {MockBackend,MockConnection} from '@angular/http/testing';
import {provide} from '@angular/core';
import {BrowserDomAdapter} from '@angular/platform-browser/src/browser/browser_adapter';


describe('Http Service Test', () => {
    BrowserDomAdapter.makeCurrent();

      beforeEachProviders(() => {
        return [
            HTTP_PROVIDERS,
            provide(XHRBackend, { useClass: MockBackend }),
            HttpService
        ];
    });

    it('should get blogs', inject([XHRBackend, HttpService], (mockBackend, httpService) => {
        mockBackend.connections.subscribe(
        (connection: MockConnection) => {
            connection.mockRespond(new Response(
            new ResponseOptions({
                body: [
                    {
                    id: 26,
                    contentRendered: "<p><b>Hi there</b></p>",
                    contentMarkdown: "*Hi there*"
                    }]
                }
            )));
      });

    httpService.getRequest()
     .subscribe((blogs: any) => {
      expect(blogs.length).toBe(1);
      expect(blogs[0].id).toBe(26);
    });
  }));
});

更新

当我删除我的服务时,仍会出现错误信息,这让我觉得这个问题与gulp任务有关,而且我们没有得到新的转换。继承我的Gulpfile:

var gulp = require('gulp'),
    gulpWatch = require('gulp-watch'),
    del = require('del'),
    runSequence = require('run-sequence'),
    argv = process.argv;

var Server = require('karma').Server;

/**
 * Ionic hooks
 * Add ':before' or ':after' to any Ionic project command name to run the specified
 * tasks before or after the command.
 */
gulp.task('serve:before', ['watch']);
gulp.task('emulate:before', ['build']);
gulp.task('deploy:before', ['build']);
gulp.task('build:before', ['build']);

// we want to 'watch' when livereloading
var shouldWatch = argv.indexOf('-l') > -1 || argv.indexOf('--livereload') > -1;
gulp.task('run:before', [shouldWatch ? 'watch' : 'build']);

/**
 * Ionic Gulp tasks, for more information on each see
 * https://github.com/driftyco/ionic-gulp-tasks
 *
 * Using these will allow you to stay up to date if the default Ionic 2 build
 * changes, but you are of course welcome (and encouraged) to customize your
 * build however you see fit.
 */
var buildBrowserify = require('ionic-gulp-browserify-typescript');
var buildSass = require('ionic-gulp-sass-build');
var copyHTML = require('ionic-gulp-html-copy');
var copyFonts = require('ionic-gulp-fonts-copy');
var copyScripts = require('ionic-gulp-scripts-copy');

var isRelease = argv.indexOf('--release') > -1;

gulp.task('watch', ['clean'], function(done){
  runSequence(
    ['sass', 'html', 'fonts', 'scripts'],
    function(){
      gulpWatch('app/**/*.scss', function(){ gulp.start('sass'); });
      gulpWatch('app/**/*.html', function(){ gulp.start('html'); });
      buildBrowserify({ watch: true }).on('end', done);
    }
  );
});

gulp.task('build', ['clean'], function(done){
  runSequence(
    ['sass', 'html', 'fonts', 'scripts'],
    function(){
      buildBrowserify({
        minify: isRelease,
        browserifyOptions: {
          debug: !isRelease
        },
        uglifyOptions: {
          mangle: false
        }
      }).on('end', done);
    }
  );
});


/**
 * Run test once and exit
 */
gulp.task('test', function (done) {

    var karmaServer = new Server({
        configFile: __dirname + '/karma.conf.js',
        singleRun: true
    }, function (exitCode) {
        done();
        process.exit(exitCode);
    }).start();
});



/**
 * Run tests in chrome browser
 */
gulp.task('test:chrome', function (done) {
    var karmaServer = new Server({
        configFile: __dirname + '/karma.conf.js',
        browsers: ['Chrome']
    }, function (exitCode) {
        done();
        process.exit(exitCode);
    }).start();
});

/**
 * Watch for file changes and re-run tests on each change
 */
gulp.task('tdd', function (done) {
    var karmaServer = new Server({
        configFile: __dirname + '/karma.conf.js'
    }, function () {
        done();
    }).start();
});

gulp.task('sass', buildSass);
gulp.task('html', copyHTML);
gulp.task('fonts', copyFonts);
gulp.task('scripts', copyScripts);
gulp.task('clean', function(){
  return del('www/build');
});

1 个答案:

答案 0 :(得分:0)

我的根工作文件夹中有一个重复的http-service.ts文件。在该文件内是一个错误。删除了重复的错误文件,它工作正常。

我希望TS Transpiler Browserify能告诉我该文件的位置(绝对路径)。