仍然得到“未找到”时angular.js路线手动刷新未找到、路线、angular、js

2023-09-02 00:48:36 作者:蜕变丶

Angular.js航线上阅读了大量的写起坐和StackOverflow的问题之后,我仍然得到未找到的错误,当我做一个手动刷新。

步骤:

浏览本地主机 - >因为我的配置(如下图),我采取了本地主机的/ home 。视图和一切负载罚款。 在浏览器中点击刷新 - >浏览器显示此未找到:请求/家是不是在此服务器上找到

此问题可能是最像​​刷新页面提供"找不到网页"

我的配置

//路由配置。 angular.module(MyModule的)     的.config(['$ routeProvider','$ locationProvider',     功能($ routeProvider,$ locationProvider){         //启用pushState中的路由。         $ locationProvider.html5Mode(真正的);         $ routeProvider             。当('/家',{                 模板:{                     布局:/views/home.html                 },                 标题:欢迎您!             })             。当('/启动板',{                 模板:{                     布局:/views/layouts/default.html,                     内容:/views/partials/profile.html                 },                 标题:快速启动             })             。除此以外({                 redirectTo:/家             });     } ]); angularJS

其他的事情我已经做了:

在我的 index.html的,我已经有<基地的HREF =/> 升级到1.2.1角

以下是我已经尝试了htaccess的规则。无工作。

从​​刷新页面提供"找不到网页"

 < ifModule mod_rewrite.c>
    RewriteEngine叙述上
    的RewriteCond%{} REQUEST_FILENAME!-f
    的RewriteCond%{} REQUEST_FILENAME!-d
    的RewriteCond%{REQUEST_URI}!索引
    的RewriteCond%{REQUEST_URI} * !。(CSS | JS |全文| PNG)#Add额外的扩展需要。
    重写规则(。*)的index.html [L]
< / ifModule>
 

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

 < IfModule mod_rewrite.c>
    RewriteEngine叙述上
    的RewriteBase /
    的RewriteCond%{} REQUEST_FILENAME!-f
    的RewriteCond%{} REQUEST_FILENAME!-d
    重写规则^(。*)/index.html/#!/$1
< / IfModule>
 

解决方案

我不知道这是否是最佳的解决方案,但我没有发现过的设置组合:

包含散preFIX('!')(见下文) 不包括<基地的HREF =/> 在我的index.html 新增 FallbackResource /index.html 我的<目录PATH_TO_WWW_FILES> 在我的服务器节 .conf文件文件中每这个帖子。设置此之后,似乎没有什么重要的地方的mod_rewrite 设置为

//路由配置。 angular.module(MyModule的)     的.config(['$ routeProvider','$ locationProvider',     功能($ routeProvider,$ locationProvider){         //启用pushState中的路由。         $ locationProvider.html5Mode(真).hash preFIX(!);         $ routeProvider             。当('/家',{                 模板:{                     布局:/views/home.html                 },                 标题:欢迎您!             })             。当('/启动板',{                 模板:{                     布局:/views/layouts/default.html,                     内容:/views/partials/profile.html                 },                 标题:快速启动             })             。除此以外({                 redirectTo:/家             });     } ]);

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.

Steps:

browse to localhost --> because of my configuration (below), I'm taken to localhost/home. Views and everything load fine. hit refresh in the browser --> browser displays this Not Found: the requested /home is not found on this server

This question is probably most like Refreshing page gives "Page not found"

My configuration

// 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'
            });

    }
]);

Other things I have done:

In my index.html, I already have the <base href="/"> upgraded to angular 1.2.1

Here are the htaccess rules I have tried. None work.

from Refreshing page gives "Page not found"

<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>

from 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:

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'
            });

    }
]);

 
精彩推荐