1.新建一個文件
加入代碼
const Base64 = {
//加密
encode(str) {
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
function toSolidBytes(match, p1) {
return String.fromCharCode('0x' + p1);
}));
},
//解密
decode(str) {
return decodeURIComponent(atob(str).split('').map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
}
}
export default Base64
2.在主體文件main.js中進行設置(引入和暴露出來)
import Base64 from "./assets/js/base64.js" 路徑寫法:
Vue.prototype.$Base64 = Base64; 暴露:
3.在需要加密的頁面中進行加密:
加密的方式有兩種:
3-1單個數據加密
this.$Base64.encode()
3-2.多數據加密(對象)
this.$Base64.encode(JSON.stringify(params))
4.在對應的文件中解密
解密也分兩種:
4-1.單個數據解密
this.$Base64.decode()
4-2.多個數據解密(對象)
JSON.parse(this.$Base64.decode(this.$route.query.id))