AngularJS - TypeError:无法读取未定义的属性'canonicalUrl'

时间:2015-06-11 08:45:27

标签: javascript angularjs angular ecmascript-6 typeerror

我正在使用ES6在AngularJS中开发一个应用程序,但我正在尝试以适合AngularJS 2.0的方式进行,所以我使用的是“Angular new router”

的application.js: application是一个指令,它作为属性放在<html>标记中

class Application {

    constructor($router) {
        this.$router = $router;
        this.routing();
    }

    routing(){
        this.$router.config(
            {
                path: '/',
                component: 'main'
                //component: {'main': 'main'} // view-name:component => name.html, NameController
            },
            {
                path: '/doctors',
                component: 'doctors'
            }
        );
    }

}

Application.$inject = ['$router'];

export default function() {
    return {
        scope: {},
        controller: Application,
        controllerAs: 'applicationCtrl'
    };
};

Dashboard.js:

import doctorsCtrl from "../../page/dashboard/DoctorsCtrl.js"; // controller for the component
import mainCtrl from "../../page/dashboard/MainCtrl.js";
import doctors from "./widget/doctors/Doctors.js";


angular.module('agenda.dashboard', ['ngNewRouter'])

    .directive("application", application)
    .directive("doctors", doctors)

    .config(function ($componentLoaderProvider) {
        $componentLoaderProvider.setTemplateMapping(function (name) {
            return 'page/dashboard/' + dotCase(name) + '.html';
        });

        $componentLoaderProvider.setCtrlNameMapping(function (name) {
            return name[0].toUpperCase() + name.substr(1) + 'Ctrl';
        });
    })

    .controller('MainCtrl', mainCtrl)
    .controller('DoctorsCtrl', doctorsCtrl);


    function dotCase(str) {
    return str.replace(/([A-Z])/g, function ($1) {
        return '.' + $1.toLowerCase();
    });
}

Main('/')工作正常,但当我尝试打开('/ doctors')时我收到错误

  

TypeError:无法读取未定义

的属性'canonicalUrl'
at Grammar.recognize (router.es5.js:1453)

at RootRouter.recognize (router.es5.js:752)

at RootRouter.navigate (router.es5.js:680)

at RootRouter.$$rootRouter.navigate (router.es5.js:94)

at Object.fn (router.es5.js:89)

at Scope.$get.Scope.$digest (angular.js:15556)

at Scope.$get.Scope.$apply (angular.js:15824)

at bootstrapApply (angular.js:1628)

at Object.invoke (angular.js:4426)

at doBootstrap (angular.js:1626)

1 个答案:

答案 0 :(得分:0)

我刚刚发现你传递给.config()的内容应该是一个数组。

所以当我把它更改为:

routing(){
        this.$router.config(
         [ // NOTE THIS
            {
                path: '/',
                component: 'main'
                //component: {'main': 'main'} // view-name:component => name.html, NameController
            },
            {
                path: '/doctors',
                component: 'doctors'
            }
         ] // NOTE THIS

        );
    }

效果很好。