vue 本地開發時使用localhost與ip訪問
阿新 • • 發佈:2018-09-25
port || tst str 刪除cookie 本地ip name spl etc
修改config文件夾下面的index.js配置,將localhost改為0.0.0.0就可以了。用ip,127.0.0.1,localhost均行
host: ‘0.0.0.0‘, // can be overwritten by process.env.HOST
問題: 使用本地ip時訪問發現登陸不上,使用localhost反而可以,後查明是cookit存入時存入是domain限制了,用內網ip如192.168.88.34:8080存不了cookei
// 創建 獲取 刪除cookie export default { install (Vue) { Vue.prototype._COOKIE = { // 創建cookie setCookie (name, value, times) { times = times || 36500 var exp = new Date() exp.setTime(exp.getTime() + times * 24 * 60 * 60 * 1000) let domain = ‘.‘ + location.host.split(‘.‘)[1] + ‘.‘ + location.host.split(‘.‘)[2] if (location.host.split(‘.‘)[2]) { document.cookie = name + ‘=‘ + escape(value) + ‘;expires=‘ + exp.toGMTString() + ‘;domain=‘ + domain + ‘;path=/‘
// 不加domain使用內網ip才可能存上 // document.cookie = name + ‘=‘ + escape(value) + ‘;expires=‘ + exp.toGMTString() + ‘;path=/‘ } else { document.cookie = name + ‘=‘ + escape(value) + ‘;expires=‘ + exp.toGMTString() + ‘;path=/‘ } }, // 獲取cookie getCookie (name) { let arrd = null let reg = new RegExp(‘(^| )‘ + name + ‘=([^;]*)(;|$)‘) if (document.cookie.match(reg)) { arrd = document.cookie.match(reg) return unescape(arrd[2]) } else { return null } }, // 刪除cookie removeCookie (name) { let domain = ‘.‘ + location.host.split(‘.‘)[1] + ‘.‘ + location.host.split(‘.‘)[2] if (location.host.split(‘.‘)[2]) { document.cookie = name + ‘="";expires=Thu, 01 Jan 1970 00:00:01 GMT;domain=‘ + domain + ‘;path=/‘ } else { document.cookie = name + ‘="";expires=Thu, 01 Jan 1970 00:00:01 GMT;path=/‘ // document.cookie = name + ‘=‘ + escape(value) + ‘;expires=‘ + exp.toGMTString() + ‘;path=/‘ } } } } }
vue 本地開發時使用localhost與ip訪問