【问题标题】:angular remove hash sign角删除哈希符号
【发布时间】:2013-09-17 11:01:43
【问题描述】:

我正在尝试在 angularJS 中为 webapps 创建一个目录结构,我现在的问题是我使用 $routeProvider,这会在链接中添加一个井号 (#)。我试过这个功能

$locationProvider.html5Mode(true);

但是在我使用了 thyis 函数之后,routeProvider 不再工作,它不会在应该加载的地方加载任何 ng-view。这是我的路由提供者

$routeProvider
            .when('/', { templateUrl: '/view/home.html' })
            .when('/blog', { templateUrl: '/view/blog.html' })
            .when('/contact', { templateUrl: '/view/contact.html' })
            .otherwise({ redirectTo: '/' });

还有其他方法可以删除哈希并使应用程序不是单页应用程序吗?谢谢你,丹尼尔。

【问题讨论】:

    标签: javascript angularjs hash


    【解决方案1】:

    从 Angular 项目中的 Url 中删除 #。

    http://localhost/#/about ---> http://localhost/about

    1) 在 index.html 中添加基本 url

    2) 在您的 app.js 或 routes.js 中使用 html5 历史记录

    if(window.history && window.history.pushState){
        $locationProvider.html5Mode(true);
    }
    

    3) 服务器端配置 - 咕噜服务器: 中间件:函数(连接){ var 中间件 = []; 中间件.push(modRewrite([ '!/assets|\.html|\.js|\.css|\woff|\ttf|\swf$ /index.html' ]));

        middlewares.push(connect.static('.tmp'));
        middlewares.push(connect().use(
          '/bower_components',
          connect.static('./bower_components')
        ));
        middlewares.push(connect.static(appConfig.app));
    
        //middlewares.push(require('grunt-connect-proxy/lib/utils').proxyRequest);
        return middlewares;
      }
    
      -Ngnix Server  
      location ~ ^/(scripts.*js|styles|images) {
            gzip_static on;
            expires 1y;
            add_header Cache-Control public;
            add_header ETag "";
            break;
        }
    
       #location /api1 {
       #  rewrite ^/api1/(.*) /$1 break;
       #  proxy_redirect off;
       #  proxy_pass https://api1.example.com;
       #  proxy_set_header X-Real-IP $remote_addr;
       #  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       #  proxy_set_header X-Forwarded-Proto https;
       #  proxy_set_header Authorization $http_authorization;
       #}
    
       #location /api2 {
       #  rewrite ^/api2/(.*) /$1 break;
       #  proxy_redirect off;
       #  proxy_pass https://api2.example.com;
       #  proxy_set_header X-Real-IP $remote_addr;
       #  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       #  proxy_set_header X-Forwarded-Proto https;
       #  proxy_set_header Authorization $http_authorization;
       #}
    
       location / {
         try_files $uri /index.html;
       }
    

    【讨论】:

      【解决方案2】:

      你需要在路由文件中,就像你做的那样: $locationProvider.html5Mode(true); 并且您必须在头中添加到索引文件中: <base href="/">

      【讨论】:

        【解决方案3】:
        $locationProvider.hashPrefix('!')
        

        现在您可以在不使用# 的情况下使用“/home”之类的链接(仅适用于 html5 浏览器)

        【讨论】:

        • 它不起作用,当我写刷新页面时它会将我发送到本地主机根
        • 你同时使用$locationProvider.html5Mode(true);$locationProvider.hashPrefix('!') 函数?
        • 我同时使用$locationProvider.html5Mode(true);$locationProvider.hashPrefix('!');。还有我的 href 属性:<a href="/blog">Blog</a>。这个对我有用。在浏览器中,我的链接类似于localhost/blog
        • 问题是我在 www 文件夹中有一个名为 pdaniel 的子文件夹中的文件
        猜你喜欢
        • 2022-01-13
        • 2013-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-15
        • 1970-01-01
        • 2013-07-21
        • 2023-04-09
        相关资源
        最近更新 更多