PathLocationStrategy works only locally

前端 未结 3 444
你的背包
你的背包 2020-12-19 13:23

I have problem with hash, on my working project when I build it, on test project all work correctly. I already read this questions in google: Angular2 without hash in the ur

3条回答
  •  庸人自扰
    2020-12-19 14:10

    PathLocationStrategy uses HTML5 pushState which depends on the HTML tag. You need to tell the browser what will be prefixed to the requested path. Remember to add it in your app:

    
    

    Here You can read more about routing in Angular

    One more important thing is to route (on the server side) every Angular route to base path specified in index.html. For example, this is how it is done in nodeJS:

    app.get('/*', (req, res) => {
        res.render('index', {req, res});
    });
    

    or Apache:

    
        RewriteEngine On
        RewriteBase /
        RewriteRule ^index\.html$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.html [L]
    
    

    Otherwise, when your customer will reach for example www.example.com/path/someThing your server will be looking for index.html file in the /var/www/example.com/path/someThing/ instead of /var/www/example.com/

提交回复
热议问题