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.exea.download = "VideoWebPlugin.exe"; //a.style.display = "none"; // 障眼法藏起來a標簽document.body.appendChild(a); // 將a標簽追加到文檔對象中a.click(); // 模擬點擊了a標簽,會觸發a標簽的href的讀取,瀏覽器就會自動下載了a.remove(); // 一次性的,用完就刪除a標簽},},
};
</script>