我是Angular,Node和Javascript中的新成員。但是我需要在這些技術上開展一些項目。我的問題是這樣的。我想在我的項目中使用庫i18n-iso-countries。所以我執行了npm install i18n-iso-countries --save
。到現在爲止還挺好。將一些庫導入Angular
在我的組件文件home.component.ts
我導入這樣的:
import * as countries from 'i18n-iso-countries';
和使用這樣,確保庫被加載並工作:
console.log(countries.alpha2ToNumeric("PL"));
console.log(countries.numericToAlpha2(840));
console.log(countries.getName(840, "en"));
console.log(countries.getName("US", "en"));
兩個第一線按預期工作,但最後兩個沒有。我注意到我需要根據文檔註冊區域設置。所以問題是如何導入區域設置?我需要執行
countries.registerLocale(require("i18n-iso-countries/langs/en.json"));
但錯誤是:
Cannot find name 'require'.
我也試着像不同的進口形式:
import locale = require('i18n-iso-countries/lang/en.json');
import * as locale from 'i18n-iso-countries/langs/en';
但沒有成功。也許我錯過了一些微不足道的東西,但我並沒有意識到這一點。我在打字稿手冊中閱讀Modules
一章,但它不回答我的問題。也許你們會幫忙。
我package.json
:
{
"name": "my-app",
"version": "0.1.1",
"license": "MIT",
"scripts": {
"ng": "ng",
"start": "ng serve --proxy-config proxy.conf.json",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "^4.4.5",
"@angular/cdk": "^2.0.0-beta.12",
"@angular/common": "^4.4.5",
"@angular/compiler": "^4.4.5",
"@angular/core": "^4.4.5",
"@angular/flex-layout": "2.0.0-beta.8",
"@angular/forms": "^4.4.5",
"@angular/http": "^4.4.5",
"@angular/material": "^2.0.0-beta.12",
"@angular/platform-browser": "^4.4.5",
"@angular/platform-browser-dynamic": "^4.4.5",
"@angular/platform-server": "^4.4.5",
"@angular/router": "^4.4.5",
"ag-grid": "^13.3.1",
"ag-grid-angular": "^13.3.0",
"core-js": "2.4.1",
"hammerjs": "2.0.8",
"i18n-iso-countries": "^3.0.0",
"rxjs": "^5.4.1",
"zone.js": "0.8.12"
},
"devDependencies": {
"@angular/cli": "1.1.3",
"@angular/compiler-cli": "^4.4.5",
"@types/hammerjs": "2.0.34",
"@types/jasmine": "2.5.45",
"@types/node": "6.0.78",
"codelyzer": "3.0.1",
"jasmine-core": "2.6.4",
"jasmine-spec-reporter": "4.1.1",
"karma": "1.7.0",
"karma-chrome-launcher": "2.1.1",
"karma-cli": "1.0.1",
"karma-coverage-istanbul-reporter": "1.3.0",
"karma-jasmine": "1.1.0",
"karma-jasmine-html-reporter": "0.2.2",
"protractor": "5.1.2",
"ts-node": "3.0.6",
"tslint": "5.3.2",
"typescript": "^2.3.4"
}
}
錯誤是'無法找到名稱'require'。' –
對於找不到名稱'require'錯誤請參閱 - https://stackoverflow.com/a/35961176/6118431 –
謝謝。有用!我只需要在我的「tsconfig.app.json」文件中將「節點」添加到「types」數組中。 –