What is the process for updating the data of a Single File Component using its own method?

Currently, I am diving into the world of Vue.js and exploring Single File Components. My goal here is to utilize the Geolocation API to prompt the user for their location, retrieve their latitude and longitude coordinates, and then display this information on the page. While I have successfully fetched the coordinate values through console.log, I'm encountering an issue updating the SFC with these values. There might be a step I'm overlooking.

geolocation.vue

<template>
  <p>Your current location: {{ text }}. Is this correct?</p>
</template>

<script>
  console.log('inside geolocation.vue');
  let text = "N/A";

  export default {
    name: "geolocation",
    data: function () {
      return {
        text: text,
      }
    },
    methods: {
      findLocation: function() {
      let that = this;
      console.log('inside findLocation()');
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (pos) {
          console.log('CONSOLE: ' + pos.coords.latitude + ', ' + pos.coords.longitude);
          that.text = pos.coords.latitude + ', ' + pos.coords.longitude;
        });
      }
    }
  }
}
</script>

<style scoped>
  p {
    color: red;
    font-size: 85%;
  }
</style>

App.vue

<template>
  <div class="center-text">
      <h1 class="site-title">{{ app_name }}</h1>
      <p>Welcome to the store page!</p>
      <p>Soon products will be displayed based on inventory at distribution center(s) in the area.</p>
      <geolocation/>
  </div>
</template>

<script>
  import {mapGetters} from 'vuex';
  import geolocation from './geolocation';

  export default {
    computed: {
      ...mapGetters(['app_name'])
    },
    components: {
      geolocation
    },
    mounted: function() {
      geolocation.methods.findLocation();
    }
  };
</script>

<style scoped>
  .site-title {
      font-family: 'Lobster', cursive;
  }
  .center-text {
      text-align: center;
  }
</style>

Answer №1

Include the following code snippet in the geolocation.vue

mounted() {
  this.findLocation();
}

Delete these lines from the App.vue file

mounted: function() {
  geolocation.methods.findLocation();
}

Break down your component into multiple child components, each with its own lifecycle hooks. Once the geolocation component is mounted, it will trigger the findLocation function and bind the location data to the template.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering a issue when trying to click the Menu icon in a Vue 3 admin template

i recently set up the CoreUI Vue admin template CoreUI Vue Template. Everything seems to be working smoothly, except for the menu icon https://i.stack.imgur.com/tOShL.png. Whenever I try to hide the side bar (which is a built-in feature), it's not fu ...

Tips for achieving a stable Vuetify configuration

Working on a project using vue 2 and vuetify, initially everything was going smoothly. However, after some usage, I encountered the following error: Module not found: Error: Can't resolve 'vuetify/src/stylus/app.styl' in '/Users/marce ...

What are some tips for incorporating vue.js into a basic web application?

I've been trying to incorporate vue.js into my web application, but for some reason, it's not loading and the HTML is not firing in the browser. Please take a look at the code snippet below: <!DOCTYPE html> <html> < ...

Using mapGetters with both modules and root getters simultaneously: A comprehensive guide

When using mapGetters in Vuex, I can easily call getters from a single root or module. For example, if I have a getter called firstRootGetter in the store/index.js, I would use ...mapGetters(['firstRootGetter']) in my Vue component. However, thi ...

Troubleshooting Cross-Origin Resource Sharing Problem in Vue.js Single Page Application and Node.js API

My Node.js Express app is set up with the cors npm package installed. Additionally, there is basic authentication enabled on the nginx server hosting my Node.js app (authentication requires an 'Authorization' key in the headers of each request). ...

Leveraging the power of the vuejs plugin within the main.js script

My goal is to develop a plugin to manage the OAuth2 token data in my Vue.js application. I followed several tutorials available on the internet to create this plugin. var plugin = {} plugin.install = function (Vue, options) { var authStorage = { ...

Having trouble with your Vue.js 2.0 project? Encountering a Module build failure due to an Unexpected token error while using webpack,

Struggling throughout the day to upgrade from Vue.js 1.X to Vue.js 2.0 resulting in errors (using gulp watch) has left me feeling frustrated. ERROR in ./~/buble-loader!./~/vue-loader/lib/selector.js?type=script&index=0!./resources/assets/js/compon ...

How to update image source in VUE.js after form submission

I am encountering an issue with vue.js2. After submitting a form, I need to refresh the image src. When the form is submitted, a new image containing a chart is generated in the backend. The new chart image replaces the old one, but the URL remains the s ...

Troubleshooting VueJs and vue-i18n issues

Currently, I am utilizing the Webpack CLI Template. As a next step, I proceed to install using the command npm install --save vue-i18n Within my main.js file, I undertake the necessary importation and configuration by setting the locale to "en" import ...

Utilizing Nicknames in a JavaScript Function

I'm dealing with a function that is responsible for constructing URLs using relative paths like ../../assets/images/content/recipe/. My goal is to replace the ../../assets/images section with a Vite alias, but I'm facing some challenges. Let me ...

Managing Uploaded Files using Laravel and Vue.js

Currently, I am in the process of developing a Vue.js + Vuetify + Axios + Laravel framework for creating a dashboard. One of the functionalities I am working on is the user profile, which includes the ability for users to upload a profile picture and their ...

Adding code snippets to VueJS components

Is it possible to manually pull scripts and insert them into VueJS components, such as pulling Bootstrap CDN or other external JS scripts and inserting them directly? Thank you for your help! ...

How to access a custom font stored in the assets folder when mounting a Vue component

Is there a way to load a local font upon mounting? The font is stored in the assets folder and I have attempted different methods. I am utilizing the FontFace API to ensure the font loads prior to using it () mounted () { const myFont = new FontFace(&a ...

Discover the secret to setting a default value in a Vue kendo-dropdownlist component

I have configured a vue kendo dropdownlist control using an array of objects for data population. <kendo-dropdownlist :data-source="months" :data-text-field="'abbrev'" :data-value-field="'value'" v-model="internal.s ...

I am experiencing difficulties with Vue.js following the implementation of vue-router

I'm currently working on developing a single-page application and I've been exploring Vue Router for this project. However, I seem to be facing some issues with my code as I can't seem to view the page where the app should run. After integra ...

Guide on redirecting to a specific Vue-app page using Flask

I am currently working on an application that includes a page that ends with '@' and provides meta information for the page without '@'. For example, if the page '/user/aabb' contains information about the user 'aabb&apos ...

Guide to watching a particular property in an array of objects using Vue

I am currently working with Vue.js version 2.5.2 My goal is to monitor changes in the forms[*].selected array of objects and trigger a function when it changes. I attempted to achieve this by using a for loop to watch each object's selected property ...

Unable to overwrite class in VueJS component

Recently, I've been encountering an issue with over-riding a specific instance of my VueJS Component. Despite my efforts, the component continues to use the default value. The particular value causing trouble is the buttonClass. Interestingly, the ot ...

What is preventing me from utilizing require in vue-router's routes.js file?

Vue-router typically requires the standard setup as outlined below: In main.js, the routes.js file is required and it usually contains code similar to this: //routes.js import Register from './components/Register' import Login from './comp ...

Utilize the vuex store within a try-catch statement

Struggling to handle error messages returned from an API call and store them in the Vuex state. Unsure of the proper approach. In a Vue file, I have the following code within a "method": axios .post("/api/model", this.model) .then(response => ...