用Vuex存储可配置下载的ip地址(用XML进行ajax请求配置文件)

1.在public文件夹下创建一个名为Configuration的文件在创建一个Configuration.txt里面就放IP地址(这里的名字可以随便命名一定性的被人解读文件含义)
例如:

http://172.171.208.1:8003

2.在store文件夹中创建一个名为 ajaxModule.js 的 Vuex 模块:


const state = {
    changeIp: ''//参数
};
const mutations = {
    setChangeIp(state, ip) {
        state.changeIp = ip;
    }
};
const getters = {
    changeIp: state => state.changeIp,
};
const actions = {
    fetchChangeIp({ commit }) {
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (this.readyState == 4 && this.status == 200) {
                var Changeid = this.responseText; 
                commit('setChangeIp', Changeid);
            }
        };
        xhttp.open("GET", "../../Configuration/Configuration.txt", true);
        xhttp.send();
    }
};
export default {
    namespaced: true,/// 添加这一行启用命名空间
    state,
    mutations,
    actions,
    getters
};

3.在您的store中index.js引入ajaxModule模块

import Vue from 'vue'
import Vuex from 'vuex'
import ajaxModule from './ajaxModule';

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
  },
  getters: {
  },
  mutations: {
  },
  actions: {
  },
  modules: {
    ajax:ajaxModule//引入模块
  }
})

4.在全局页面中调用该 Vuex 模块:

<template>
  <div>
    <p>Change IP: {{ changeIp }}</p>
  </div>
</template>

<script>
import { mapActions, mapGetters } from "vuex"; //引入vuex模块

export default {
  computed: {
    // 添加命名空间 'ajax' 到 mapGetters
    ...mapGetters("ajax", ["changeIp"]),
  },
  methods: {
    // 同样,添加命名空间 'ajax' 到 mapActions
    ...mapActions("ajax", ["fetchChangeIp"]),

    frontDownload() {
      var a = document.createElement("a"); // 创建一个<a></a>标签
      var peizhiurl = this.changeIp; //获取到配置文件的ip地址
      a.href = peizhiurl + "/ViebPlugin.exe"; //拼接起来就可以了http://172.17.208.1:8003/ViebPlugin.exe
      a.download = "VideoWebPlugin.exe"; //
      a.style.display = "none"; // 障眼法藏起来a标签
      document.body.appendChild(a); // 将a标签追加到文档对象中
      a.click(); // 模拟点击了a标签,会触发a标签的href的读取,浏览器就会自动下载了
      a.remove(); // 一次性的,用完就删除a标签
    },
  },
};
</script>
  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张清悠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值