示例一:
请求
// vue页面 获取服务器时间
getServerTime() {
return new Promise((resolve, reject) => {
this.$http.post('getServerTime').then(res => {
if (res.data.code == 1) {
resolve(res.data.data.time)
}
});
})
},
vue页面使用 async await
async startCountdown() {
const nowTime = await this.getServerTime();
console.log(nowTime,'服务器时间')
},
示例二:
在vue页面调用js文件中的方法
@/utils/tool.js 文件
// 获取页面链接 携带的参数
export function getHashParam(param) {
const hash = window.location.hash.substring(1); // 移除开头的'#'
const params = hash.split('&').reduce((acc, curr) => {
const [key, value] = curr.split('=');
acc[key] = value;
return acc;
}, {});
return params[param] ? decodeURIComponent(params[param]) : null;
}
vue页面使用
import { getHashParam } from '@/utils/tool';
mounted() {
const myParam = getHashParam('ip');
console.log('页面链接携带的参数ip=',myParam );
// 替换 %3A
if(myParam.search('%3A')){
this.ip = myParam.replace(/%3A/g, ':');
}else{
this.ip = myParam;
}
// 如果没有端口号,添加默认端口号
if (!this.ip.includes(':')) this.ip += ':8000'
}