首先在src的utils文件夹中创建个base64.js (这个文件随便创建,在哪都行,只要你能找到)
base64.js:
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) {
// Going backwards: from bytestream, to percent-encoding, to original string.
return decodeURIComponent(atob(str).split('').map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
}
}
export default Base64
然后再main.js中引入
// 引入base64加密
import Base64 from '@/utils/base64.js'
//(选填)
// 将引入的Base64 挂在到this上
Vue.provide('Base64', Base64)
在进行跳转的地方使用:如在this.$router.push()的时候
//此处使用的是name跳转,
//还可以使用path跳转,用法都一样
this.$router.push(
{
name:'/home',
params:{
navigatorID:this.Base64.encode('navigatorID')
}
}
)