簡體   English   中英

AngularJs + Typescript:錯誤TS2307:找不到模塊'angular'

[英]AngularJs + Typescript: error TS2307: Cannot find module 'angular'

我正在嘗試組合Angular1.5 + Typescript。 已安裝所需類型:

npm install @types/angularjs

但仍然看到錯誤:

錯誤TS2307:找不到模塊'angular'

已嘗試過不同的選擇:

import 'angular';
import 'angularjs'; //that is name inside @types
import angular from 'angularjs';
import * as angular from 'angular';

我正在使用:

"ts-loader": "1.0.0",
"typescript": "2.0.7",
"webpack": "1.13.3",
"webpack-dev-server": "1.16.2"

tsconfig非常標准:

{
  "compilerOptions": {
    "target": "es5",
    "noImplicitAny": false,
    "sourceMap": false,
    "lib": ["es6", "dom"],
    "typeRoots": ["./node_modules/@types"]
  },
  "exclude": ["node_modules"]
}

已經簡化了webpack配置:var webpack = new require('webpack');

module.exports = {
  context: __dirname + '/src',
  entry: './index.ts',
  output: {
    path: './dist',
    filename: 'app.bundle.js'
  },
  module: {loaders: [{test: /\.tsx?$/, loader: 'ts-loader'},]},
  resolve: {
    extensions: ['', '.webpack.js', '.web.js', '.ts', '.tsx', '.js'],
  },

  plugins: [new webpack.NoErrorsPlugin()],

  devtool: 'source-map',

  devServer: {
    contentBase: './dist',
    historyApiFallback: true,
    inline: true
  }
};

我還簡化了index.ts本身,只關注類型導入:

import * as angular from 'angular';
angular.module('testmodule', []);

UPD :tsconfig.json。 添加了typeRoots選項。

現在得到新的錯誤:

ERROR in ... / node_modules/@types/angularjs/angular-mocks.d.ts(6,26):error TS2307:找不到模塊'angular'。

ERROR在... / node_modules/@types/angularjs/angular-mocks.d.ts(65,49):錯誤TS2304:找不到名稱'IServiceProvider'。

ERROR在... / node_modules/@types/angularjs/angular-mocks.d.ts(122,62):錯誤TS2304:找不到名稱'IScope'。

ERROR在... / node_modules/@types/angularjs/index.d.ts(66,43):錯誤TS2304:找不到名稱'JQuery'。

如果你在命令行中運行它應該解決問題。

npm install @types/angular

令人煩惱的是這個修復是由於某種原因它不會更新你的package.json ..但是如果你將以下內容添加到package.json中的依賴項而不是上面它應該修復問題並擁有package.json鏡像你的包裹。

"@types/angular": "1.6.5",
"@types/jquery": "2.0.40",

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM