鲁班H5是基于Vue2.0开发的,通过拖拽的形式,生成页面的工具,类似易企秀百度 H5 等工具

相关地址

  1. GitHub:https://github.com/ly525/luban-h5
  2. Gitee:https://gitee.com/ly525/luban-h5

如何将数据转换成手机端网页(所谓H5页面)? - 图1

核心原理解析

简单的说:借助vue的createElement方法,将json 逐一解析成对应的组件,渲染即可。使用slider插件实现上下或者左右翻页

1. 从 Vue.js 文档中寻求灵感

相信阅读过 Vue JSX 文档 的同学对下面的代码应该不会陌生,可以直接访问 在线Demo

  1. // 以下代码来自:https://cn.vuejs.org/v2/guide/render-function.html#createElement-参数
  2. // @returns {VNode}
  3. createElement(
  4. // {String | Object | Function}
  5. // An HTML tag name, component options, or async
  6. // function resolving to one of these. Required.
  7. 'div',
  8. // {Object}
  9. // A data object corresponding to the attributes
  10. // you would use in a template. Optional.
  11. {
  12. // (see details in the next section below)
  13. },
  14. // {String | Array}
  15. // Children VNodes, built using `createElement()`,
  16. // or using strings to get 'text VNodes'. Optional.
  17. [
  18. 'Some text comes first.',
  19. createElement('h1', 'A headline'),
  20. createElement(MyComponent, {
  21. props: {
  22. someProp: 'foobar'
  23. }
  24. })
  25. ]
  26. )

2. 抽象 Demo

  1. 移除注释
  2. 把 createElement(tagName || componentOptions, {data}, children) 对应到上面的代码中,把 children 部分单独抽象成一个数组
  3. 整理之后的代码如下:
  1. // a component options demo:
  2. const MyComponent = {
  3. props:['someProp'],
  4. render(h) {
  5. return h('span', this.someProp)
  6. },
  7. }
  8. new Vue({
  9. el: '#app',
  10. // 这里的 render(createElement) 我们更常见的写法是:render(h)
  11. // 关于这部分的解释,可以参见: https://segmentfault.com/q/1010000007130348?_ea=17466196
  12. render(createElement) {
  13. const pageJSON = [
  14. 'Some text comes first.',
  15. createElement('h1', 'A headline'),
  16. createElement(MyComponent /** component options */, {
  17. props: {
  18. someProp: 'foobar'
  19. }
  20. })
  21. ]
  22. return h('div', {}, pageJSON)
  23. }
  24. })

3. 再抽象一步

  1. //
  2. new Vue({
  3. el: '#app',
  4. render(h) {
  5. const pageJSON = [
  6. {component: 'span', text: 'Some text comes first.'},
  7. {component: 'h1', text: 'A headline'},
  8. {component: 'MyComponent', data: {props: {someProp: 'foobar'}} }
  9. ]
  10. return h('div', {}, pageJSON.map(ele => {
  11. return h(ele.component, ele.text ? ele.text : ele.data)
  12. }))
  13. }
  14. })

4. 再抽象一步(哎,不对啊,作者,我咋感觉你这一步啥都没做啊😂,你说对了)

  1. //
  2. const PageJSON = [
  3. {component: 'span', text: 'Some text comes first.'},
  4. {component: 'h1', text: 'A headline'},
  5. {component: 'MyComponent', data: {props: {someProp: 'foobar'}} }
  6. ]
  7. new Vue({
  8. el: '#app',
  9. render(h) {
  10. return h('div', {}, pageJSON.map(ele => {
  11. return h(ele.component, ele.text ? ele.text : ele.data)
  12. }))
  13. }
  14. })

5. 再抽象一步

  1. const WorkJSON = {
  2. title: '我是作品标题',
  3. description: '我是作品描述',
  4. created_time: '2019-09-01',
  5. updated_time: '2019-09-01',
  6. pages: [
  7. elements: [
  8. {component: 'span', text: 'Some text comes first.'},
  9. {component: 'h1', text: 'A headline'},
  10. {component: 'MyComponent', data: {props: {someProp: 'foobar'}} }
  11. ],
  12. ],
  13. }
  14. new Vue({
  15. el: '#app',
  16. render(h) {
  17. return h('div', {}, WorkJSON.pages[0].elements.map(ele => {
  18. return h(ele.component, ele.text ? ele.text : ele.data)
  19. }))
  20. }
  21. })

总结

  1. 相信到最后以这一步大家应该有些头绪了吧(要没有的话 😂😂😂,接着往下看吧)
  2. 其实鲁班H5的一个作品其实是一个就是一个大JSON(结构和上面的 WorkJSON 几乎一致)
  3. 这个大JSON 里面包含了很多页面(鲁班源码-Page),每个页面里面包含了很多元素(鲁班源码-Element)
  4. 最终这个JSON 会传给 render(h) 进行解析渲染
  5. 到这里,也就能解答这一小节的问题了:
    1. 鲁班H5 的核心原理是什么??
    2. 如何将数据转换成手机端网页(所谓H5页面)?
    3. JSON 转换成 H5??

完结撒花 🎉🎉🎉🎉🎉🎉🎉🎉