首页 > 其他分享 >vue3组合式API

vue3组合式API

时间:2022-10-05 16:26:37浏览次数:70  
标签:组合式 const setup InjectionKey getCurrentInstance API inject key vue3

 

选项 API 生命周期选项和组合式 API 之间的映射

  • beforeCreate -> use setup()

  • created -> use setup()

  • beforeMount -> onBeforeMount

  • mounted -> onMounted

  • beforeUpdate -> onBeforeUpdate

  • updated -> onUpdated

  • beforeUnmount -> onBeforeUnmount

  • unmounted -> onUnmounted

  • errorCaptured -> onErrorCaptured

  • renderTracked -> onRenderTracked

  • renderTriggered -> onRenderTriggered

 

Provide / Inject

provide 和 inject 启用依赖注入。只有在使用当前活动实例的 setup() 期间才能调用这两者。

  • 类型声明:
interface InjectionKey<T> extends Symbol {}

function provide<T>(key: InjectionKey<T> | string, value: T): void

// without default value
function inject<T>(key: InjectionKey<T> | string): T | undefined
// with default value
function inject<T>(key: InjectionKey<T> | string, defaultValue: T): T
// with factory
function inject<T>(
  key: InjectionKey<T> | string,
  defaultValue: () => T,
  treatDefaultAsFactory: true
): T

 

Vue 提供了一个 InjectionKey 接口,该接口是扩展 Symbol 的泛型类型。它可用于在提供者和消费者之间同步注入值的类型:

import { InjectionKey, provide, inject } from 'vue'

const key: InjectionKey<string> = Symbol()

provide(key, 'foo') // 提供非字符串值将导致错误

const foo = inject(key) // foo 的类型: string | undefined

 

如果使用字符串 key 或非类型化 symbols,则需要显式声明注入值的类型:

const foo = inject<string>('foo') // string | undefined

getCurrentInstance

getCurrentInstance 允许访问对高级用法或库创建者有用的内部组件实例.

import { getCurrentInstance } from 'vue'

const MyComponent = {
  setup() {
    const internalInstance = getCurrentInstance()

    internalInstance.appContext.config.globalProperties // access to globalProperties
  }
}

 

 

getCurrentInstance only works during setup or Lifecycle Hooks(仅在设置或生命周期挂钩期间有效)

When using outside of setup or Lifecycle Hooks, please call getCurrentInstance() on setup and use the instance instead.

(在setup或Lifecycle Hooks之外使用时,请在安装程序上调用getCurrentInstance()并改用实例。)

const MyComponent = {
  setup() {
    const internalInstance = getCurrentInstance() // works

    const id = useComponentId() // works

    const handleClick = () => {
      getCurrentInstance() // doesn't work
      useComponentId() // doesn't work

      internalInstance // works
    }

    onMounted(() => {
      getCurrentInstance() // works
    })

    return () =>
      h(
        'button',
        {
          onClick: handleClick
        },
        `uid: ${id}`
      )
  }
}

// also works if called on a composable
function useComponentId() {
  return getCurrentInstance().uid
}

 

 

标签:组合式,const,setup,InjectionKey,getCurrentInstance,API,inject,key,vue3
From: https://www.cnblogs.com/vant-xie/p/16755733.html

相关文章

  • vue3 ref()和reactive()的
    vue3中对响应式数据的声明官方给出了ref()和reactive()这两种方式,今天我们来聊聊两种定义定义数据方式有什么不同如上代码,我们使用变量声明的方式,ref的方式,reactive......
  • Vue3.x 组合式api的生命周期钩子
    Vue3组合式api的生命周期beforeCreatecreated,setup语法糖模式(组合式api)是没有这两个生命周期的,用setup去代替onBeforeMount获取不到DOM,onMounted可以获取DOMon......
  • 干货 | 通用 api 封装实战,带你深入理解 PO
    在普通的接口自动化测试中,如果接口的参数,比如url,headers等传参改变,或者测试用例的逻辑、断言改变,那么整个测试代码都需要改变。apiobject设计模式借鉴了pageobject的设计模......
  • Python-API笔记
    random.seed()&np.random.seed()np.random.seed()函数用于生成指定随机数。seed()被设置了之后,np,random.random()可以按顺序产生一组固定的数组。如果使用相同的se......
  • 文件系统API
                                       ​​何志丹​​​API是应用程序接口(ApplicationProgrammingInterface)的简称,用API函数操作文件......
  • quick-vue3-admin
    quick-vue3-admin是一款免费开源快速搭建中后台系统框架。本框架基于vite2+element-plus等最新主流技术并封装了通用的组件方便开发者提高工作效率。后期也会通过版本升......
  • 07-RabbitMQ核心API-Direct Exchange
    DirectExchange简介所有发送到directexchange的消息被转发到Routekey中指定的Queue注意:Direct模式可以使用RabbitMQ自带的Exchange(defaultexchange),所以不需......
  • 08-RabbitMQ核心API-Topic Exchange
    TopicExchange简介所有发送到TopicExchange的消息被转发到所有关心RouteKey中指定Topic的Queue上Exchange将RouteKey和某Topic进行模糊匹配,此时队列需要绑定一个T......
  • 09-RabbitMQ核心API-Fanout Exchange
    FanoutExchange简介不处理路由键,只需要简单的将队列绑定到交换机上发送到交换机的消息都会被转发到与该交换机绑定的所有队列上Fanout交换机转发消息是最快的......
  • 10-RabbitMQ核心API-其他[Binding, Queue, Message, Virtual host]
    Binding绑定关系Exchange和Exchange,Queue之间的连接关系Binding中可以包含RouteKey或者参数Queue消息队列,实际存储消息数据Durability:是否持久化,Durable......