Angular routing without hash in url

时间:2017-04-24 17:16:17

标签: angularjs routing

I use angular seed template for my project. I tried to make routing without hash. I added in my app.js $locationProvider.html5Mode(true); and in my index.html in head section <base href="/"> but when I refresh a page or enter the url, result is the index of my folder. index of view

What am I doing wrong?

1 个答案:

答案 0 :(得分:0)

您的网址正在被处理,就像它是服务器资源一样,通常它会使用重写或路由来为您的index.html文件(或您的默认输入)提供每个网址。

你可以使用不同的服务器来实现,我提取superstatic npm包,它提供了url重写,而不是angularjs-seed项目中的http-server

npm install superstatic --save-dev

使用以下内容在应用的根目录上创建文件superstatic.json

{
  "cleanUrls": true,
  "rewrites": [
    {"source":"/**","destination":"/app/index.html"}
  ]
}

并覆盖文件package.json

中的以下行
"scripts": {    
    ...
    "start": "http-server -a localhost -p 8000 -c-1 ./app",
    ...    
}

"scripts": {    
    ...
    "start": "superstatic --config superstatic.json --port 8080 --host localhost",
    ...    
}

最后

npm start