简体   繁体   English

如何配置角度以使用基线路径而不使用尾部斜杠?

[英]How do I configure angular to use base routes without trailing slashes?

Desired behavior 期望的行为

I have a search page that lives at this URL: 我有一个位于此URL的搜索页面:

http://example.com/search

The desired behavior is that the provided query parameters get passed to the page, and get updated as the user types a different query. 所需的行为是提供的查询参数传递给页面,并在用户键入不同的查询时更新。

Eg, 例如,

http://example.com/search?q=foo

results in the search box containing "foo" on page load. 导致页面加载时包含“foo”的搜索框。 If the user types "delicious pancakes" into the search box, the location bar's URL will update automatically to this: 如果用户在搜索框中键入“美味煎饼”,则位置栏的URL将自动更新为:

http://example.com/search?q=delicious+pancakes

This part is working fine; 这部分工作正常; I'm using $watch to monitor the value of the query and then update the location bar via $location.search('q', newValue) . 我正在使用$watch来监控查询的值,然后通过$location.search('q', newValue)更新位置栏。

But I'd like to use HTML5 mode (I am using $locationProvider.html5Mode(true) ), and I need the hashbang fallback to work for IE9 users. 但是我想使用HTML5模式(我使用的是$locationProvider.html5Mode(true) ),我需要hashbang回退才能为IE9用户工作。

I would like the hashbang fallback to look like this: 我希望hashbang回退看起来像这样:

http://example.com/search#?q=delicious+pancakes

The Problem 问题

If I don't set the <base> element, the $location service treats everything after the domain as the path, so it actually falls back to this in IE9: 如果我没有设置<base>元素,$ location服务会将域之后的所有内容视为路径,因此它实际上会在IE9中回退到这一点:

http://example.com/#search?q=delicious+pancakes

This results in the browser redirecting to the home page (ie, it takes me away from the search page completely!) 这导致浏览器重定向到主页(即,它完全带我离开搜索页面!)

So the best practice seems to be to use the <base> tag; 所以最好的做法似乎是使用<base>标签; so I set it to 所以我把它设置为

<base href="http://example.com/search/"></base>

(Note the trailing slash; I understand this is required by both Angular and by the HTML spec.) (注意尾部斜杠;我理解这是Angular和HTML规范都需要的。)

However, this means I must use the following URL: 但是,这意味着我必须使用以下URL:

http://example.com/search/?q=delicious+pancakes

(with the IE9 fallback being http://example.com/search/#?q=delicious+pancakes ) (IE9后备版本为http://example.com/search/#?q=delicious+pancakes

This works, but I don't want to require users to type that slash after "search" and before the question mark. 这有效,但我不想要求用户在“搜索”之后和问号之前键入斜杠。 Currently if I try to access the page via http://example.com/search?q=foo , I receive this error in the console: 目前,如果我尝试通过http://example.com/search?q=foo访问该页面,我会在控制台中收到此错误:

TypeError: whole is undefined

This error is apparently caused because the $location service's $$rewrite function not returning anything. 这个错误显然是因为$ location服务的$$rewrite函数没有返回任何内容。 For reference, I'll copy the source from angular.js: 作为参考,我将从angular.js复制源代码:

this.$$rewrite = function(url) {
    var appUrl, prevAppUrl;

    if ( (appUrl = beginsWith(appBase, url)) !== undefined ) {
      prevAppUrl = appUrl;
      if ( (appUrl = beginsWith(basePrefix, appUrl)) !== undefined ) {
        return appBaseNoFile + (beginsWith('/', appUrl) || appUrl);
      } else {
        return appBase + prevAppUrl;
      }
    } else if ( (appUrl = beginsWith(appBaseNoFile, url)) !== undefined ) {
      return appBaseNoFile + appUrl;
    } else if (appBaseNoFile == url + '/') {
      return appBaseNoFile;
}

Is there any way I can achieve the desired behavior without the slash after "search", and still have IE9 support? 有没有什么方法可以在“搜索”之后没有斜线的情况下实现所需的行为,并且仍然支持IE9?

Use a conditional statement to exclude IE9 from HTML5mode: 使用条件语句从IE5mode中排除IE9:

if(!!$window.innerWidth && !$window.matchMedia)
  {
  $locationProvider.html5Mode({enabled: false, requireBase: false});
  $locationProvider.hashPrefix("search");
  }
else
  $locationProvider.html5Mode({enabled: true, requireBase: false});

References 参考

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

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