Javascript 无法删除AngularJS中的#

Javascript 无法删除AngularJS中的#,javascript,angularjs,hash,angular-ui-router,single-page-application,Javascript,Angularjs,Hash,Angular Ui Router,Single Page Application,该应用程序正在使用以下各项: AngularJS(v1.6.4) 网页包 用户界面路由器 我们需要路径独立于url中的# 为此,我在代码中添加了以下内容: 在app.js中 /* in the config section */ $locationProvider.hashPrefix(''); $locationProvider.html5Mode(true); var app = angular.module("app", ["ui.router"]); 在index.html中

该应用程序正在使用以下各项:

  • AngularJS(v1.6.4)
  • 网页包
  • 用户界面路由器
  • 我们需要路径独立于url中的#

    为此,我在代码中添加了以下内容:

    在app.js中

     /* in the config section */
     $locationProvider.hashPrefix('');
     $locationProvider.html5Mode(true);
    
    var app = angular.module("app", ["ui.router"]);
    
    index.html中

     <!-- under the head section -->
     <base href="/">
    
    <head>
       <base href="/" />
    </head
    
    它给出了以下错误:

    无法获取/pathName

    但当通过超链接访问时,同样的功能也很好。 此外,在访问“”时,浏览器会将其修改为“”

    是否有一种方法可以通过GET请求访问url,或者从webpack进行配置(或者其他方式)

    编辑1:

    路由代码:

    .config(($stateProvider) => {
        $stateProvider
            .state('pathName', {
                url: '/pathName',
                template: '<pathNameTemplate />'
            });
    })
    
    .config($stateProvider)=>{
    $stateProvider
    .state('路径名'{
    url:“/pathName”,
    模板:“”
    });
    })
    
    无html5:

  • 浏览器中的用户类型
    www.site.com/#/section
  • 请求被发送到
    www.site.com
    (#…被忽略)
  • 服务器返回索引.html
  • 角度启动并查看是否存在#--loads特定视图
  • Html5:

  • 浏览器中的用户类型
    www.site.com/section
  • 请求发送至
    www.site.com/section
  • 服务器返回索引.html
  • 角度启动并查看是否存在/--加载特定视图 所以服务器必须为您的所有路径返回index.html,也就是说,这意味着您无法从文件系统使其工作(file://.../). 您需要服务器并将其配置为添加重定向
  • 另外,你不需要这条线

    $locationProvider.hashPrefix('');
    

    这是使用从url中删除#的示例

    • AngularJsv1.5.7
    • ui路由器v0.4.2
    • web.config
    app.js

     /* in the config section */
     $locationProvider.hashPrefix('');
     $locationProvider.html5Mode(true);
    
    var app = angular.module("app", ["ui.router"]);
    
    app.config

    var config = function (locationProvider) {
        locationProvider.html5Mode(true);
    }
    config.$inject = ["$locationProvider"];
    app.config(config);
    
    <rewrite>
      <rules>
        <rule name="Main Rule" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
      </rules>
    </rewrite>
    
    index.html

     <!-- under the head section -->
     <base href="/">
    
    <head>
       <base href="/" />
    </head
    
    
    
    你能粘贴你的路线代码吗?@Avihaym,我认为这不是必需的,但我还是添加了。问题出在你的服务器上。您必须将服务器配置为始终返回基本服务器view@DeblatonJean-菲利普,你能解释一下我怎么做吗;该应用程序本身基本上是一个前端,没有像NodeJS这样的服务器端技术。在不了解您的服务器的情况下,我如何帮助您?您的错误发生在javascript代码尚未加载到浏览器时。你不能用JS代码来处理这个问题