我正嘗試使用Angular 1 + Typescript 2和Webpack設置一個裸機應用程序。該應用程序工作正常,直到我嘗試使用外部模塊,例如:angular-ui-router
。如何獲取webpack以查找角度模塊?
它總是抱怨說,它無法找到的依賴:
ERROR in ./src/app.ts Module not found: Error: Cannot resolve module 'angular-ui-router' in ./src/app.ts 3:26-54
演示顯示問題:https://github.com/jxc876/angular-ts
我懷疑我不是正確導入路由依賴,嘗試:
import uiRouter from 'angular-ui-router';
import * as uiRouter from 'angular-ui-router'
試用angular-route
和ui-router
但都沒有作用。試過ts-loader
和awesome-typescript-loader
。
import * as angular from 'angular';
import uiRouter from 'angular-ui-router';
let myApp = angular.module('myApp', [uiRouter]);
myApp.config(function($stateProvider) {
let homeState = {
name: 'home',
url: '/home',
template: '<div>It works !!!</div>'
}
$stateProvider.state(homeState);
});
配置
的package.json
{
"name": "ts-demo",
"scripts": {
"start": "webpack-dev-server --content-base ./src"
},
...
"devDependencies": {
"@types/angular": "^1.5.16",
"@types/angular-ui-router": "^1.1.34",
"awesome-typescript-loader": "^3.0.0-beta.3",
"typescript": "^2.0.9",
"webpack": "^1.13.3",
"webpack-dev-server": "^1.16.2"
},
"dependencies": {
"angular": "^1.5.8",
"angular-ui-router": "^0.3.1",
"enhanced-resolve": "^2.3.0"
}
}
webpack.config.js
module.exports = {
entry: './src/app',
output: {
filename: './dist/bundle.js'
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx']
},
devtool: 'source-map',
module: {
loaders: [
{
test: /\.ts$/,
loader: 'awesome-typescript-loader'
}
]
}
};
tsconfig.json
{
"compilerOptions": {
"outDir": "./dist/",
"allowJs": true,
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"strictNullChecks": true,
"listFiles": true
},
"include": [
"./src/**/*"
],
"exclude": [
"node_modules"
]
}
相關:http://stackoverflow.com/questions/34657652/webpack-with-typescript-external-commonjs-module/34658780 –