2016-03-02 81 views
26

我正在構建一個節點應用程序,並且在.js中的每個文件中用於執行此操作,以便在各種包中執行此操作。不能重新聲明塊作用域變量(typescript)

let co = require("co"); 

但要

enter image description here

等,所以使用打字稿似乎只能有一個這樣的聲明在整個項目/需要? 我對此感到困惑,因爲我認爲let被限制在當前文件中。

我剛剛有一個項目正在工作,但重構後我現在得到這些錯誤到處都是。

有人可以解釋一下嗎?

+0

這個怎麼樣 - let co_module = require(「co」);我遇到了類似的情況,這解決了錯誤。 – tyrion

+0

@tyrion如果你讓這個答案,我會給你一個upvote。 – phreed

+0

這似乎是一個打字稿錯誤。我在這裏做了一個小例子:https://gist.github.com/rjmunro/428ec644b6e53a499ca3a5ba8de2edc7 – rjmunro

回答

22

let用於聲明本地變量存在於block scopes而不是函數作用域。

導入外部模塊的最新首選方法是ES6 syntax反正,其中包含沒有明確的分配:

import * as co from "./co" 
+7

這給出了「無法找到模塊co」。我也嘗試過'typings install co',它在註冊表中給出'Unable to find「co。任何其他想法? – dcsan

+0

我遇到了與@dcsan相同的問題,它說它找不到該模塊,即使我清楚地知道它已安裝npm。 –

+0

這可能是由相對路徑引起的。我不熟悉NodeJS模塊管理,但在RequireJS中,如果模塊被相對引用,則當前文件夾必須明確表示。也就是說,如果co.ts位於同一文件夾(或編譯後的輸出,co.js)中,則使用'。/ co'而不是'co'。 –

1

我得到了同樣的問題,我的解決辦法是這樣的:

// *./module1/module1.ts* 
export module Module1 { 
    export class Module1{ 
     greating(){ return 'hey from Module1'} 
    } 
} 


// *./module2/module2.ts* 
import {Module1} from './../module1/module1'; 

export module Module2{ 
    export class Module2{ 
     greating(){ 
      let m1 = new Module1.Module1() 
      return 'hey from Module2 + and from loaded Model1: '+ m1.greating(); 
     } 
    } 
} 

現在我們可以在服務器端使用它:

// *./server.ts* 
/// <reference path="./typings/node/node.d.ts"/> 
import {Module2} from './module2/module2'; 

export module Server { 
    export class Server{ 
     greating(){ 
      let m2 = new Module2.Module2(); 
      return "hello from server & loaded modules: " + m2.greating(); 
     } 
    } 
} 

exports.Server = Server; 

// ./app.js 
var Server = require('./server').Server.Server; 
var server = new Server(); 
console.log(server.greating()); 

並在客戶方太:

// *./public/javscripts/index/index.ts* 

import {Module2} from './../../../module2/module2'; 

document.body.onload = function(){ 
    let m2 = new Module2.Module2(); 
    alert(m2.greating()); 
} 

// ./views/index.jade 
extends layout 

block content 
    h1= title 
    p Welcome to #{title} 
    script(src='main.js') 
    // 
    the main.js-file created by gulp-task 'browserify' below in the gulpfile.js 

,當然,一大口文件所有這一切:

// *./gulpfile.js* 
var gulp = require('gulp'), 
    ts = require('gulp-typescript'), 
    runSequence = require('run-sequence'), 
    browserify = require('gulp-browserify'), 
    rename = require('gulp-rename'); 

gulp.task('default', function(callback) { 

    gulp.task('ts1', function() { 
     return gulp.src(['./module1/module1.ts']) 
      .pipe(ts()) 
      .pipe(gulp.dest('./module1')) 
    }); 

    gulp.task('ts2', function() { 
     return gulp.src(['./module2/module2.ts']) 
      .pipe(ts()) 
      .pipe(gulp.dest('./module2')) 
    }); 

    gulp.task('ts3', function() { 
     return gulp.src(['./public/javascripts/index/index.ts']) 
      .pipe(ts()) 
      .pipe(gulp.dest('./public/javascripts/index')) 
    }); 

    gulp.task('browserify', function() { 
     return gulp.src('./public/javascripts/index/index.js', { read: false }) 
      .pipe(browserify({ 
       insertGlobals: true 
      })) 
      .pipe(rename('main.js')) 
      .pipe(gulp.dest('./public/javascripts/')) 
    }); 

    runSequence('ts1', 'ts2', 'ts3', 'browserify', callback); 
}) 

更新。 當然,分開編譯打印稿文件並不是必需的。 runSequence(['ts1', 'ts2', 'ts3'], 'browserify', callback)作品完美。

+0

爲了防止任何人通過該gulp文件的冗長性和重複性推遲TypeScript,沒有人這樣做。 –

0

我升級

一飲而盡,打字稿3.0.2→3.1.0

把它回到3.0.2固定它

3

使用IIFE(Immediately Invoked Function Expression),得到這個錯誤IIFE

(function() { 
    all your code is here... 

})(); 
+0

簡單。完美的作品。謝謝! (在我的例子中,爲了在Angular 5項目中使用Cucumber測試,我聲明瞭'const expect = chai.expect;')。 –