在项目开发中需要调用一些工具类方法,所以需要将公共方法放在公共js中,并且需要全局引用这些公共js
1:创建公共JS(utils.js) src/common/utils.js
export default { text(){ console.log("测试,测试!!!") } }
2:在main.js中引入公共js并注册
import Utils from './common/utils.js'; Vue.prototype.utils=Utils;
3:调用公共方法
this.utils.text();
也可以一个一个方法注册:
1:utils.js
import Vue from 'vue'
import CryptoJS from 'crypto-js'
export function encrypt(word, keyStr){//加密 keyStr = keyStr ? keyStr : 'abcdefgabcdefg12'; var key = CryptoJS.enc.Utf8.parse(keyStr);//Latin1 w8m31+Yy/Nw6thPsMpO5fg== var srcs = CryptoJS.enc.Utf8.parse(word); var encrypted = CryptoJS.AES.encrypt(srcs, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7}); return encrypted.toString(); } export function decrypt(word, keyStr){//解密 keyStr = keyStr ? keyStr : 'abcdefgabcdefg12'; var key = CryptoJS.enc.Utf8.parse(keyStr);//Latin1 w8m31+Yy/Nw6thPsMpO5fg== var decrypt = CryptoJS.AES.decrypt(word, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7}); return CryptoJS.enc.Utf8.stringify(decrypt).toString(); }
2:在main.js中引入并注册
import Utils from './common/utils.js'; Vue.prototype.$encrypt=encrypt; Vue.prototype.$decrypt=decrypt;
3:调用
this.$encrypt("欢迎登录","siR6WLQ9pPE0738Y"); var aa=this.$encrypt("欢迎登录","siR6WLQ9pPE0738Y"); this.$decrypt(aa,"siR6WLQ9pPE0738Y");