类型错误:无法读取未定义的属性“替换" [英] TypeError: Cannot read property 'replace' of undefined
问题描述
我在尝试将 html5Mode
与 ui-router 一起使用时遇到上述错误.谁能指出我做错了什么?
I'm getting the above error when trying to use html5Mode
with ui-router. Can anyone point me to what I'm doing wrong?
TypeError: Cannot read property 'replace' of undefined
at bd (angular.js:10555)
at Object.<anonymous> (angular.js:11403)
at l.$get.l.$digest (angular.js:14222)
at l.$get.l.$apply (angular.js:14493)
at angular.js:1449
at Object.e [as invoke] (angular.js:4182)
at d (angular.js:1447)
at sc (angular.js:1467)
at Jd (angular.js:1361)
at HTMLDocument.<anonymous> (angular.js:26086)
angular.js
中的那一段是:
10554 function trimEmptyHash(url) {
10555 return url.replace(/(#.+)|#$/, '$1');
10556 }
路由文件:
(function(){
'use strict';
angular
.module('app')
.config(routes);
routes.$inject = ['$stateProvider', '$locationProvider'];
function routes($stateProvider, $locationProvider) {
// Configure app states
$stateProvider
.state('app', {
abstract: true,
templateUrl: 'modules/app/app.html',
controller: 'AppController'
})
.state('app.home', {
url: '/',
templateUrl: 'modules/home/index.html'
});
$locationProvider.html5Mode(true);
}
})();
我已经在 html 中设置了基本网址:
I've set the base url in the html:
<base href="/app/" />
推荐答案
我遇到了同样的问题,就我而言,传递给 trimEmptyHash() 的未定义url"参数最终来自 $$absUrl$location 上的属性未初始化.进一步挖掘,似乎 $$absUrl 通常在 $$compose() 方法中初始化,该方法通常从 $$parse() 调用,而 $$parse() 通常从 $$parseLinkUrl() 方法调用.这是 $$parseLinkUrl()(看 Angular 1.4.1):
I ran into the same issue, and in my case the undefined "url" argument being passed to trimEmptyHash() was ultimately coming from the fact that $$absUrl propery on $location was not getting initialized. Digging in further, it appears that $$absUrl normally gets initialized in the $$compose() method, which typically gets called from $$parse() which typically gets called form the $$parseLinkUrl() method. Here is $$parseLinkUrl() (looking at Angular 1.4.1):
this.$$parseLinkUrl = function(url, relHref) {
if (relHref && relHref[0] === '#') {
// special case for links to hash fragments:
// keep the old url and only replace the hash fragment
this.hash(relHref.slice(1));
return true;
}
var appUrl, prevAppUrl;
var rewrittenUrl;
if ((appUrl = beginsWith(appBase, url)) !== undefined) {
prevAppUrl = appUrl;
if ((appUrl = beginsWith(basePrefix, appUrl)) !== undefined) {
rewrittenUrl = appBaseNoFile + (beginsWith('/', appUrl) || appUrl);
} else {
rewrittenUrl = appBase + prevAppUrl;
}
} else if ((appUrl = beginsWith(appBaseNoFile, url)) !== undefined) {
rewrittenUrl = appBaseNoFile + appUrl;
} else if (appBaseNoFile == url + '/') {
rewrittenUrl = appBaseNoFile;
}
if (rewrittenUrl) {
this.$$parse(rewrittenUrl);
}
return !!rewrittenUrl;
};
在我的例子中,最后的if"子句没有触发对 $$parse 的调用,因为rewrittenUrl"从来没有得到一个值,因为中间的 if/else if 子句都没有解析为 true.就我而言,这是因为我用来提供应用程序的 url (CompanyName/admin.html) 实际上高于我为应用程序设置的基本 Href (CompanyName/admin/),因此没有任何条件解析为 true.一旦我将基本 Href 更改为 CompanyName/,我就不再遇到此问题.
In my case, the final "if" clause was not triggering the call to $$parse, because "rewrittenUrl" never got a value, because none of the if/else if clauses in the middle resolved to true. In my case, it was because the url I was using to serve the app (CompanyName/admin.html) was actually above the base Href that I set for the app (CompanyName/admin/), so none of the conditionals resolved to true. As soon as I changed my base Href to just CompanyName/, I no longer ran into this issue.
或者,您可以在位置原型上初始化 $$absUrl,或等待来自 Angular 的某种修复 - 观看 这个问题,并查看 3 月 30 日 joelmdev 提出的解决方法.
Alternatively, you could initialize $$absUrl on the location prototype, or wait for some kind of fix from Angular - watch this issue, and see the workaround proposed by joelmdev on March 30th.
这篇关于类型错误:无法读取未定义的属性“替换"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!