刷新页面angularjs问题

刷新页面angularjs问题

本文介绍了Html5Mode - 刷新页面angularjs问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的angularJS应用程序,这在格式具有的URL(在开发中) -

I am working on an angularJS app, which has URLs in the format (in dev) -

http://localhost:6001/#/home/index
http://localhost:6001/#/content/index

由于这个程序,也将可在桌面浏览器,我想的网址是没有'#',所以我说这在我的应用程序的配置部分 -

As this app, will also be available on desktop browser, I wanted the URLs to be without '#', so I added this in my app's config section -

$locationProvider.html5Mode(true);

的正常工作和URL不显示#。但是现在有一个问题。在刷新浏览器中的某个特定页面,现在我得到这个错误 -

Which works fine and URLs don't show '#'. But there is a problem now. On refreshing a particular page in browser, now I get this error -

Cannot GET /home/index

在那里得到这个工作所需要的任何其他更改?

Are there any other changes required to get this working?

注意:这个程序是使用科尔多瓦CLI和托管使Node.js服务器上

Note: This app is using cordova CLI and is hosted on a Node.js server.

推荐答案

试试这个:


  1. 请确保你在你的server.js文件中这些行

  1. Make sure you have these lines in your server.js file

var express = require('express');
var app = express();
app.use(express.static(__dirname + '/public.build'));
app.use(function(req, res) {
res.sendFile('index.html', { root: __dirname + "/public.build" });
});

为什么会这样?请在这里阅读 - https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-configure-your-server-to-work-with-html5mode

第一步导致我下面的console.log消息应用程序:

The first step resulted in my app in the following console.log message:

Resource interpreted as Stylesheet but transferred with MIME type
text/html: "http://localhost:8080/board/css/style.css".

正如你可以看到,样式试图从不存在的路径加载(/板/文件夹不是在应用present,它的状态之一在UI的路由器)。
要解决它,我更换了以下行

As you can see, the styles are trying to load from non-existent path (/board/ folder is not present in the app, it's one of the states in ui-router).To fix it I replaced the following line

<link rel="stylesheet" href="css/style.css">

这一个:

<link rel="stylesheet" href="/css/style.css">


这篇关于Html5Mode - 刷新页面angularjs问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 18:00