【问题标题】:I am trying to remove the # sign from the url in Angular 4我正在尝试从 Angular 4 中的 url 中删除 # 符号
【发布时间】:2018-01-31 10:15:35
【问题描述】:

我正在使用 PathLocationStrategy 并添加以下代码

imports: [
    ...
    RouterModule.forRoot(routes, { useHash: true })  // remove second argument
]

但是已经从url中删除了但是刷新页面时出现以下错误

找不到页面

【问题讨论】:

    标签: javascript angular angular4-router


    【解决方案1】:

    当你删除哈希时,你开始使用HTML5路由机制。要使用它,您需要配置 web 服务器 以支持您托管网站的 HTML5 路由机制。每个 Web 服务器的配置不同。

    【讨论】:

    • 或者,将所有 HTML5 路由重定向到 index.html 让 Angular 处理路由
    • 是的,配置服务器实际上也是这样做的
    【解决方案2】:

    您可以将网络服务器默认设置为您的 index.html

    nginx

    server {
        listen       80;
        server_name  localhost;
    
    
        location / {
          root   /usr/share/nginx/html;
          index  index.html index.htm;
          try_files $uri $uri/ /index.html;
        }
    }
    

    apache2

    使用此内容创建一个 .htaccess 文件

    <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteBase /
      RewriteEngine On
      RewriteCond %{HTTPS} off
      RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
      RewriteRule ^index\.html$ - [L]
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule . /index.html [L]
    </IfModule>
    

    【讨论】:

      猜你喜欢
      • 2023-02-03
      • 2018-08-07
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-01
      • 2022-06-30
      • 1970-01-01
      相关资源
      最近更新 更多