GithubHelp home page GithubHelp logo

muskanmahajan37 / composition-api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zhengxs2018/composition-api

0.0 0.0 0.0 277 KB

纯 vue2 代码实现的 composition-api,不依赖 ES6 的 Proxy 对象, 轻量,无依赖。不兼容官方的 @vue/composition-api,理论上只要 vue2 兼容的浏览器都能运行。

Home Page: https://zhengxs2018.github.io/composition-api/

License: MIT License

JavaScript 5.29% TypeScript 94.71%

composition-api's Introduction

@zhengxs/composition-api

TypeScript code style: prettier npm package npm downloads npm downloads Dependency Status DevDependency Status License

纯 vue2 代码实现的 composition-api,不依赖 ES6 的 Proxy 对象, 轻量,无依赖。

不兼容官方的 @vue/composition-api,理论上只要 vue2 兼容的浏览器都能运行。

TS 文档在线演示

快速开始

安装

$ npm i @zhengxs/composition-api --save

使用

import CompositionAPI from '@zhengxs/composition-api'

Vue.use(CompositionAPI)

示例

响应式状态

import { defineComponent, reactive, ref } from '@zhengxs/composition-api'

export default defineComponent({
  setup() {
    // 注意和官方的有区别
    const loading = ref(false)

    const state = reactive({
      msg: 'Vue',
    })

    return {
      loading,
      state,
    }
  },
})

生命周期

import {
  defineComponent,
  onBeforeMount,
  onMounted,
  onBeforeDestroy,
  onDestroyed,
} from '@zhengxs/composition-api'

export default defineComponent({
  setup() {
    onBeforeMount(() => {
      // 挂载前,对应 created
    })

    onMounted(() => {
      // 挂载后,对应 mounted
    })

    onBeforeDestroy(() => {
      // 销毁前,对应 beforeDestroy
    })

    onDestroyed(() => {
      // 销毁后,对应 destroyed
    })
  },
})

数据监听

import {
  defineComponent,
  reactive,
  watchEffect,
} from '@zhengxs/composition-api'

export default defineComponent({
  setup() {
    const list = reactive([])

    watchEffect(() => {
      console.log('length:', list.length)
    })

    function addItem() {
      list.push({
        id: Math.random(),
        text: `item ${list.length}`,
      })
    }

    return {
      list,
      addItem,
    }
  },
})

License

  • MIT

composition-api's People

Contributors

zhengxs2018 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.