使用 angular.js 路由手动刷新时仍然“未找到" [英] Still getting 'Not Found' when manually refreshing with angular.js route

查看:33
本文介绍了使用 angular.js 路由手动刷新时仍然“未找到"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读了大量关于 Angular.js 路由的文章和 stackoverflow 问题后,我在手动刷新时仍然收到未找到"错误.

After reading a ton of write-ups and stackoverflow questions on Angular.js route, I'm still getting the 'Not Found' error when I do a manual refresh.

步骤:

  1. 浏览到 localhost --> 因为我的配置(如下),我被带到了 localhost/home.视图和一切正常加载.
  2. 在浏览器中点击刷新 --> 浏览器显示此 Not Found: the required/home is not found on this server
  1. browse to localhost --> because of my configuration (below), I'm taken to localhost/home. Views and everything load fine.
  2. hit refresh in the browser --> browser displays this Not Found: the requested /home is not found on this server

这个问题可能最像​​刷新页面给出找不到页面"

我的配置

// Routing configuration.
angular.module('myModule')
    .config(['$routeProvider', '$locationProvider', 
    function ($routeProvider, $locationProvider) {
        // Enable pushState in routes.
        $locationProvider.html5Mode(true);

        $routeProvider
            .when('/home', {
                templates: {
                    layout: '/views/home.html'
                },
                title: 'Welcome!'

            })
            .when('/launchpad', {
                templates: {
                    layout: '/views/layouts/default.html',
                    content: '/views/partials/profile.html'
                },
                title: "Launchpad"
            })
            .otherwise({
                redirectTo: '/home'
            });

    }
]);

我做过的其他事情:

  • 在我的 index.html 中,我已经有了 <base href="/">
  • 升级到 Angular 1.2.1

这是我尝试过的 htaccess 规则.都不起作用.

来自​​刷新页面显示未找到页面"

<ifModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !index
    RewriteCond %{REQUEST_URI} !.*\.(css|js|html|png) #Add extra extensions needed.
    RewriteRule (.*) index.html [L]
</ifModule>

来自 http://ericduran.io/2013/05/31/angular-html5Mode-with-yeoman/

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

推荐答案

我不知道这是否是最佳解决方案,但我确实找到了有效的设置组合:

I don't know if this is the optimal solution, but I did find a combination of settings that worked:

  • 包括hashPrefix('!')(见下文)
  • 没有在我的 index.html 中包含
  • FallbackResource/index.html 添加到我的服务器 .conf 文件中的 部分,每个 这篇文章.设置好之后,本地mod_rewrite 的设置似乎无关紧要.
  • Include the hashPrefix('!') (see below)
  • Did not include <base href="/"> in my index.html
  • Added FallbackResource /index.html to my <Directory PATH_TO_WWW_FILES> section in my server .conf file per this post. After setting this, it didn't seem to matter what the local mod_rewrite settings were.
// Routing configuration.
angular.module('myModule')
    .config(['$routeProvider', '$locationProvider', 
    function ($routeProvider, $locationProvider) {
        // Enable pushState in routes.
        $locationProvider.html5Mode(true).hashPrefix('!');

        $routeProvider
            .when('/home', {
                templates: {
                    layout: '/views/home.html'
                },
                title: 'Welcome!'

            })
            .when('/launchpad', {
                templates: {
                    layout: '/views/layouts/default.html',
                    content: '/views/partials/profile.html'
                },
                title: "Launchpad"
            })
            .otherwise({
                redirectTo: '/home'
            });

    }
]);

这篇关于使用 angular.js 路由手动刷新时仍然“未找到"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆