类型错误:无法读取属性'取代'未定义 [英] TypeError: Cannot read property 'replace' of undefined
问题描述
我试图用 html5Mode
与UI的路由器时,得到上述错误。任何人都可以指向我,我做错了什么?
类型错误:无法读取属性'取代'未定义
在BD(angular.js:10555)
在对象<&匿名GT; (angular.js:11403)
。为L $ get.l. $摘要(angular.js:14222)
。为L $ get.l. $申请(angular.js:14493)
在angular.js:1449
在Object.e [如调用(angular.js:4182)
在D(angular.js:1447)
在SC(angular.js:1467)
在JD(angular.js:1361)
在HTMLDocument的<&匿名GT; (angular.js:26086)
在 angular.js
这片段是:
10554功能trimEmptyHash(URL){
10555回url.replace(/(#.+)|#$ /,'$ 1');
10556}
路由文件:
(函数(){ 使用严格的; 角
.module(应用)
的.config(路线); 。路线$注射='$ stateProvider','$ locationProvider']; 功能的路由($ stateProvider,$ locationProvider){ //配置的应用程序状态
$ stateProvider .STATE('应用',{
摘要:真实,
templateUrl:'模块/应用/ app.html',
控制器:'AppController的
}) .STATE('app.home',{
网址:'/',
templateUrl:'模块的/ home / index.html的
}); $ locationProvider.html5Mode(真);
}
})();
我已经设置了基本的URL在html:
<基本href =/应用程序//>
我遇到了同样的问题,在我的情况下,不确定的URL的说法被传递到trimEmptyHash()最终被从事实来的$$ absUrl在$位置属性格式没有得到初始化。挖进一步,似乎$$ absUrl通常被在$$构成()方法,通常会从$$解析(),它通常被调用的形式$$ parseLinkUrl()方法调用初始化。这里是$$ parseLinkUrl()(看着角度1.4.1):
这一点。$$ parseLinkUrl =功能(URL,relHref){
如果(relHref&安培;&安培; relHref [0] ==='#'){
//特殊情况的链接,散列片段:
//保持旧网址,只有更换哈希片段
this.hash(relHref.slice(1));
返回true;
}
VAR appUrl,prevAppUrl;
VAR rewrittenUrl; 如果((appUrl = beginsWith(的appBase,URL))!==未定义){
prevAppUrl = appUrl;
如果((appUrl = beginsWith(基地preFIX,appUrl))!==未定义){
rewrittenUrl = appBaseNoFile +(beginsWith('/',appUrl)|| appUrl);
}其他{
rewrittenUrl = +的appBase prevAppUrl;
}
}否则如果((appUrl = beginsWith(appBaseNoFile,URL))!==未定义){
rewrittenUrl = appBaseNoFile + appUrl;
}否则如果(appBaseNoFile ==网址+'/'){
rewrittenUrl = appBaseNoFile;
}
如果(rewrittenUrl){
这$$解析(rewrittenUrl);
}
返回!rewrittenUrl;
};
在我的情况下,最后的如果条款没有触发调用$$解析,因为rewrittenUrl从来没有得到一个价值,因为没有一个的if / else,如果在中间的条款确定为true。就我而言,那是因为我用的是服务于应用程序的URL(公司名称/ admin.html)实际上,我为应用程序设置的基本HREF(公司名称/管理/)之上,因此没有条件句解析为真。当我改变了我的基本href只是公司名称/,我不再遇到了这个问题。
另外,你可以初始化$$ absUrl上的位置的原型,或等待一段实物角修复 - 看的这个问题,看看3月30日提出的joelmdev变通方法。
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)
That segment in angular.js
is:
10554 function trimEmptyHash(url) {
10555 return url.replace(/(#.+)|#$/, '$1');
10556 }
The routing file:
(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);
}
})();
I've set the base url in the html:
<base href="/app/" />
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;
};
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.
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屋!