V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
jaskang
V2EX  ›  程序员

在小程序里用上 Composition API

  •  
  •   jaskang · 2022-09-28 16:30:35 +08:00 · 1232 次点击
    这是一个创建于 574 天前的主题,其中的信息可能已经有所发展或是发生改变。

    众所周知 Vue3 带来了 Composition API ,它使我们能够通过组合函数来实现更加简洁高效的逻辑复用。在选项式 API 中我们主要的逻辑复用机制是 mixins ,而 Composition API 解决了 mixins 的所有缺陷。

    而在小程序中我们有同样的问题,小程序是选项式的,也有着和 Vue2 中 mixins 同样定位的 behaviors

    那么我们能不能把小程序也变成和 Vue3 一样的 Composition API 呢?

    今天给大家介绍的 Rubic 便是这样一款小程序框架,它支持我们用写 Vue3 一样的方式开发小程序。

    下面是一个最基本的组件示例:

    import { defineComponent, ref, computed, onLoad } from 'rubic'
    
    defineComponent({
      setup(props, ctx) {
        const count = ref(0)
        const double = computed(() => count.value * 2)
    
        const increment = () => {
          count.value++
        }
        
        
        return {
          count,
          double,
          increment,
        }
      },
    })
    
    <view bindtap="increment">{{count}} x2:{{double}}</view>
    

    链接

    源码: https://github.com/jaskang/rubic

    文档: https://rubic.jaskang.vip

    3 条回复    2022-09-29 16:16:59 +08:00
    mynameislihua
        1
    mynameislihua  
       2022-09-28 16:54:33 +08:00
    good ,点个 star 支持一下
    lblblong
        2
    lblblong  
       2022-09-28 22:19:53 +08:00
    为啥不用 Taro+vue3
    jaskang
        3
    jaskang  
    OP
       2022-09-29 16:16:59 +08:00
    @lblblong 这个是运行时框架不依赖编译过程。如果没有有多端需求的话会比 Taro 更轻量。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5261 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 09:21 · PVG 17:21 · LAX 02:21 · JFK 05:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.