1.引入模块
2.调用模块抛出的方法
<script type="module">
import tools from "./tools.js"
tools.typeOf("123")
</script>
1.main中引入模块
2.挂载在vue的原型链上
import p from './libs/tools.js';
Vue.prototype.p=p;
组件中使用
this.p.typeOf("123")
Ajax(method, url, data, flag)
method:方法("post"/"get")
url:请求地址
data:请求数据
this.p.Ajax("post","http://192.168.2.2/api/login",{
"user_name":"admin",
"user_password":"123",
}).then(function(data){
//成功返回值
console.log(data)
}).catch(function(err){
//错误返回值
console.log(err)
})
setCookie(name, value, Hours)
name:cookies名称
value:cookies值
Hours:有效时间(单位:小时)
this.p.setCookie("name","abc",3)
getCookie(name)
name:cookies名称
delCookie(name)
name:cookies名称
isPhoneNumber(v,t)
v:号码
t:标识("phone":检测手机格式,"tel":检测固话格式,无则符合手机号或固话即可)
this.p.isPhoneNumber("13248979","phone")
this.p.isMobileUserAgent()
this.p.isAndroidMobileDevice()
this.p.isAppleMobileDevice()
timeFormat(time)
time:时间戳
this.p.timeFormat(1619321487923)
transform(tranvalue)
tranvalue:数值字符串
this.p.transform("120.5")
getQueryString(url, param)
url:url地址
param:参数名
this.p.getQueryString("http://192.xxxxx","name")
let ws = new WebSocket("ws://xxxxxx");
this.p.setWs(ws);
this.p.ws.onopen = function() {
console.log("打开websocket")
}
this.p.ws.onerror = function(ev) {
}
this.p.ws.onclose = function(ev) {
// 关闭 websocket
console.log("连接已关闭...");
}
this.p.ws.onmessage = function(res) {
console.log("App.vue收到服务器内容", res.data);
}
//发送消息时
if(this.p.ws.readyState == 1){
console.log('发送keepalve')
this.p.sendMsg({
"type":"keepalive"
})
}