demo.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <template>
  2. <div class="demo">
  3. <h2>经典分类模式</h2>
  4. <nut-category :category="category" @change="change">
  5. <nut-categorypane :categoryChild="categoryChild" @onChange="onChange"> </nut-categorypane>
  6. </nut-category>
  7. <h2>只显示文字</h2>
  8. <nut-category :category="category" @change="changeText">
  9. <nut-categorypane type="text" :categoryChild="categoryChild" @onChange="onChange"> </nut-categorypane
  10. ></nut-category>
  11. <h2>自定义</h2>
  12. <nut-category
  13. ><nut-categorypane type="custom" :customCategory="customCategory" @onChange="changeCustom"> </nut-categorypane
  14. ></nut-category>
  15. </div>
  16. </template>
  17. <script lang="ts">
  18. import { createComponent } from '@/packages/utils/create';
  19. const { createDemo } = createComponent('cmt');
  20. import { reactive, toRefs, onMounted } from 'vue';
  21. export default createDemo({
  22. props: {},
  23. setup() {
  24. const data = reactive({
  25. categoryInfo: {},
  26. category: [{}],
  27. categoryChild: [{}],
  28. customCategory: [{}]
  29. });
  30. onMounted(() => {
  31. setTimeout(() => {
  32. getData();
  33. }, 500);
  34. });
  35. const getData = () => {
  36. fetch('//storage.360buyimg.com/nutui/3x/categoryData.js')
  37. .then((response) => response.json())
  38. .then((res) => {
  39. console.log('res', res);
  40. const { categoryInfo, categoryChild, customCategory } = res;
  41. data.categoryInfo = categoryInfo;
  42. data.category = categoryInfo.category;
  43. data.categoryChild = categoryChild;
  44. data.customCategory = customCategory;
  45. })
  46. .catch((err) => console.log('Oh, error', err));
  47. };
  48. const change = (index: any) => {
  49. data.categoryChild = [].concat(data.categoryInfo.category[index + 1].children as any);
  50. };
  51. const changeText = (index: any) => {
  52. data.categoryChild = [].concat(data.categoryInfo.category[index + 1].children as any);
  53. };
  54. const changeCustom = () => {
  55. console.log('点击分类数据');
  56. };
  57. const onChange = () => {
  58. console.log('当前分类数据');
  59. };
  60. return {
  61. change,
  62. onChange,
  63. changeText,
  64. changeCustom,
  65. ...toRefs(data)
  66. };
  67. }
  68. });
  69. </script>
  70. <style lang="scss" scoped>
  71. .demo {
  72. padding-left: 0 !important;
  73. padding-right: 0px !important;
  74. }
  75. </style>