Vue3 开发新范式,不用`ref/reactive`,不用`ref.value`

查看 256|回复 32
作者:zhennann   
什么是 Cabloy-Front ?
Cabloy-Front 是一款支持 IOC 容器的 Vue3 框架。不用ref/reactive,不用ref.value,不用pinia
与 UI 库的配合
Cabloy-Front 可以搭配任何 UI 库使用,并且内置了几款 UI 库的项目模版,便于开箱即用,包括:
  • antdv
  • element-plus
  • quasar
  • vuetify

    特性
    Cabloy-Front 为 Vue3 引入了以下鲜明特征:
  • 不用 ref/reactive:因为在大多数场景下,不需要使用 ref 和 reactive
  • 不用 ref.value:因为在 Cabloy-Front 中定义响应式变量更加直观,不再需要 ref 语义
  • 不用 pinia:因为 Cabloy-Front 提供了 IOC 容器,可以更加灵活的定义和使用全局对象

    动图演示

    演示:不用ref/reactive,不用ref.value
    1. 定义响应式状态
    @Local()
    export class MotherPageCounter extends BeanMotherPageBase {
      counter: number = 0;
      inrement() {
        this.counter++;
      }
      decrement() {
        this.counter--;
      }
    }
    2. 使用响应式状态
    @Local()
    export class RenderPageCounter extends BeanRenderBase {
      render() {
        return (
          
            counter(ref): {this.counter}
             this.inrement()}>Inrement
             this.decrement()}>Decrement
          
        );
      }
    }
    演示:依赖注入
    1. 逻辑抽离
    将counter逻辑抽离出来,创建一个Counter Bean
    @Local()
    export class LocalCounter extends BeanBase {
      counter: number = 0;
      inrement() {
        this.counter++;
      }
      decrement() {
        this.counter--;
      }
    }
    2. 在组件中注入并使用
    @Local()
    export class MotherPageCounter extends BeanMotherPageBase {
      @Use()
      $$counter: LocalCounter;
    }
    @Local()
    export class RenderPageCounter extends BeanRenderBase {
      render() {
        return (
          
            counter(ref): {this.$$counter.counter}
             this.$$counter.inrement()}>Inrement
             this.$$counter.decrement()}>Decrement
          
        );
      }
    }
    框架已开源: https://github.com/cabloy/cabloy-front
  • sun2920989   
    按理说应该放到推广节点
    sjhhjx0122   
    我也写过一个这方面的库,后来觉得只要提供依赖注入,其他还是标准 vue 的范式比较好
    zhennann
    OP
      
    @sjhhjx0122 思路是一致的。对于组件的类型导出,仍然延用 vue3 setup 的特性。依赖注入主要是面向业务层面。
    Terry05   
    等等后续
    linzhe141   
    都这样了,那为什么不去用 ng
    FrankFang128   
    extends 太多了,不喜欢
    nulIptr   
    光看你截图这 class 写法,这 render 函数,有点像 react 。。。。
    相当于整个把 vue 的范式都变了,新框架+1
    ixoy   
    都魔改成这样了,直接 react 。Vue3 定义数据都搞这样。真无语。
    zhennann
    OP
      
    @linzhe141 ng 的 ioc 太繁琐了。cabloy-front 采用依赖注入和依赖查找相结合的方式,大量减少装饰器函数的使用,让代码更加简洁,再加上 vue3 的响应式和 tsx 的灵活性,整体效果比 ng 好太多。
    您需要登录后才可以回帖 登录 | 立即注册

    返回顶部