Currently, I am attempting to retrieve an external JavaScript file that contains a useful helper function which I want to implement in my Vue component. My goal is to make use of resources like and https://www.npmjs.com/package/vue-plugin-load-script. This endeavor is inspired by a helpful article that can be found at: https://www.sitepoint.com/geo-location-2-lines-javascript/
The main objective here is to access the city location of the current user for a weather application. In case there are no results, I would like to default to a predetermined city. The specific function within the external script that interests me is function geoplugin_city()
, which provides the string representation of the current city, such as "Sydney".
My Vue component contains the following code snippet:
mounted () {
// Retrieve the script
this.$loadScript("http://www.geoplugin.net/javascript.gp")
.then(() => {
// Utilize the script
var city = geoplugin_city();
this.getWeather(city); // --> execute my custom weather function using the city obtained from the script file
})
.catch(() => {
// Failed to fetch the script
this.getWeather("Sydney"); // --> execute my custom weather function with the predetermined city
});
}
Any assistance provided would be greatly appreciated! :)