tabs.nvue 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <script>
  2. import tabContent from './tabContent.nvue'
  3. export default {
  4. props: {
  5. index: {
  6. type: Number,
  7. default: 0
  8. }
  9. },
  10. data() {
  11. return {
  12. tabIndex: this.index
  13. }
  14. },
  15. components: {
  16. tabContent,
  17. },
  18. render(createElement) {
  19. const vnodes = this.$slots.default;
  20. const newVNodes = []
  21. if (vnodes && vnodes.length) {
  22. for (let i = 0; i < vnodes.length; i++) {
  23. let vnode = vnodes[i]
  24. if (!vnode || !vnode.componentOptions) {
  25. continue
  26. }
  27. if (vnode.componentOptions.tag === 'tab-content') {
  28. const newVNode = createElement('tab-content', {
  29. staticClass: vnode.data.staticClass,
  30. 'class': vnode.data['class'],
  31. style: vnode.data.style
  32. }, vnode.componentOptions.children)
  33. if (!newVNode.data) {
  34. newVNode.data = Object.create(null)
  35. }
  36. if (!newVNode.data.attrs) {
  37. newVNode.data.attrs = Object.create(null)
  38. }
  39. if (!newVNode.data.props) {
  40. newVNode.data.props = Object.create(null)
  41. }
  42. if (!newVNode.data.on) {
  43. newVNode.data.on = Object.create(null)
  44. }
  45. newVNode.data.attrs.index = this.index
  46. newVNode.data.on.change = this._change
  47. newVNodes.push(newVNode)
  48. }
  49. if (vnode.componentOptions.tag === 'tab-bar') {
  50. if (!vnode.componentOptions.listeners) { //监听子元素传递过来的事件
  51. vnode.componentOptions.listeners = Object.create(null)
  52. }
  53. vnode.componentOptions.listeners._tabBarClick = this._tabBarClick;
  54. newVNodes.push(vnode)
  55. }
  56. }
  57. }
  58. var newNode = createElement('div', {
  59. style: {
  60. flex: 1,
  61. flexDirection: 'column',
  62. },
  63. on: {
  64. change2: this._change2
  65. }
  66. }, newVNodes);
  67. return newNode;
  68. },
  69. methods: {
  70. _tabBarClick(e) {
  71. this.tabIndex = e.index;
  72. this.$emit('change', e);
  73. },
  74. _change(e) {
  75. if (this.tabIndex === e.index) {
  76. return;
  77. }
  78. this.tabIndex = e.index;
  79. this.$emit('change', {
  80. index: e.index,
  81. change:true
  82. })
  83. }
  84. }
  85. }
  86. </script>