vuex对结构不确定的复杂obj的处理方案?

问题:已知要实现一个模板生成系统,此系统中,用户会选择模板,然后上传图片、文字、视频,生成一个新的模板。这个新的模板可以用来投放到iOS, android,H5各个端,用来生成原生或H5页面。每个模板其实都是一个json文件,标识了一个h5页面的所有信息。换言之,一个模板就是一个h5页面的json表达。
json文件如下:

{
    "head": {
        "version": "0.1.0",
        "width": 750,
        "height": 1334,
        "bgColor": "0x000000",
        "landscape": 0,
    },
    "body": {
        "view": "scrollView",
        "subviews": [{
                "view": "page",
                "x": 0,
                "y": 0,
                "width": 750,
                "height": 1334,
                "subviews": [{
                        "view": "video",
                        "x": 0,
                        "y": 0,
                        "width": 750,
                        "height": 422,
                        "custom": {
                            "videoUrl": "http://wxsnsdy.video.qq.com/105/20210/snsdyvideodownload?filekey=30270201010420301e0201690402534804107cc63287148fa491fbf676c7ef5768520203191d970400&hy=SH&storeid=32303137303131373037353131303030303961616365313336666664393330343561333230613030303030303639&bizid=1023",
                            "cover": ""
                        },
                        "subviews": []
                    },
                    {
                        "view": "image",
                        "x": 0,
                        "y": 422,
                        "width": 750,
                        "height": 912,
                        "subviews": [],
                        "custom": {
                            "imgUrl": "http://vweixinthumb.tc.qq.com/109/20204/snsvideodownload?filekey=30270201010420301e02016d040253480410efffdcf15893248926716cd72b710df002030134320400&hy=SH&storeid=32303137303131393031333335303030303639653534313336666664393330333561333230613030303030303664&bizid=1023"
                        }
                    }
                ]
            },
            ...
        ]
    }
}

json文件由于一一对应不同的h5页面,所以他们的结构是不固定的,首先是page的数目不定,有可能是3个page, 有些是4、5、6、7...其次每个page里面,组件的个数、种类和次序也是不确定的。

系统的demo稿如下:
系统demo稿

当前这个页面就是用户选择了一个模板(json)自动生成的,当点击图片和视频之后,就会触发上传操作,并用cdn上的url替换当前的图片,用户点击了保存之后,就会上传修改后的json,并返回修改后的json在cdn上的链接。

系统的结构图如下:
系统组件结构图

Lander是整个H5页面,LEditor是上面系统demo稿,修改json的部分,LPreview是未来要实现的预览部分,将H5页面实际的渲染在系统的右边。LEditor和LPreview都是Page组件,Page组件又由多个其他子组件组成。

重点来了:
因为涉及到多个父子、兄弟组件间的交互,如果直接使用event、props和event bus的方式,会使得状态非常的复杂,所以想使用vuex做统一的状态处理。但是因为json的结构是不确定的,所以初始化mutation只能写成这个样子:

mutations: {
  change (state, json) {
    state.json = json
  }
}
store.commit('change', json)

那么问题就变成了子组件如何找到自己处于json数据对象的某一部分的问题————这样子组件才能修改自己修改整个json数据。在我的理解中,这个时候只能通过父子组件关系,1对1的将index传递下去,比如说一个图片要修改imgURL, 就要得到这个组件是第2页的第1个轮播图中的第2个图片,这种写法会很复杂和繁琐。

我想知道是否有更好的解决方案

阅读 3.2k
2 个回答

最终我采取了传index的实现方案,现在系统开发完成了,没有问题,从灵活性上也可以接受。

方案:
父组件:

<div v-if="data.subviews.length">
            <div v-for="(component, cIndex) in data.subviews" :key="component.id">
                <div v-if="component.view == 'video'">
                    <EVideo :data="component" :cIndex="cIndex" :pIndex="pIndex"/>
                </div>
                <div v-if="component.view == 'image' || component.view == 'landscape'">
                    <EImage :data="component" :cIndex="cIndex" :pIndex="pIndex"/>
                </div>
                <div v-if="component.view == 'gallery'">
                    <EGallery :data="component" :cIndex="cIndex" :pIndex="pIndex"/>
                </div>
                <div v-if="component.view == 'button'">
                    <EButton :data="component" :cIndex="cIndex" :pIndex="pIndex"/>
                </div>
            </div>
        </div>

子组件:

export default {
    props: ['data', 'pIndex', 'cIndex'],
    computed: {
        backgroundImage () {
            return 'url(' + this.data.custom.imgUrl + ')';
        }
    },
    methods: {
        successHandler (response, file, fileList) {
            let tempData = this.$store.state.data;
            tempData.body.subviews[this.pIndex].subviews[this.cIndex].custom.imgUrl = response.data.Fcdn_url;
            this.$store.commit('change', tempData);
        }
    }
}

你是怎么知道要修改的是哪个组件呢?通过点击吗?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进