Table of Contents
reactive
ref
总结
Home Web Front-end Vue.js Detailed explanation of the difference between reactive and ref in vue3 (source code analysis)

Detailed explanation of the difference between reactive and ref in vue3 (source code analysis)

Aug 22, 2022 pm 07:53 PM
vue3 reactive ref

What is the difference between reactive and ref in

vue? The following article will take you deep into the source code to thoroughly understand the difference between reactive and ref in vue3. I hope it will be helpful to you!

Detailed explanation of the difference between reactive and ref in vue3 (source code analysis)

In the daily development of vue3, I found that many people use a shuttle based on their own habitsreactiveorref , although this can meet the requirements, if this is the case, why do we need to design another ref when we already have reactive? What are the actual application scenarios and differences between the two?

And regarding the underlying logic of ref, some people say that the underlying logic of ref is still reactive. Some people say that the bottom layer of ref is class, and value is just an attribute of class. Which of the two statements is correct? Woolen cloth? Is there any basis for this?

With this question, we will go deep into the source code this time and thoroughly understand the difference between reactive and ref in vue3. (Learning video sharing: vue video tutorial)

If you don’t want to see the source code, you can just scroll to the back and read the summary

reactive

Source code address: packages/reactivity/reactive.ts

First let’s take a look at the target used in vue3 ReactiveFlags<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">// 标记目标对象 target 类型的 ReactiveFlags export const enum ReactiveFlags {   SKIP = '__v_skip',   IS_REACTIVE = '__v_isReactive',   IS_READONLY = '__v_isReadonly',   RAW = '__v_raw' } export interface Target {   [ReactiveFlags.SKIP]?: boolean          // 不做响应式处理的数据   [ReactiveFlags.IS_REACTIVE]?: boolean   // target 是否是响应式   [ReactiveFlags.IS_READONLY]?: boolean   // target 是否是只读   [ReactiveFlags.RAW]?: any               // 表示proxy 对应的源数据, target 已经是 proxy 对象时会有该属性 }</pre><div class="contentsignin">Copy after login</div></div>

reactive

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

export function reactive<t>(target: T): UnwrapNestedRefs<t>

export function reactive(target: object) {

  // if trying to observe a readonly proxy, return the readonly version.

  // 如果目标对象是一个只读的响应数据,则直接返回目标对象

  if (target && (target as Target)[ReactiveFlags.IS_READONLY]) {

    return target

  }

  // 创建 observe

  return createReactiveObject(

    target,

    false,

    mutableHandlers,

    mutableCollectionHandlers,

    reactiveMap

  )

}</t></t>

Copy after login
of type Object

target

The reactive function receives a target object. If the target object is read-only, it will return the object directly.

If it is not read-only, it will pass directly. createReactiveObjectCreateobserveObject

createReactiveObject

Don’t be afraid of the long look, Post the complete code of createReactiveObject first, let’s read it in sections

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

/**

 

 * @param target 目标对象

 * @param isReadonly 是否只读

 * @param baseHandlers 基本类型的 handlers

 * @param collectionHandlers 主要针对(set、map、weakSet、weakMap)的 handlers

 * @param proxyMap  WeakMap数据结构

 * @returns 

 */

 

function createReactiveObject(

  target: Target,

  isReadonly: boolean,

  baseHandlers: ProxyHandler<any>,

  collectionHandlers: ProxyHandler<any>,

  proxyMap: WeakMap<target>

) {

 

  // typeof 不是 object 类型的,在开发模式抛出警告,生产环境直接返回目标对象

  if (!isObject(target)) {

    if (__DEV__) {

      console.warn(`value cannot be made reactive: ${String(target)}`)

    }

    return target

  }

  // target is already a Proxy, return it.

  // exception: calling readonly() on a reactive object

  // 已经是响应式的就直接返回(取ReactiveFlags.RAW 属性会返回true,因为进行reactive的过程中会用weakMap进行保存,

  // 通过target能判断出是否有ReactiveFlags.RAW属性)

  // 例外:对reactive对象进行readonly()

  if (

    target[ReactiveFlags.RAW] &&

    !(isReadonly && target[ReactiveFlags.IS_REACTIVE])

  ) {

    return target

  }

  // target already has corresponding Proxy

  // 对已经Proxy的,则直接从WeakMap数据结构中取出这个Proxy对象

  const existingProxy = proxyMap.get(target)

  if (existingProxy) {

    return existingProxy

  }

  // only a whitelist of value types can be observed.

  // 只对targetTypeMap类型白名单中的类型进行响应式处理

  const targetType = getTargetType(target)

  if (targetType === TargetType.INVALID) {

    return target

  }

  // proxy 代理 target

  // (set、map、weakSet、weakMap) collectionHandlers

  // (Object、Array) baseHandlers

  const proxy = new Proxy(

    target,

    targetType === TargetType.COLLECTION ? collectionHandlers : baseHandlers

  )

  proxyMap.set(target, proxy)

  return proxy

}</target></any></any>

Copy after login

First we see that createReactiveObject receives five parameters

1

2

3

4

5

  target: Target,

  isReadonly: boolean,

  baseHandlers: ProxyHandler<any>,

  collectionHandlers: ProxyHandler<any>,

  proxyMap: WeakMap<target></target></any></any>

Copy after login

target Target object

isReadonly Whether it is read-only

baseHandlers Basic type handlers Processing arrays, objects

collectionHandlers Processing set, map, weakSet, weakMap

proxyMap WeakMap data structure stores side effect functions


Here mainly through ReactiveFlags. RAW and ReactiveFlags.IS_REACTIVE determine whether it is responsive data. If so, the object will be returned directly

1

2

3

4

5

6

 if (

    target[ReactiveFlags.RAW] &&

    !(isReadonly && target[ReactiveFlags.IS_REACTIVE])

  ) {

    return target

  }

Copy after login

For those that are already Proxy, then Directly take out the Proxy object from the WeakMap data structure and return

1

2

3

4

  const existingProxy = proxyMap.get(target)

  if (existingProxy) {

    return existingProxy

  }

Copy after login

Here is a check to see if the type of the current target is Object, Array, Map, Set, WeakMap, WeakSet, if none, go directly Return the object without performing responsive processing

1

2

3

4

5

 // 只对targetTypeMap类型白名单中的类型进行响应式处理

  const targetType = getTargetType(target)

  if (targetType === TargetType.INVALID) {

    return target

  }

Copy after login

Verification type logic

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

function getTargetType(value: Target) {

  return value[ReactiveFlags.SKIP] || !Object.isExtensible(value)

    ? TargetType.INVALID

    : targetTypeMap(toRawType(value))

}

 

function targetTypeMap(rawType: string) {

  switch (rawType) {

    case 'Object':

    case 'Array':

      return TargetType.COMMON

    case 'Map':

    case 'Set':

    case 'WeakMap':

    case 'WeakSet':

      return TargetType.COLLECTION

    default:

      return TargetType.INVALID

  }

}

Copy after login

After all pre-verifications are completed, you can useproxy AgenttargetObject

A ternary operator is used here to execute different processing logic# through TargetType.COLLECTION

    ##(set, map, weakSet, weakMap) Use
  • collectionHandlers
  • (Object, Array) Use
  • baseHandlers

1

2

3

4

5

6

7

8

9

// proxy 代理 target

  // (set、map、weakSet、weakMap) collectionHandlers

  // (Object、Array) baseHandlers

  const proxy = new Proxy(

    target,

    targetType === TargetType.COLLECTION ? collectionHandlers : baseHandlers

  )

  proxyMap.set(target, proxy)

  return proxy

Copy after login

Now the execution logic of

createReactiveObject is very clear

It’s not over yet, the last

proxy in createReactiveObject How to proxy target? Here we use baseHandlers as an example and go deep inside baseHandlers to see

baseHandlers

Source code address:

packages/reactivity/baseHandlers.ts

In

reactive.ts we can see that a total of four handlers have been introduced

1

2

3

4

5

6

import {

  mutableHandlers,

  readonlyHandlers,

  shallowReactiveHandlers,

  shallowReadonlyHandlers

} from './baseHandlers'

Copy after login
  • mutableHandlers Variable processing
  • readonlyHandlers Read-only processing
  • shallowReactiveHandlers Shallow observation Processing (only observe the first-level properties of the target object)
  • shallowReadonlyHandlers Shallow observation && read-only
We take

mutableHandlers as an example

1

2

3

4

5

6

7

8

9

10

11

12

// 可变处理

// const get = /*#__PURE__*/ createGetter()

// const set = /*#__PURE__*/ createSetter()

// get、has、ownKeys 会触发依赖收集 track()

// set、deleteProperty 会触发更新 trigger()

export const mutableHandlers: ProxyHandler<object> = {

  get,                  // 用于拦截对象的读取属性操作

  set,                  // 用于拦截对象的设置属性操作

  deleteProperty,       // 用于拦截对象的删除属性操作

  has,                  // 检查一个对象是否拥有某个属性

  ownKeys               // 针对 getOwnPropertyNames,  getOwnPropertySymbols, keys 的代理方法

}</object>

Copy after login
The

get and set here correspond to createGetter() and createSetter()# respectively.

  • ##createGetter()

  • First go to the complete version of the code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

/**

 * 用于拦截对象的读取属性操作

 * @param isReadonly 是否只读

 * @param shallow 是否浅观察

 * @returns 

 */

function createGetter(isReadonly = false, shallow = false) {

  /**

   * @param target 目标对象

   * @param key 需要获取的值的键值

   * @param receiver 如果遇到 setter,receiver 则为setter调用时的this值

   */

  return function get(target: Target, key: string | symbol, receiver: object) {

    // ReactiveFlags 是在reactive中声明的枚举值,如果key是枚举值则直接返回对应的布尔值

    if (key === ReactiveFlags.IS_REACTIVE) {

      return !isReadonly

    else if (key === ReactiveFlags.IS_READONLY) {

      return isReadonly

    else if (

      // 如果key是raw  receiver 指向调用者,则直接返回目标对象。

      // 这里判断是为了保证触发拦截 handle 的是 proxy 本身而不是 proxy 的继承者

      // 触发拦的两种方式:一是访问 proxy 对象本身的属性,二是访问对象原型链上有 proxy 对象的对象的属性,因为查询会沿着原型链向下找

      key === ReactiveFlags.RAW &&

      receiver ===

        (isReadonly

          ? shallow

            ? shallowReadonlyMap

            : readonlyMap

          : shallow

          ? shallowReactiveMap

          : reactiveMap

        ).get(target)

    ) {

      return target

    }

 

    const targetIsArray = isArray(target)

    // 如果目标对象 不为只读、是数组、key属于arrayInstrumentations:['includes', 'indexOf', 'lastIndexOf']方法之一,即触发了这三个方法之一

    if (!isReadonly && targetIsArray && hasOwn(arrayInstrumentations, key)) {

      // 通过 proxy 调用,arrayInstrumentations[key]的this一定指向 proxy

      return Reflect.get(arrayInstrumentations, key, receiver)

    }

 

    const res = Reflect.get(target, key, receiver)

 

    // 如果 key 是 symbol 内置方法,或者访问的是原型对象__proto__,直接返回结果,不收集依赖

    if (isSymbol(key) ? builtInSymbols.has(key) : isNonTrackableKeys(key)) {

      return res

    }

 

    // 不是只读类型的 target 就收集依赖。因为只读类型不会变化,无法触发 setter,也就会触发更新

    if (!isReadonly) {

      track(target, TrackOpTypes.GET, key)

    }

 

    // 如果是浅观察,不做递归转化,就是说对象有属性值还是对象的话不递归调用 reactive()

    if (shallow) {

      return res

    }

 

    // 如果get的结果是ref

    if (isRef(res)) {

      // ref unwrapping - does not apply for Array + integer key.

      // 返回 ref.value,数组除外

      const shouldUnwrap = !targetIsArray || !isIntegerKey(key)

      return shouldUnwrap ? res.value : res

    }

 

    // 由于 proxy 只能代理一层,如果子元素是对象,需要递归继续代理

    if (isObject(res)) {

      // Convert returned value into a proxy as well. we do the isObject check

      // here to avoid invalid value warning. Also need to lazy access readonly

      // and reactive here to avoid circular dependency.

      return isReadonly ? readonly(res) : reactive(res)

    }

 

    return res

  }

}

Copy after login

It looks long, but in the end it is

track()

Dependency collection

track()

There are too many dependency collection contents, and trigger() triggers updates together, so I will write a separate article Article

  • createSetter()

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

/**

 * 拦截对象的设置属性操作

 * @param shallow 是否是浅观察

 * @returns 

 */

function createSetter(shallow = false) {

  /**

   * @param target 目标对象

   * @param key 设置的属性名称

   * @param value 要改变的属性值

   * @param receiver 如果遇到setter,receiver则为setter调用时的this值

   */

  return function set(

    target: object,

    key: string | symbol,

    value: unknown,

    receiver: object

  ): boolean {

    let oldValue = (target as any)[key]

    // 如果模式不是浅观察模式

    if (!shallow) {

      // 拿新值和老值的原始值,因为新传入的值可能是响应式数据,如果直接和 target 上原始值比较是没有意义的

      value = toRaw(value)

      oldValue = toRaw(oldValue)

      // 目标对象不是数组,旧值是ref,新值不是ref,则直接赋值,这里提到ref

      if (!isArray(target) && isRef(oldValue) && !isRef(value)) {

        oldValue.value = value

        return true

      }

    else {

      // in shallow mode, objects are set as-is regardless of reactive or not

    }

    // 检查对象是否有这个属性

    const hadKey =

      isArray(target) && isIntegerKey(key)

        ? Number(key) <p><code>trigger()</code>触发更新</p><h2 id="strong-ref-strong"><strong>ref</strong></h2><p>源码地址:<code>packages/reactivity/src/ref.ts</code></p><p>接收一个可选<code>unknown</code>,接着直接调用<code>createRef()</code></p><pre class="brush:php;toolbar:false">export function ref(value?: unknown) {

  return createRef(value, false)

}

Copy after login

Detailed explanation of the difference between reactive and ref in vue3 (source code analysis)

ref的区别就是在调用createRef()时第二个值传的是true

1

2

3

export function shallowRef(value?: unknown) {

  return createRef(value, true)

}

Copy after login

看一下官方文档上对shallowRef的解释

Detailed explanation of the difference between reactive and ref in vue3 (source code analysis)

createRef

通过isRef()判断是否是ref数据,是则直接返回该数据,不是则通过new RefImpl创建ref数据

在创建时会传两个值一个是rawValue(原始值),一个是shallow(是否是浅观察),具体使用场景可看上面refshallowRef的介绍

1

2

3

4

5

6

7

function createRef(rawValue: unknown, shallow: boolean) {

  // 是否是 ref 数据

  if (isRef(rawValue)) {

    return rawValue

  }

  return new RefImpl(rawValue, shallow)

}

Copy after login
  • isRef()

通过__v_isRef只读属性判断是否是ref数据,此属性会在RefImpl创建ref数据时添加

1

2

3

export function isRef(r: any): r is Ref {

  return Boolean(r && r.__v_isRef === true)

}

Copy after login

RefImpl

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

class RefImpl<t> {

  private _value: T

  private _rawValue: T

 

  public dep?: Dep = undefined

  // 只读属性 __v_isRef 判断是否是ref数据的静态标识

  public readonly __v_isRef = true

 

  constructor(value: T, public readonly _shallow: boolean) {

    this._rawValue = _shallow ? value : toRaw(value)  // 非浅观察用toRaw()包裹原始值

    this._value = _shallow ? value : toReactive(value) // 非浅观察用toReactive()处理数据

  }

 

  get value() {

  // 依赖收集

    trackRefValue(this)

    return this._value

  }

 

  set value(newVal) {

    newVal = this._shallow ? newVal : toRaw(newVal) // 非浅观察用toRaw()包裹值

    // 两个值不相等

    if (hasChanged(newVal, this._rawValue)) {

      this._rawValue = newVal

      this._value = this._shallow ? newVal : toReactive(newVal)

      triggerRefValue(this, newVal) // 触发依赖,派发更新

    }

  }

}</t>

Copy after login

根据RefImpl我们可以看到ref的底层逻辑,如果是对象确实会使用reactive进行处理,并且ref的创建使用的也是RefImpl class实例,value只是RefImpl的属性

在我们访问设置 ref的value值时,也分别是通过getset拦截进行依赖收集派发更新

  • toReactive

我们来看一下toReactive()这个方法,在RefImpl中创建ref数据时会调用toReactive()方法,这里会先判断传进来的值是不是对象,如果是就用reactive()包裹,否则就返回其本身

1

2

export const toReactive = <t>(value: T): T =>

  isObject(value) ? reactive(value) : value</t>

Copy after login
  • trackRefValue

ref的依赖收集方法

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

export function trackRefValue(ref: RefBase<any>) {

  if (isTracking()) {

    ref = toRaw(ref)

    if (!ref.dep) {

      ref.dep = createDep()

    }

    if (__DEV__) {

      trackEffects(ref.dep, {

        target: ref,

        type: TrackOpTypes.GET,

        key: 'value'

      })

    else {

      trackEffects(ref.dep)

    }

  }

}</any>

Copy after login
  • triggerRefValue

ref的派发更新方法

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

export function triggerRefValue(ref: RefBase<any>, newVal?: any) {

  ref = toRaw(ref)

  if (ref.dep) {

    if (__DEV__) {

      triggerEffects(ref.dep, {

        target: ref,

        type: TriggerOpTypes.SET,

        key: 'value',

        newValue: newVal

      })

    else {

      triggerEffects(ref.dep)

    }

  }

}</any>

Copy after login

总结

看完reactiveref源码,相信对本文一开始的几个问题也都有了答案,这里也总结了几个问题:

  • 问:ref的底层逻辑是什么,具体是如何实现的

答:ref底层会通过 new RefImpl()来创造ref数据,在new RefImpl()会首先给数据添加__v_isRef只读属性用来标识ref数据。而后判断传入的值是否是对象,如果是对象则使用toReactive()处理成reactive,并将值赋给RefImpl()value属性上。在访问设置ref数据的value时会分别触发依赖收集派发更新流程。


  • 问:ref底层是否会使用reactive处理数据

答:RefImpl中非浅观察会调用toReactive()方法处理数据,toReactive()中会先判断传入的值是不是一个对象,如果是对象则使用reactive进行处理,不是则直接返回值本身。


  • 问:为什么已经有了reactive还需要在设计一个ref呢?

答: 因为vue3响应式方案使用的是proxy,而proxy的代理目标必须是非原始值,没有任何方式能去拦截对原始值的操作,所以就需要一层对象作为包裹,间接实现原始值的响应式方案。


  • 问:为什么ref数据必须要有个value属性,访问ref数据必须要通过.value的方式呢?

答:这是因为要解决响应式丢失的问题,举个例子:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

// obj是响应式数据

const obj = reactive({ foo: 1, bar: 2 })

 

// newObj 对象下具有与 obj对象同名的属性,并且每个属性值都是一个对象

// 该对象具有一个访问器属性 value,当读取 value的值时,其实读取的是 obj 对象下相应的属性值 

const newObj = {

    foo: {

        get value() {

            return obj.foo

        }

    },

    bar: {

        get value() {

            return obj.bar

        }

    }

}

 

effect(() => {

    // 在副作用函数内通过新对象 newObj 读取 foo 的属性值

    console.log(newObj.foo)

})

// 正常触发响应

obj.foo = 100

Copy after login

可以看到,在现在的newObj对象下,具有与obj对象同名的属性,而且每个属性的值都是一个对象,例如foo 属性的值是:

1

2

3

4

5

{

    get value() {

        return obj.foo

    }

}

Copy after login

该对象有一个访问器属性value,当读取value的值时,最终读取的是响应式数据obj下的同名属性值。也就是说,当在副作用函数内读取newObj.foo时,等价于间接读取了obj.foo的值。这样响应式数据就能够与副作用函数建立响应联系

(Learning video sharing: web front-end development, Basic programming video)

The above is the detailed content of Detailed explanation of the difference between reactive and ref in vue3 (source code analysis). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use tinymce in vue3 project How to use tinymce in vue3 project May 19, 2023 pm 08:40 PM

tinymce is a fully functional rich text editor plug-in, but introducing tinymce into vue is not as smooth as other Vue rich text plug-ins. tinymce itself is not suitable for Vue, and @tinymce/tinymce-vue needs to be introduced, and It is a foreign rich text plug-in and has not passed the Chinese version. You need to download the translation package from its official website (you may need to bypass the firewall). 1. Install related dependencies npminstalltinymce-Snpminstall@tinymce/tinymce-vue-S2. Download the Chinese package 3. Introduce the skin and Chinese package. Create a new tinymce folder in the project public folder and download the

vue3+vite: How to solve the error when using require to dynamically import images in src vue3+vite: How to solve the error when using require to dynamically import images in src May 21, 2023 pm 03:16 PM

vue3+vite:src uses require to dynamically import images and error reports and solutions. vue3+vite dynamically imports multiple images. If vue3 is using typescript development, require will introduce image errors. requireisnotdefined cannot be used like vue2 such as imgUrl:require(' .../assets/test.png') is imported because typescript does not support require, so import is used. Here is how to solve it: use awaitimport

How to refresh partial content of the page in Vue3 How to refresh partial content of the page in Vue3 May 26, 2023 pm 05:31 PM

To achieve partial refresh of the page, we only need to implement the re-rendering of the local component (dom). In Vue, the easiest way to achieve this effect is to use the v-if directive. In Vue2, in addition to using the v-if instruction to re-render the local dom, we can also create a new blank component. When we need to refresh the local page, jump to this blank component page, and then jump back in the beforeRouteEnter guard in the blank component. original page. As shown in the figure below, how to click the refresh button in Vue3.X to reload the DOM within the red box and display the corresponding loading status. Since the guard in the component in the scriptsetup syntax in Vue3.X only has o

How Vue3 parses markdown and implements code highlighting How Vue3 parses markdown and implements code highlighting May 20, 2023 pm 04:16 PM

Vue implements the blog front-end and needs to implement markdown parsing. If there is code, it needs to implement code highlighting. There are many markdown parsing libraries for Vue, such as markdown-it, vue-markdown-loader, marked, vue-markdown, etc. These libraries are all very similar. Marked is used here, and highlight.js is used as the code highlighting library. The specific implementation steps are as follows: 1. Install dependent libraries. Open the command window under the vue project and enter the following command npminstallmarked-save//marked to convert markdown into htmlnpmins

How to select an avatar and crop it in Vue3 How to select an avatar and crop it in Vue3 May 29, 2023 am 10:22 AM

The final effect is to install the VueCropper component yarnaddvue-cropper@next. The above installation value is for Vue3. If it is Vue2 or you want to use other methods to reference, please visit its official npm address: official tutorial. It is also very simple to reference and use it in a component. You only need to introduce the corresponding component and its style file. I do not reference it globally here, but only introduce import{userInfoByRequest}from'../js/api' in my component file. import{VueCropper}from'vue-cropper&

How to use Vue3 reusable components How to use Vue3 reusable components May 20, 2023 pm 07:25 PM

Preface Whether it is vue or react, when we encounter multiple repeated codes, we will think about how to reuse these codes instead of filling a file with a bunch of redundant codes. In fact, both vue and react can achieve reuse by extracting components, but if you encounter some small code fragments and you don’t want to extract another file, in comparison, react can be used in the same Declare the corresponding widget in the file, or implement it through renderfunction, such as: constDemo:FC=({msg})=>{returndemomsgis{msg}}constApp:FC=()=>{return(

How to use vue3+ts+axios+pinia to achieve senseless refresh How to use vue3+ts+axios+pinia to achieve senseless refresh May 25, 2023 pm 03:37 PM

vue3+ts+axios+pinia realizes senseless refresh 1. First download aiXos and pinianpmipinia in the project--savenpminstallaxios--save2. Encapsulate axios request-----Download js-cookienpmiJS-cookie-s//Introduce aixosimporttype{AxiosRequestConfig ,AxiosResponse}from"axios";importaxiosfrom'axios';import{ElMess

How to use defineCustomElement to define components in Vue3 How to use defineCustomElement to define components in Vue3 May 28, 2023 am 11:29 AM

Using Vue to build custom elements WebComponents is a collective name for a set of web native APIs that allow developers to create reusable custom elements (customelements). The main benefit of custom elements is that they can be used with any framework, even without one. They are ideal when you are targeting end users who may be using a different front-end technology stack, or when you want to decouple the final application from the implementation details of the components it uses. Vue and WebComponents are complementary technologies, and Vue provides excellent support for using and creating custom elements. You can integrate custom elements into existing Vue applications, or use Vue to build

See all articles