【问题标题】:remove # symbol from URL in Angular JS [duplicate]从Angular JS中的URL中删除#符号[重复]
【发布时间】:2019-05-23 23:49:12
【问题描述】:

我是 AngularJS 的初学者,我正在尝试从 URL 中删除 # 符号,我正在使用 Visual Studio code IDE,我执行了以下步骤从 URL 中删除 #,当我使用 HTTP Live server 启动应用程序时工作和 # 从 URL 中删除,但是当我重新加载页面时,我得到 404,有人可以建议我吗

app.js

var routerApp = angular.module('routerApp', ['ui.router']);

routerApp.config(function ($stateProvider,$locationProvider,$urlRouterProvider) {

    $urlRouterProvider.otherwise('/employeeList');

    $stateProvider

        .state('home', {
            url: '/home',
            templateUrl: 'templates/home.html'
        })
       .state('employeeList', {
            url: '/employeeList',
            templateUrl: 'templates/list_of_employees.html',
            controller: 'EmployeeController'
        })
        $locationProvider.html5Mode(true).hashPrefix('')

});

index.html

<head>
  <base href='/'>
</head>

【问题讨论】:

  • Ahmed Ghoniem 我在上面的代码中做了同样的事情,请检查
  • remove .hashPrefix('') 只保留 $locationProvider.html5Mode(true);
  • 即使删除也没有问题
  • 我正在使用 vs 代码我认为我们必须在 web.config 文件中设置一些东西但是在 vs 代码中没有 web.config 文件

标签: angularjs


【解决方案1】:

使用这个 $locationProvider.html5Mode(true) 需要在服务器端重写 URL,基本上你必须重写所有指向应用程序入口点的链接(例如 index.html)。

假设您使用的是 Apache 并且您的索引文件是 index.php 或 index.html ,请尝试将以下内容添加到您的 .htaccess 文件中以创建重写规则。

   <IfModule mod_rewrite.c> 
      RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f 
      RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*) /index.php/#/$1 
   </IfModule>

【讨论】:

  • 我应该在哪里使用这个代码?
  • 你需要在你的项目文件夹中创建一个.htaccess文件
  • 如何创建?
  • 我们可以在 vsCode 中创建这个吗?
  • 创建一个名为 .htaccess 的新文件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-01
  • 2021-05-09
相关资源
最近更新 更多