這里給大家分享我在網上總結出來的一些知識,希望對大家有所幫助
一.布置全域組件
在我們開發的程序中,會碰到一個現象,就是在頁面里面引入組件,總算要寫import,components才能參考,這里給大家分享我們的一個解決方案
1.首先要建立一個components檔案夾,用來放我們的所有組件

2.然后在里面寫好組件

3.來到main.js,在代碼中加入兩行代碼
import movable from "@/components/movable/index.vue";
Vue.component("movable", movable);
這樣我們就能在頁面里,不用寫import,components,就能參考了
二.獲取當前經緯度解決方案
這里給大家分享出一套我使用的獲取當前經緯度的方案
1.小程式設定,去小程式公眾平臺,開啟介面權限

2.代碼中manifest.json檔案以下位置加上代碼

/* 小程式特有相關 */
"mp-weixin" : {
"appid" : "",
"setting" : {
"urlCheck" : false
},
"usingComponents" : true,
"permission" : {
"scope.userLocation" : {
"desc" : "你的位置資訊將用于和門店的距離長度"
}
},
"requiredPrivateInfos" : [ "chooseLocation", "getLocation" ]
},
3.頁面方法分享,分為檢測權限,成功處理,錯誤處理
檢測權限
// 位置授權
getAuthorizeInfo() {
const that = this;
uni.authorize({
scope: 'scope.userLocation',
success() { // 允許授權
that.getLocationInfo();
},
fail() { // 拒絕授權
that.openConfirm();
// console.log("你拒絕了授權,無法獲得周邊資訊")
}
})
},
獲取地理位置
// 獲取地理位置
getLocationInfo() {
const that = this
uni.getLocation({
type: 'wgs84',
success(res) {
uni.setStorageSync("lat", res.latitude)
uni.setStorageSync("lng", res.longitude)
},
fail(res) { // 拒絕授權
console.log(res, '222');
}
});
},
錯誤處理
// 再次獲取授權
// 當用戶第一次拒絕后再次請求授權
openConfirm() {
uni.showModal({
title: '請求授權當前位置',
content: '需要獲取您的地理位置,請確認授權',
showCancel: false,
success: (res) => {
if (res.confirm) {
uni.openSetting(); // 打開地圖權限設定
}
}
});
},
如果對您有所幫助,歡迎您點個關注,我會定時更新技術檔案,大家一起討論學習,一起進步,

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/544993.html
標籤:其他

