我目前正在尝试将使用新路由器和 ECMAScript 6 编写的 Angular 1.4 应用程序的基本工作示例放在一起。我一直在不停地摆弄这段代码,但我不明白为什么我会收到错误被抛出:
Failed to instantiate module bookShelf due to:
TypeError: Cannot read property '$routeConfig' of undefined
我的 Angular 应用程序在我的 index.html 文件中被引导,如下所示:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
<div class="container">
<ng-viewport></ng-viewport>
</div>
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-new-router/dist/router.es5.js"></script>
<script src="bower_components/angular-messages/angular-messages.js"></script>
<script src="https://google.github.io/traceur-compiler/bin/traceur.js"></script>
<script src="https://google.github.io/traceur-compiler/src/bootstrap.js"></script>
<script type="module" src="app/bootstrap.js"></script>
</body>
</html>
我在另一个文件中配置了 1.4 路由器以及我的角度模块加载代码:
import { default as NerdController } from 'public/components/Nerd/Nerd.js';
import { default as MainController } from 'public/components/Main/Main.js';
import { default as NerdService } from 'public/services/NerdService.js';
var moduleName = 'bookShelf';
var app = angular.module(moduleName, ['ngNewRouter', NerdService])
.config(['$componentLoaderProvider', SetTemplatesPath])
.controller('AppController', ['$router', AppController])
.controller(NerdController)
.controller(MainController);
function SetTemplatesPath ($componentLoaderProvider) {
$componentLoaderProvider.setTemplateMapping(name => `public/components/${name}/${name}.html`);
}
function AppController ($router) {
$router.config([
{ path: '/', redirectTo: '/main' },
{ path: '/main', component: 'main' }, // component is template + controller from components folder
{ path: '/nerds', component: 'nerd' }
]);
}
export default moduleName;
最后是实际的引导文件:
import { default as bookShelfModule} from './app/bookShelf.main.js';
angular.bootstrap(document, [bookShelfModule]);
我完全不知所措,任何帮助都将不胜感激,因为我真的想开始使用 ES6 和新路由器。