简体   繁体   English

html5mode Angular给出异常

[英]html5mode Angular giving exception

I am building a website in Angular 1.5.5 and want to have pretty url (without #). 我正在Angular 1.5.5中构建一个网站,并希望拥有漂亮的url(不带#)。 Following the instructions on some other posts, I added Html5mode to be router along with base url in the index.html 按照其他文章的说明,我在index.html中添加了Html5mode作为路由器以及基本url。

    $locationProvider.html5Mode(true).hashPrefix('!');
    <base href="innovationglobal"> 

However, I get the following exception : 但是,我得到以下异常:

Error: Failed to execute 'replaceState' on 'History': A history state object with URL ' http://home/ ' cannot be created in a document with origin ' http://localhost:8080 '. 错误:无法在“历史记录”上执行“ replaceState”:无法在源为“ http:// localhost:8080 ”的文档中创建URL为“ http:// home / ”的历史状态对象。 at Error (native) at Yf.k.url (lib/1.5.5/angular.min.js:47:311) 在Yf.k.url(lib / 1.5.5 / angular.min.js:47:311)出现错误(本机)时

After changing your tag to an absolute URL or path 将标签更改为绝对URL或路径后

<base href="innovationglobal">

either by adding / or https:// 通过添加/https://

<base href="/innovationglobal/">

now when you reload the page, the browser will send a request to the server for your current path: /innovationglobal/ 现在,当您重新加载页面时,浏览器将向服务器发送您当前路径的请求: /innovationglobal/

You need to set up your server to handle that path --and any other path your Angular app may put into the browser address bar. 您需要设置服务器以处理该路径-以及Angular应用可能放入浏览器地址栏中的任何其他路径。 That could be /innovationglobal/users/ or /innovationglobal/products/detail/ etc. 可能是/innovationglobal/users//innovationglobal/products/detail/等。

Because the user may press F5 to reload on any page they are viewing. 因为用户可以按F5重新加载他们正在查看的任何页面。 And that would take control away from your Angular app, and send the request for that URL to the server. 这将使您的Angular应用程序失去控制权,并将对该URL的请求发送到服务器。

So you need to set up your server with a catch-all. 因此,您需要为服务器设置全部功能。 Whatever the path in the address bar, your server should reply with the index.html of your Angular app. 无论地址栏中的路径如何,您的服务器都应使用Angular应用程序的index.html进行回复。

But careful, you need to make an exception for your static assets (files like .js and .css). 但是要小心,您需要为静态资产(.js和.css之类的文件)设置一个例外。 For nginx, that would looks somewhat like this: 对于nginx,这看起来像这样:

location = /static/ {
    alias /var/www/static/;
    try_files $uri $uri/ =404;
}
location /innovationglobal/ {
    root /var/www/innovationglobal;
    try_files $uri /app/index.html =404;
}

Depending on the web server you are using. 根据您使用的Web服务器。

Have you tried enable html5Mode without using a base: 您是否尝试过html5Mode不使用基础的情况下启用html5Mode

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

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

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