V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
jaskang
V2EX  ›  程序员

在小程序里用上 Composition API

  •  
  •   jaskang · Sep 28, 2022 · 2091 views
    This topic created in 1319 days ago, the information mentioned may be changed or developed.

    众所周知 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 replies    2022-09-29 16:16:59 +08:00
    mynameislihua
        1
    mynameislihua  
       Sep 28, 2022
    good ,点个 star 支持一下
    lblblong
        2
    lblblong  
       Sep 28, 2022
    为啥不用 Taro+vue3
    jaskang
        3
    jaskang  
    OP
       Sep 29, 2022
    @lblblong 这个是运行时框架不依赖编译过程。如果没有有多端需求的话会比 Taro 更轻量。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2643 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 15:55 · PVG 23:55 · LAX 08:55 · JFK 11:55
    ♥ Do have faith in what you're doing.