Vue.js 如何更新单个文件组件';s数据及其方法?

Vue.js 如何更新单个文件组件';s数据及其方法?,vue.js,vuejs2,vue-component,Vue.js,Vuejs2,Vue Component,我正在学习Vue.js,现在我正在学习单文件组件。我的目标是通过地理定位API向用户询问他们的位置,然后用他们的纬度和经度坐标值更新页面。我可以通过console.log获取coords值,但无法让SFC使用这些值更新自身。我可能遗漏了什么 地理定位.vue <template> <p>Your location is: {{ text }}. Is this correct?</p> </template> <script>

我正在学习Vue.js,现在我正在学习单文件组件。我的目标是通过地理定位API向用户询问他们的位置,然后用他们的纬度和经度坐标值更新页面。我可以通过console.log获取coords值,但无法让SFC使用这些值更新自身。我可能遗漏了什么

地理定位.vue

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

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

  export default {
    name: "geolocation",
    data: function () {
      return {
        text: text,
      }
    },
    methods: {
      findLocation: function() {
      let that = this;
      console.log('in 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>
<template>
  <div class="center-text">
      <h1 class="site-title">{{ app_name }}</h1>
      <p>I'm the store page!</p>
      <p>Soon I'll display products according to the inventory at the 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>

您的位置是:{{text}。这是正确的吗

log('in geolocation.vue'); 让text=“不适用”; 导出默认值{ 名称:“地理定位”, 数据:函数(){ 返回{ 文本:文本, } }, 方法:{ findLocation:function(){ 让那=这; log('in findLocation()'); if(导航器.地理位置){ navigator.geolocation.getCurrentPosition(函数(pos)){ log('console:'+pos.coords.latitude+','+pos.coords.longitude'); that.text=pos.coords.latitude+,'+pos.coords.longitude; }); } } } } p{ 颜色:红色; 字号:85%; }
App.vue

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

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

  export default {
    name: "geolocation",
    data: function () {
      return {
        text: text,
      }
    },
    methods: {
      findLocation: function() {
      let that = this;
      console.log('in 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>
<template>
  <div class="center-text">
      <h1 class="site-title">{{ app_name }}</h1>
      <p>I'm the store page!</p>
      <p>Soon I'll display products according to the inventory at the 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>

{{app_name}}
我是商店页面

很快,我将根据该地区配送中心的库存展示产品

从“vuex”导入{mapGetters}; 从“/geolocation”导入地理位置; 导出默认值{ 计算:{ …映射器(['app_name']) }, 组成部分:{ 地理定位 }, 挂载:函数(){ 地理定位.methods.findLocation(); } }; .网站名称{ 字体系列:“龙虾”,草书; } .居中文本{ 文本对齐:居中; }
将其添加到geolocation.vue

删除App.vue中的这些行

将您的组件拆分为多个子组件,它本身就具有循环挂钩。 安装
geolocation
组件后,将调用
findLocation
,并将该位置绑定到模板中。

将其添加到geolocation.vue中

删除App.vue中的这些行

将您的组件拆分为多个子组件,它本身就具有循环挂钩。
安装
geolocation
组件后,将调用
findLocation
,并将该位置绑定到模板中。

解决了我的问题。非常感谢。这解决了我的问题。非常感谢。