Tag:微信开发vue.jsjavascript
Article From:https://segmentfault.com/q/1010000012147192
Question:

This project is my usevueThe project is written on WeChat webpage. Now I want to ask WeChat’s location.apiThen I get the latitude and longitude parameters and then request the background interface to get the data. I write it in each component. Now I want to put this method into a common method, but I can call it and ask for the data first, but I can’t get the data in the component of the method, and the following is the code.Write in a separate onejsIn the document

//Location locationImport {getLocationUrl} from'common/config'Import {SaveLocalStorage, GetLocalStorage} from 'Common/localStorage'Import {PasswordPost} from'common/tool'Export function GetLocationPos () {Let _this=thisReturn wx.ready (function () {()Wx.getLocation ({Type:'wgs84The default is the GPS coordinates of WGS84. If we want to return the Mars coordinates directly to openLocation, we can import'gcj02'coordinates.Success: function (RES) {Let data=JSON.stringify (RES)SaveLocalStorage ('_User_Loaction_', data)Let str=res.latitude+','+res.longitudeReturn SendLocationAjax (STR)}})})}Function SendLocationAjax (params) {/ / access to detailed city informationReturn vm.$http.post (getLocationUrl, PasswordPost ({location:params})).Then ((RES) => {/ / console.log (res.data)Let data=JSON.stringify (res.data.data)Vm.$cookie.set ('user_location_info', data, {expires:'1h'}) / / save a 1 hour geographic cookie.Console.log (JSON.parsE (vm.$cookie.get ('user_location_info')))Return res.data})}

Ask how to modify it to get data in the component.

Answer 0:

In the component that you need this set of data, refer to your external JS file that gets the location.

import {GetLocationPos} from 'js File path 'Import {SendLocationAjax} from'js file path '

Next, you need to call these two methods in your life cycle to get the value of your return.

Answer 1:

Pass a callback function in

Answer 2:

GetLocationPosCan this function call directly to return the data?
wx.readyThe callback function function does not define the return return value, and it should be undefined.

Leave a Reply

Your email address will not be published. Required fields are marked *