2016-10-22 82 views
0

我是新來的,我正在學習一個教程。我的代碼是從正在工作的教程視頻逐字記錄的,但我的代碼不是。我得到錯誤「無法找到名爲‘無極’,我想不通爲什麼。有沒有人有,爲什麼我得到這個錯誤的想法?找不到名字Promise Angular2

import {Control} from 'angular2/common' 

export class UsernameValidators{ 
static shouldBeUnique(control: Control){ 

return new Promise ((resolve) => { 

setTimeout(function(){ 
    if(control.value == "andy") 
    resolve({shouldBeUnique: true}) 
else 
resolve(null); 
}, 1000); 

}); 

} 
    static cannotContainSpace(control: Control){ 
if (control.value.indexOf(' ') >=0) 
return {cannotContainSpace: true}; 
return null; 

    } 
} 
+0

你是在你的tsconfig中定位ES6嗎? – NPhillips

回答

0

你似乎是一個測試版。Angular2版本以下的答案必須假設您正在使用:

"@angular/common": "2.0.0", 
    "@angular/compiler": "2.0.0", 
    "@angular/core": "2.0.0", 
    "@angular/forms": "2.0.0", 
    "@angular/http": "2.0.0", 
    "@angular/platform-browser": "2.0.0", 

您使用的角度2.0.0或更高版本提供,你可以做到以下幾點:

import { FormControl } from "@angular/forms"; 
import { Promise } from "./path/to/node_modules/es6-promise"; 

export class UsernameValidators { 
    static shouldBeUnique(control: FormControl) { 

     return new Promise((resolve) => { 

      setTimeout(function() { 
       if (control.value === "andy") 
        resolve({ shouldBeUnique: true }); 
       else 
        resolve(null); 
      }, 1000); 

     }); 

    } 
    static cannotContainSpace(control: FormControl) { 
     if (control.value.indexOf(" ") >= 0) 
      return { cannotContainSpace: true }; 
     return null; 

    } 
} 
+0

我認爲你是正確的測試版。這將解釋它。感謝您的意見。 – user6680

0

如果你是經驗找不到名稱'承諾',主要是你e更改了基本設置中的某些文件,或者您可能未成功完成節點包安裝。 Angular2不再使用typings包。

解決方案:

  • 克隆以下庫https://github.com/angular/quickstart
  • 運行npm install在項目目錄中,並確保它完成成功
  • 現在複製和配置你的應用程序的項目文件夾到克隆的新文件夾 現在這將完美地工作。