code.js 1020 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. Vue.component('tabs', {
  2. template: `
  3. <div>
  4. <div class="tabs">
  5. <ul>
  6. <li v-for="tab in tabs" :class="{ 'is-active': tab.isActive }">
  7. <a :href="tab.href" @click="selectTab(tab)">{{ tab.name }}</a>
  8. </li>
  9. </ul>
  10. </div>
  11. <div class="tabs-details">
  12. <slot></slot>
  13. </div>
  14. </div>
  15. `,
  16. created() {
  17. this.tabs = this.$children;
  18. },
  19. data() {
  20. return {
  21. tabs: [],
  22. };
  23. },
  24. methods: {
  25. selectTab(selectedTab) {
  26. this.tabs.forEach(tab => {
  27. tab.isActive = (tab.name === selectedTab.name);
  28. });
  29. }
  30. },
  31. });
  32. Vue.component('tab', {
  33. computed: {
  34. href() {
  35. return '#' + this.name.toLowerCase().replace(/ /g, '-');
  36. }
  37. },
  38. data() {
  39. return {
  40. isActive: false
  41. };
  42. },
  43. mounted() {
  44. this.isActive = this.selected;
  45. },
  46. props: {
  47. name: { required: true },
  48. selected: { default: false },
  49. },
  50. template: `
  51. <div v-show="isActive"><slot></slot></div>
  52. `,
  53. });
  54. const app = new Vue({
  55. el: '#root',
  56. data: {
  57. },
  58. });