모 4:"을 찾을 수 없는 이름'야'

나는 응용 프로그램을 구축하는을 가진각 4웹팩.

내가 다음 중 하나에서의 구성 요소:

ngOnInit() {
    require('/assets/js/regular-expresions.js');
}

려고 시도하면 컴파일하려,나를 얻을:

에서의 오류 C:/SRC/Sandbox/eat-sleep-code.com/src/app/content.component.ts (21,9): 을 찾을 수 없는 이름'야'.

내가 실행npm@형/노드-저장-dev고 업데이트 내 tsconfig.json 을 다음과 같다:

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "baseUrl": "src",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "types": [
      "node"
    ],
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2016",
      "dom"
    ]
  }
}

그러나 아아,같은 오류가 점점 발생합니다. 어떤 아이디어가?


여기에 제 패키지입니다.json:

{
  "name": "eat-sleep-code.com",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^4.0.1",
    "@angular/common": "^4.0.0",
    "@angular/compiler": "^4.0.0",
    "@angular/core": "^4.0.0",
    "@angular/forms": "^4.0.0",
    "@angular/http": "^4.0.0",
    "@angular/material": "^2.0.0-beta.3",
    "@angular/platform-browser": "^4.0.0",
    "@angular/platform-browser-dynamic": "^4.0.0",
    "@angular/router": "^4.0.0",
    "@types/node": "^6.0.69",
    "angularfire2": "^2.0.0-beta.8",
    "core-js": "^2.4.1",
    "firebase": "^3.7.8",
    "jquery": "^3.2.1",
    "jquery-validation": "^1.16.0",
    "ng2-gist": "^1.0.0",
    "promise-polyfill": "^6.0.2",
    "requirejs": "^2.3.3",
    "rxjs": "^5.1.0",
    "typescript": "^2.2.2",
    "web-animations-js": "^2.2.4",
    "webpack": "^2.4.1",
    "zone.js": "^0.8.4"
  },
  "devDependencies": {
    "@angular/cli": "1.0.0",
    "@angular/compiler-cli": "^4.0.0",
    "@types/jasmine": "2.5.38",
    "@types/node": "^6.0.69",
    "codelyzer": "~2.0.0",
    "jasmine-core": "~2.5.2",
    "jasmine-spec-reporter": "~3.2.0",
    "karma": "~1.4.1",
    "karma-chrome-launcher": "~2.0.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^0.2.0",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.0",
    "ts-node": "~2.0.0",
    "tslint": "~4.5.0",
    "typescript": "^2.2.2",
    "webpack": "^2.4.1"
  }
}
질문에 대한 의견 (10)

도 추가하기선언 var 필요합니다 어떤;상단에 있습니다.

해설 (7)
해결책

require()하지 않는 것이 좋습에 대한 몇 가지 이유입니다. 중 하나,그것은 것이"break"의 효율성을 나무에 흔들리는 웹팩 2 고 두지 않도록 적절한 코드를 분할.

훨씬 더 나은 대안은 사용하는 웹팩's 의 이행시스템이다.가져오기:

System.import('/assets/js/regular-expresions.js').then(file => {
   // perform additional interactions after the resource has been asynchronously fetched
});

을 만족시키기 위해서는 타이프 라이터 컴파일러(으로 당신을 발견했),당신은 말할 필요는시스템은 허용되는 변수를 선언하여 그것:

declare var System: any;    

할 수 있습이 줄을 추가하여 구성 요소는 경우에만하면 그것을 참조,또는 당신에 추가 할 수 있습니다typings.d.ts종류 정의 파일을 사용하려면 전반에 걸쳐 사용합니다. 한 경우에는 변경 typings 파일을 다시 시작해야 합니다ng 역할세션이 있습니다.

이 만족하는 컴파일러에서 빌드 도 납치에 의해 실행시에 빌드 시스템,패치를 적용하여 통화로 웹팩's의 시스템입니다.가져오기.

웹팩이 될 것입니다 경우 확인 그것은 덩어리를 만족하는 요청한 파일이고,그렇지 않은 경우,이 동적으로 생성하는 스크립트 태그에 삽입하는 머리,로드 파일의`비동기의 특성,무엇에서 발생합니다.

한 파일을 로드 그것이 구문 분석하고 실행됩니다. 그러나,당신이 원하는 경우와 상호 작용하는 그것의 일부를 통해 비동기.다음()콜백을 당신의 파일이 필요하여 수출 당신이 원하는 무엇이든을 호출하는 등,:

export function test() {
    console.log('we called test');
}

이것이 노출됩니다.테스트()기능을 콜백을 처리기:

System.import('/assets/js/regular-expresions.js').then(file => {
   file.test();
});
해설 (6)

아래에보십시오,

require('/assets/js/regular-expresions.js')

세요!

해설 (1)

compilerOptions: {
   ...
   "typeRoots": [ "../node_modules/@types" ],
   ...
}

에 tsconfig.json

해설 (0)

도 이러한 문제를 해결하는 단계류

  1. npm-저장-dev@형/웹팩-env

  2. 업데이트 tsconfig.json 으로 다음에서 compilerOptions:

"유형":[ "웹팩-env" ]

3.지금 가 tsconfig.app.json 과이

"compilerOptions":{

"types": [
  "node"
]

}

해설 (0)