vue2 new Promise 在页面中多次 引用通过调用某个接口获取到的数据 async await resolve, 调用js文件中的方法,获取页面链接 携带的参数

示例一:

请求

    // 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'
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值