problem description
error using internally defined variable values outside the axios.get method. The value of
ipAddress is the current country code of ip, which I want to get from outside of the get method.
tried to define a variable to assign the entire axios.get method to it, but the result was a promise object.
also failed to use global variables. I don"t know if my code was written wrong
related codes
/ / Please paste the code text below (do not replace the code with pictures)
promise
var getIP = axios.get("http://ip-api.com/json").then(function (response) {
var ipAddress = response.data.countryCode
console.log(ipAddress)
}).catch(function (error) {
console.log(error)
})
console.log(getIP)
App.vue:
<script>
export default {
name: "App",
created: function () {
},
ipAddress: ""
}
</script>
event.vue:
import _global from "../App.vue"
created: function () {
Vue.prototype.GLOBAL = _global
axios.get("http://ip-api.com/json").then(function (response) {
_global.ipAddress = response.data.countryCode
}).catch(function (error) {
console.log(error)
})
console.log(this.GLOBAL.ipAddress)
}
when using the global variable method, the value of console.log (this.GLOBAL.ipAddress) is still the null value of ipAddress in App.vue, but every time I change the ipAddresses value in App.vue, the console outputs the correct value once, and then outputs the changed value. Ask for directions = = | |