【问题标题】:angular-ui-router error - Error: invalid 'handler' in when()angular-ui-router 错误 - 错误:when() 中的“处理程序”无效
【发布时间】:2016-02-16 08:04:45
【问题描述】:

我正在使用 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'
          });
      }]);

我收到以下错误:

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

我认为是template 属性。不能这样设置模板吗?

【问题讨论】:

    标签: angularjs angular-ui-router


    【解决方案1】:

    我认为您将urlRouterProviderrouteProvider 混淆了。

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

    所以只要切换就可以了:

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

    【讨论】:

    • 能否请您告诉我完整的模块代码,因为使用 angular 1.5,我仍然收到错误。 $routeProvider 是来自 'ui.router' 还是 'ngRoute'?
    • $routerProvider 与 ngRoute 一起使用,您使用的是 ui.router 吗?如果是这样,您需要使用 $stateProvider。
    • 是的,我在问题的标题中说明了这一点。抱歉,如果不清楚。
    • 哦,抱歉,没注意。您需要更改为 $stateProvider 并使用 .state 而不是 .when。所以.when('/home', ... 将是.state('home', ...(还要注意删除'/')
    猜你喜欢
    • 2016-12-04
    • 1970-01-01
    • 1970-01-01
    • 2016-08-18
    • 2017-12-15
    • 2016-09-16
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多