Abstract :vue-router Default hash Pattern —— Use URL Of hash To simulate a complete one URL, So when URL When the change , The page will not be reloaded .
This article is shared from Huawei cloud community 《 Study VueRouter,HTML5 History Pattern , because history The mode refresh page appears 404》, author : DevFeng .
vue-router Default hash Pattern —— Use URL Of hash To simulate a complete one URL, So when URL When the change , The page will not be reloaded .
If you don't want something ugly hash, We can use routing history Pattern , This model takes full advantage history.pushState API To complete URL Jump without reloading the page .
When you use history Mode time ,URL Just like normal url, for example http://yoursite.com/user/id, Good looking, too !
But play it well , Background configuration support is also required . Because our application is a single page client application , If the background is not configured correctly , When the user is directly accessed in the browser http://oursite.com/user/id It will return 404, It's not good .
So , You want to add a candidate resource to the server that covers all cases : If URL No static resources were matched , The same should be returned index.html page , This page is you app Dependent pages .
Be careful : The following example assumes that you are serving the application in the root directory . If you want to deploy to a subdirectory , You need to use VueCLI Of
publicPath Options (opens new window) And the related router base property (opens new window). You also need to adjust the root directory in the following example to a subdirectory ( For example RewriteBase /name-of-your-subfolder/ Replace RewriteBase/).
except mod_rewrite, You can also use FallbackResource (opens new window).
about Node.js/Express, Please consider using connect-history-api-fallback middleware (opens new window).
1. install IIS UrlRewrite(opens new window)
2. Create a... In the root of your site web.config file , The contents are as follows :
In your firebase.json Add :
Give me a warning , Because after that , Your server will not return 404 Error page , Because it returns for all paths
index.html file . To avoid that , You should be Vue The application covers all routing conditions , Then give another example 404 page .
perhaps , If you use Node.js The server , You can use the server route to match the incoming URL, Does not return... When there is no match to the route 404, To achieve a fallback .
Click to follow , The first time to learn about Huawei's new cloud technology ~