在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/網(wǎng)絡(luò)安全  HTML/ 基于vue腳手架的多頁結(jié)構(gòu),如何才能支持vue-router的history模式

基于vue腳手架的多頁結(jié)構(gòu),如何才能支持vue-router的history模式?

問題描述

基于vue-cli 2.x.x 通過修改webpack配置實(shí)現(xiàn)多頁結(jié)構(gòu)(如何配置網(wǎng)上很多博客說明,如有需要我再貼出來),但目前此結(jié)構(gòu)的各個(gè)單頁只能使用vue-router默認(rèn)的hash模式,無法使用history模式。

問題補(bǔ)充1

如果無法通過寫改webpack / vue-loader等配置實(shí)現(xiàn)(development & product),也可以提出通過nginx配置代理轉(zhuǎn)發(fā)或其他方式(product)。

nginx代理方式目前遇到的問題是:如果使用history模式,訪問url雖然正常但不能進(jìn)行刷新操作(try_files有添加)。

問題補(bǔ)充2

多頁模式 + 開發(fā)時(shí)(history模式將無法訪問路由)
http://localhost:8011/main.html#/admin/customer/detail?test=101039

多頁模式 + 生產(chǎn)時(shí)(通過nginx可實(shí)現(xiàn),history模式刷新會404)
http://localhost:8011/#/admin/customer/detail?test=101039
或者
http://localhost:8011/mian/#/admin/customer/detail?test=101039

單頁模式 + 開發(fā)時(shí)/生產(chǎn)時(shí) + history (期望的url結(jié)構(gòu))
http://localhost:8011/admin/customer/detail?test=101039

問題補(bǔ)充3

nginx生效配置

    server {
        listen       9009;
        server_name  localhost;
        
        #字符集
        charset utf-8;

        location / {
            root D:/本機(jī)軟件/TortoiseSVN/repository/iop/iop-view/dist;
            index main.html;
            try_files $uri $uri/ /main.html;
        }

        location ^~ /login/ {
            proxy_pass http://127.0.0.1:9009/login.html;
        }        
    }   
回答
編輯回答
乖乖噠

也可以考慮用 nuxt 吧,能力還更多

2017年7月30日 03:31
編輯回答
荒城

使用history模式很簡單,官網(wǎng)有文檔,需要前后端配合使用:

第一,前端配置mode方式

const router = new VueRouter({
  mode: 'history',
  routes: [...]
})

第二,因?yàn)榈刂窓诘膗rl勢必先通過后端解析,如果后端不認(rèn)識url,就會報(bào)錯(cuò)返回404,所以后端需要配置一下兼容錯(cuò)誤,指向 某個(gè)特定頁面,比如首頁的設(shè)定。

nginx方式

location / {
  try_files $uri $uri/ /index.html;
}

node/express
推薦使用 connect-history-api-fallback 中間件

2017年3月7日 21:16
編輯回答
幼梔

樓上答的應(yīng)該沒問題,如果使用正常不能刷新的話,多半是路徑錯(cuò)了,你可以看一下 error_log 做判斷。

2017年6月18日 05:27