gpt4 book ai didi

javascript - AngularJS - 类型错误 : Cannot read property 'canonicalUrl' of undefined

转载 作者:行者123 更新时间:2023-11-28 07:16:38 25 4
gpt4 key购买 nike

我正在使用 ES6 在 AngularJS 中开发一个应用程序,但我试图以适合 AngularJS 2.0 的方式来实现它,所以我使用“Angular new router”

应用程序.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'
};
};

仪表板.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: Cannot read property 'canonicalUrl' of undefined

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)

最佳答案

我刚刚发现您传递给 .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

);
}

它工作得很好。

关于javascript - AngularJS - 类型错误 : Cannot read property 'canonicalUrl' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30775846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com