595
595
  • 发布:2024-03-12 16:34
  • 更新:2024-03-12 17:17
  • 阅读:270

移动端无法使用vuex

分类:uni-app
sendViewer(viewer) {  
        // 调用逻辑层的方法,将viewer传递给逻辑层  
        console.log('调用sendViewer方法,viewer为:'   viewer);  
        this.$emit('receiveViewer', viewer);  
},

我在带有renderjs的script中引入cesium并初始化viewer,通过调用sendViewer方法来将viewer从renderjs视图层传给逻辑层:

<script>  
    import {  
        mapMutations,  
        mapState  
    } from "vuex";  
    export default {  
        data() {  
            return {}  
        },  
        mounted() {  
            // 监听视图层传递的viewer  
            this.$on('receiveViewer', this.handleViewer);  
            console.log('开始监听数据');  
        },  
        methods: {  
            ...mapMutations(['setViewer']),  
            handleViewer(viewer) {  
                console.log('逻辑层接收到视图层传来的viewer为:'   viewer)  
                // 将viewer上传至Vuex的状态管理  
                this.$store.commit('setViewer', viewer);  
            }  
        },  
    }  
</script>


如图是store/index.js中的代码以及内置浏览器中的运行情况,可以看到正常运行且成功从vuex中存取数据。但是在移动端中只打印出了这一句:调用sendViewer方法,viewer为:[object Object]

代码没有进入script逻辑层中的handleViewer方法,请问各位大佬应该如何解决

2024-03-12 16:34 负责人:无 分享
已邀请:
595

595 (作者)

并且在对viewer进行操作时,打印vuex中的viewer值,h5打印如下:

16:38:40.085 ---------------getViewer---------------- at store/index.js:19
16:38:40.094 拿到的viewer[object Object] at components/BasicFunction.vue:193
16:38:40.101 vuex中的viewer[object Object] at components/BasicFunction.vue:194

app端打印为:
16:38:29.305 ---------------getViewer---------------- at store/index.js:19
16:38:29.306 拿到的viewernull at components/BasicFunction.vue:193
16:38:29.306 vuex中的viewernull at components/BasicFunction.vue:194

锦鲤丶接单丶

锦鲤丶接单丶 - 锦鲤丶接单丶18560000860丶10+年开发经验

把this.$emit和this.$on改成uni.$emit和uni.$on试试呢

  • 595 (作者)

    谢谢老哥,但是报错Uncaught TypeError: uni.$emit is not a function at app-view.js:1455,而且据我所知renderjs中好像用不了uni

    2024-03-12 16:44

锦鲤丶接单丶

锦鲤丶接单丶 - 锦鲤丶接单丶18560000860丶10+年开发经验

那你就在

sendViewer(viewer) {    
        // 调用逻辑层的方法,将viewer传递给逻辑层    
        console.log('调用sendViewer方法,viewer为:'   viewer);    
        this.$emit('receiveViewer', viewer);    

        // 在这里直接更新到store不行吗?为什么还要向上分发  

        this.$store.commit('setViewer', viewer);   

        // 在需要显示的地方直接  ...mapMutations(['viewer']),  使用viewer展示最新数据就行了啊  
},
  • 595 (作者)

    之前已经尝试过了,会报错Uncaught TypeError: Cannot read property 'commit' of undefined at app-view.js:1471


    向上分发是因为使用renderjs后涉及到一个视图层和逻辑层的问题,之前试了好多种传值方法都无法在移动端上实现,我尝试了一下将viewer从带有renderjs的script也就是视图层传给不带renderjs的script也就是逻辑层,再将其传给vuex。移动端不报错了但是通过打印发现代码卡在了这一步:调用sendViewer方法,viewer为:[object Object]

    2024-03-12 17:04

锦鲤丶接单丶

锦鲤丶接单丶 - 锦鲤丶接单丶18560000860丶10+年开发经验

官方已经说的很明白了,APP端不能直接访问逻辑层数据

  • 595 (作者)

    老哥那请问您知道还什么解决办法吗,我希望通过一个组件引入cesium,然后在其他组件中对地图实现了一些功能,这些功能就要对cesium.viewer进行操作,但在移动端这个viewer始终无法传值,用过props %bus window 浏览器上都可以但app不行

    2024-03-12 17:15

锦鲤丶接单丶

锦鲤丶接单丶 - 锦鲤丶接单丶18560000860丶10+年开发经验

我没遇到过这种问题,不过我帮你问了下AI,你可以照着AI给的答案试试,还不行的话我也帮不到什么忙了

  • 595 (作者)

    好的哥,非常感谢。

    2024-03-12 17:19

  • 595 (作者)

    哥我还想请问一下,目前我已经成功在移动端将viewer传给vuex了,请问您知道移动端上的vuex数据不更新应该如何解决吗,还是图上的index.js的代码,目前我移动端打印出来的是:

    18:12:52.667 ---------------setViewer---------------- at store/index.js:12 at app-view.js:1600

    18:12:52.687 --------vuex中的viewer为:[object Object]----------- at store/index.js:14 at app-view.js:1600

    18:12:52.688 Cesium的版本号:1.83 at components/InitCesium.vue:79 at app-view.js:1600

    18:13:31.427 ---------------getViewer---------------- at store/index.js:19

    18:13:31.460 --------vuex中的viewer为:111111111111----------- at store/index.js:20

    18:13:31.460 拿到的viewer111111111111 at components/BasicFunction.vue:192

    18:13:31.461 vuex中的viewer111111111111 at components/BasicFunction.vue:193


    在h5上是:

    18:13:14.013 ---------------setViewer---------------- at store/index.js:12

    18:13:14.016 --------vuex中的viewer为:[object Object]----------- at store/index.js:14

    18:13:14.027 Cesium的版本号:1.83 at components/InitCesium.vue:79

    18:13:19.487 ---------------getViewer---------------- at store/index.js:19

    18:13:19.493 --------vuex中的viewer为:[object Object]----------- at store/index.js:20

    18:13:19.494 拿到的viewer[object Object] at components/BasicFunction.vue:192

    18:13:19.501 vuex中的viewer[object Object] at components/BasicFunction.vue:193


    现在应该是已经传给vuex了,但是移动端取的时候store里的viewer没更新,拿到的还是store初始化中定义的viewer,一串数字1

    2024-03-12 18:17

要回复问题请先登录注册