gpt4 book ai didi

AngularJs + typescript : error TS2307: Cannot find module 'angular'

转载 作者:搜寻专家 更新时间:2023-10-30 20:37:54 26 4
gpt4 key购买 nike

我正在尝试结合使用 Angular1.5 + Typescript。安装需要的类型:

npm install @types/angularjs

但仍然看到错误:

error TS2307: Cannot find module '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: Cannot find module 'angular'.

ERROR in .../node_modules/@types/angularjs/angular-mocks.d.ts (65,49): error TS2304: Cannot find name 'IServiceProvider'.

ERROR in .../node_modules/@types/angularjs/angular-mocks.d.ts (122,62): error TS2304: Cannot find name 'IScope'.

ERROR in .../node_modules/@types/angularjs/index.d.ts (66,43): error TS2304: Cannot find name 'JQuery'.

最佳答案

如果您在命令行中运行它应该可以解决问题。

npm install @types/angular

此修复程序令人恼火的是,出于某种原因,它不会更新您的 package.json.. 但是,如果您将以下内容添加到 package.json 中的依赖项中,而不是 上面的内容应该解决问题并让 package.json 镜像您的包。

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

关于AngularJs + typescript : error TS2307: Cannot find module 'angular' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40419682/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com