laixiansheng
laixiansheng
  • 发布:2020-09-30 14:25
  • 更新:2022-09-29 16:54
  • 阅读:2253

【报Bug】onLaunch 中调用不了 methods 中的函数

分类:uni-app

产品分类: uniapp/App

PC开发环境操作系统: Windows

PC开发环境操作系统版本号: win10 教育版

HBuilderX类型: 正式

HBuilderX版本号: 2.8.13

手机系统: Android

手机系统版本号: Android 5.1

手机厂商: pda

手机机型: EA600

页面类型: nvue

打包方式: 离线

项目创建方式: HBuilderX

操作步骤:
<script>  
    import {  
        mapMutations  
    } from 'vuex'  
    import request from './common/request/index'  
    import cache from './common/cache.js'  
    import tools from '@/common/tools.js'  
    import utils from '@/common/utils.js'  

    export default {  
        globalData: {  
            $tools: tools,  
            $request: request  
        },  
        methods: {  
            ...mapMutations,  
                        isIos(){  
                           return true;  
                        },  
            isAndroid() {  

            }  
        },  
        onLaunch() {  
            console.log('on Launch', this)  
            console.log(this.isIos());  
            uni.getSystemInfo({  
                success: (res) => {  
                    console.log(res);  
                    this.setSystemInfo(res)  
                }  
            })  
            plus.screen.lockOrientation('portrait-primary')  

        },  
        onError: function(err) {  

        },  
        onShow: function() {  
            console.log('App Show')  
        },  
        onHide: function() {  
            console.log('App Hide')  
        }  
    }  
</script>

预期结果:

tue

实际结果:

TypeError: this.isIos is not a function

bug描述:

在app.vue 中 onLaunch 中调用 methods 中的函数控制台打印

 TypeError: this.isIos is not a function

并且 globalData 赋值 在其它页面也获取不到函数中的方法。

console.log(getApp().globalData.$tools);

输出:{}

估计是uni的生命周期比vue的早导致

2020-09-30 14:25 负责人:无 分享
已邀请:
laixiansheng

laixiansheng (作者)

有没有大神能帮忙解决一下呀

1***@qq.com

1***@qq.com

success中拿不到vue中的this,可以用that存储this

onLaunch() {
let that = this;
uni.getSystemInfo({
success: (res) => {
console.log(res);
that.setSystemInfo(res)
}
})
plus.screen.lockOrientation('portrait-primary')
}

该问题目前已经被锁定, 无法添加新回复