router.js的配置css
Vue.use(Router) // 得到相對路徑的方法 function getAbsolutePath () { let path = location.pathname return path.substring(0, path.lastIndexOf('/') + 1) } export default new Router({ // 配置base路徑 base: getAbsolutePath(), mode: 'history', routes: [ { path: '/', name: 'index', redirect: '/sample' }, { path: '/hello', name: 'Hello', component: Hello } ], linkActiveClass: 'active' })
config/config.js的配置html
build: { env: require('./prod.env'), index: path.resolve(__dirname, '../dist/index.html'), assetsRoot: path.resolve(__dirname, '../dist'), assetsSubDirectory: 'static', // 修改assetsPublicPath配置相對路徑 assetsPublicPath: '/', productionSourceMap: true, // Gzip off by default as many popular static hosts such as // Surge or Netlify already gzip all static assets for you. // Before setting to `true`, make sure to: // npm install --save-dev compression-webpack-plugin productionGzip: false, productionGzipExtensions: ['js', 'css'], // Run the build command with an extra argument to // View the bundle analyzer report after build finishes: // `npm run build --report` // Set to `true` or `false` to always turn it on or off bundleAnalyzerReport: process.env.npm_config_report },
開啓rewrite模塊
sudo a2enmod rewrite
修改sites-enable下的配置文件vue
<VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/html <Directory /var/www/html> Order deny,allow Allow from all AllowOverride all </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
在網頁根目錄下添加.htaccess文件webpack
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.html$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.html [L] </IfModule>
重啓服務器後便可web
可參考:https://router.vuejs.org/zh/guide/essentials/history-mode.htmlapache