简体   繁体   English

Angular $ locationProvider html5Mode / <base> 错误

[英]Angular $locationProvider html5Mode / <base> error

So I'm running into a problem with Angular 1.3.15 and the $locationProvider. 所以我遇到了Angular 1.3.15和$ locationProvider的问题。 Every time I turn html5mode on I get a TypeError: Cannot read property 'replace' of undefined error. 每次我打开html5mode我得到一个TypeError: Cannot read property 'replace' of undefined错误的TypeError: Cannot read property 'replace' of undefined If I leave html5mode off, everything works fine. 如果我关闭html5mode,一切正常。

My directory structure is like so ( 4.DEV being a versioned directory name ): 我的目录结构是这样的( 4.DEV是一个版本化的目录名 ):

public /
    - index.html
    4.DEV /
        css /
        js /
        images /
        partials /
        etc...

I have a base tag set like this: 我有一个基本标签设置如下:

<base href="/4.DEV/">

And I'm configuring the $locationProvider like so: 我正在配置$ locationProvider,如下所示:

$locationProvider.html5Mode({ enabled: true });

If I change the base tag href to "/" Angular doesn't have any issues (but my assets don't load). 如果我将基本标记href更改为"/" Angular没有任何问题(但我的资产不加载)。 I suspect that the issue Angular is having deals with the fact that my index.html file is one directory up from the rest of the site. 我怀疑Angular问题涉及到我的index.html文件是网站其他部分的一个目录这一事实。

For reasons I won't list here, I can't change this dir structure and I REALLY don't want to change the base tag (because I don't want to manually stick a version number into all these files). 由于我不会在此列出的原因,我无法更改此dir结构,我真的不想更改基本标记(因为我不想手动将版本号粘贴到所有这些文件中)。 I also want to leave html5mode on. 我也想留下html5mode。

Does anyone have a solution for this problem? 有没有人有这个问题的解决方案? Is there any way for me to MANUALLY set the "base href" for Angular, but leave the tag alone for all my static assets? 有没有办法让我手动为Angular设置“base href”,但是对于我所有的静态资产都保留标签?

So, it looks like Angular's inner workings are tightly coupled to the href of the tag which results in major issues if you have a directory structure that's a bit unconventional like mine. 因此,看起来Angular的内部工作与标签的href紧密耦合,如果你的目录结构有点像我一样非常规,那么会导致重大问题。

Stumbled across this Github thread where people are asking for a feature to change this and @greglockwood posted a fix. 偶然发现这个Github线程 ,人们要求改变这个功能,@ greglockwood发布了修复程序。

With his fix, the tag can work for your static assets while you provide your own set "baseHref" for Angular's stuff. 通过他的修复,标签可以为您的静态资产工作,同时为Angular的东西提供自己的set“baseHref”。

Worked like a charm for me! 对我来说就像一个魅力! Hope this helps anyone who stumbles across this! 希望这可以帮助任何偶然发现的人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM