Why is AppController.$routeConfig undefined?

2019-07-14 06:39发布

问题:

I'm trying to follow this tutorial:

https://angular.github.io/router/getting-started

Why is AppController.$routeConfig undefined?

angular.module('app', ['ngNewRouter', 'app.home'])
  .controller('AppController', ['$router', AppController]);

AppController.$routeConfig([
  {path: '/', component: 'home' }
]);
function AppController ($router) {}

my html:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <base href="/">
  <title>My app</title>
</head>
<body ng-app="myApp" ng-controller="AppController as app">
<div ng-viewport></div>

<script src="node_modules/angular/angular.js"></script>
<script src="node_modules/angular-new-router/dist/router.es5.js"></script>
<script src="./components/home/home.js"></script>

<script src="app.js"></script>
</body>
</html>

回答1:

I found this manual to work for me with angular 1.5 but author uses angular 1.3 http://www.sitepoint.com/introduction-futuristic-new-router-angularjs/

here is the code that should work for you:

angular.module('app', ['ngNewRouter', 'app.home'])
  .controller('AppController', ['$router', function($router){
  $router.config([
    { path:'/', redirectTo:'/home' },
    { path:'/home', component:'home' },
    { path:'/example', component:'example' }
  ]);

  this.name='visitor';
}]);

and in html change these two lines to:

<body ng-controller="AppController">
<div ng-viewport></div>


回答2:

Try this instead, worked for me:

AppController.$routeConfig = [
  { path: '/', component: 'home' }
]

I think it may have been a mistake in the tutorial. $routeConfig is a property, not a function.