组件生命周期函数

@Lifetime
[] 封装地图组件 map
  1. 创建组件 - 目录、文件
  2. 地图组件结构 - 指定id和经纬度、中心点;绑定事件;其它略
  3. <view class="map-wrap">
        <map class="map" id="my-map" longitude="{{longitude}}" latitude="{{latitude}}" show-location />
        <image class="center-img" src="/utils/center.png" mode="aspectFill" bind:tap="moveToLocation" />
    </view>
  4. 地图组件逻辑 - 创建地图组件上下文变量,这里声明为页面全局变量,避免使用 this 引用
  5. let mapCtx;
    Component({})
  6. 地图组件逻辑的生命周期函数
  7. lifetimes: {
        attached: function () {
            // 实例化地图组件上下文
            mapCtx = wx.createMapContext('my-map', this)
            // 获取当前位置
            wx.getLocation({
            type: 'gcj02',
            success: res => {
                this.setData({
                    longitude: res.longitude,
                    latitude: res.latitude
                })
            }
            })
        }
    }
  8. 地图组件逻辑的数据 - 经纬度
  9. longitude: '"",
    latitude: ""
  10. 地图组件逻辑的方法 - 返回中心点方法
  11. moveToLocation() {
        mapCtx.moveToLocation({})
    }
  12. 样式 - 略