angular-ui-router 错误 - 错误:when() 中的“处理程序”无效

dag*_*da1 0 angularjs angular-ui-router

我正在使用 angular 1.5 和 angular-ui-router 版本 0.2.18

我的 app.js 文件如下所示:

import angular from 'angular';
import uiRouter from 'angular-ui-router';
import 'angular-ui-bootstrap';
import './components/logIn/logIn';
import './components/home/home';

    angular.module('app', ['ui.bootstrap', 'app.components', uiRouter])
      .controller('mainCtrl', function() {
      })
      .config(['$stateProvider', '$urlRouterProvider', '$urlMatcherFactoryProvider', function($stateProvider,   $urlRouterProvider,   $urlMatcherFactoryProvider){
        $urlRouterProvider
          .when('/home', {
            template: '<home></home>'
          })
          .otherwise({
            redirectTo: '/home'
          });
      }]);
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

未捕获的错误:[$injector:modulerr] 无法实例化模块应用程序,因为:错误:$UrlRouterProvider.when 处的 when() 中的“处理程序”无效(eval at ( http://localhost:8080/app.bundle.js: 511:2 ), :1916:13) 在 eval (eval at ( http://localhost:8080/app.bundle.js:493:2 ), :20:22) 在 Object.invoke (eval at ( http: //localhost:8080/app.bundle.js:505:2 ), :4604:19) 在 runInvokeQueue (eval at ( http://localhost:8080/app.bundle.js:505:2 ), :4497: 35) 在 eval (eval at ( http://localhost:8080/app.bundle.js:505:2 ), :4506:11) at forEach (eval at ( http://localhost:8080/app.bundle. js:505:2 ), :321:20) 在 loadModules (eval at (http://localhost:8080/app.bundle.js:505:2 ), :4487:5) 在 createInjector (eval at ( http://localhost:8080/app.bundle.js:505:2 ), : 4409:19) 在 doBootstrap (eval at ( http://localhost:8080/app.bundle.js:505:2 ), :1691:20)

我认为是template财产。不能像这样设置模板吗?

Omr*_*ron 5

我认为您urlRouterProviderrouteProvider.

后者用于定义路由(以及其中的模板)。当 URL 不匹配(作为字符串或函数)时,您可以使用第一个进行重定向。在otherwise您使用属于$routeProvider为好。

所以只需切换,你应该很高兴:

$routeProvider.when('/home', {
    template: '<home></home>'
});

// or $urlRouterProvider.otherwise('/home');
$routeProvider.otherwise({ redirectTo: '/home' });
Run Code Online (Sandbox Code Playgroud)