Table of Contents
Responsive syntactic sugar is a unique feature of
You Yuxi personally gave the reason for abandonment 2 weeks ago (10:05 am GMT 8, February 21, 2023) , the translation is as follows:
Home Web Front-end Vue.js Note that Vue's reactive syntactic sugar has been deprecated!

Note that Vue's reactive syntactic sugar has been deprecated!

Mar 06, 2023 pm 03:51 PM
front end vue.js vite

This article brings you the latest situation about Vue. It mainly talks about the responsive syntax sugar in Vue. Friends who are interested can take a look at it together. I hope it will be helpful to everyone.

Note that Vue's reactive syntactic sugar has been deprecated!

Introduction

Since the introduction of the concept of composite APIs, a major unsolved problem has been ref# Which one should I use between ## and reactive? reactive has the problem of losing responsiveness due to deconstruction, while ref needs to be used everywhere .value feels cumbersome and can easily be missed without the help of the type system. Drop .value.

For example, the following counter:

<template>
  <button @click="increment">{{ count }}</button>
</template>
Copy after login
Use

ref to define the count variable and increment method:

let count = ref(0)

function increment() {
  count.value++
}
Copy after login
Using responsive syntax sugar, we can write code like this:

let count = $ref(0)

function increment() {
  count++
}
Copy after login
    Vue’s responsive syntax sugar is a compile-time conversion step,
  1. $ref() The method is a compile-time macro command. It is not a real method that will be called at runtime, but is used as a marker for the Vue compiler to indicate the final count The variable needs to be a reactive variable.
  2. Reactive variables can be accessed and reassigned like ordinary variables, but these operations will become
  3. ref with .value after compilation. So the code in the above example will also be compiled into the syntax defined using ref.
  4. Each reactive API that returns
  5. ref has a corresponding macro function prefixed with $. Including the following APIs:
    ref -> $ref
  • computed -> $computed
  • shallowRef -> $shallowRef
  • customRef -> $customRef
  • toRef -> $toRef
##You can use the
    $()
  1. macro to convert an existing ref Convert to reactive variables.
    const a = ref(0)
    let count = $(a)
    count++
    console.log(a.value) // 1
    Copy after login
You can use the
    $$()
  1. macro to keep any reference to a reactive variable as a reference to the corresponding ref.
    let count = $ref(0)
    console.log(isRef($$(count))) // true
    Copy after login
$$()

also works with destructured props since they are also reactive variables. The compiler will efficiently do the conversion through toRef: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">const { count } = defineProps&lt;{ count: number }&gt;() passAsRef($$(count))</pre><div class="contentsignin">Copy after login</div></div>Configuration

Responsive syntactic sugar is a unique feature of

Combined API

, and Must be used via the build step.

Required, requires
    @vitejs/plugin-vue@>=2.0.0
  1. , will be applied to SFC and js(x)/ts(x) files.
    // vite.config.js
    export default {
      plugins: [
        vue({
          reactivityTransform: true
        })
      ]
    }
    Copy after login
Note
    reactivityTransform
  • is now a top-level option of a plug-in, and is no longer located in script.refSugar, because it is not only Only effective for SFC.
  • If it is
vue-cli

build, vue-loader@>=17.0.0 is required. It is currently only effective for SFC. <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">// vue.config.js module.exports = {   chainWebpack: (config) =&gt; {     config.module       .rule('vue')       .use('vue-loader')       .tap((options) =&gt; {         return {           ...options,           reactivityTransform: true         }       })   } }</pre><div class="contentsignin">Copy after login</div></div> If

webpack

vue-loader is built, vue-loader@>=17.0.0 is required, currently only for SFC. effect. <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">// webpack.config.js module.exports = {   module: {     rules: [       {         test: /\.vue$/,         loader: 'vue-loader',         options: {           reactivityTransform: true         }       }     ]   } }</pre><div class="contentsignin">Copy after login</div></div>

Optional, add the following code to the
    tsconfig.json
  1. file, otherwise an error will be reported TS2304: Cannot find name '$ref'., although it does not It affects the use, but it will affect the development experience:
    "compilerOptions":{ "types": ["vue/ref-macros"] }
    Copy after login
Optional, add the following code to the
    eslintrc.cjs
  1. file, otherwise it will prompt ESLint: '$ ref' is not defined.(no-undef)
    module.exports = { ...globals: {
        $ref: "readonly",
        $computed: "readonly",
        $shallowRef: "readonly",
        $customRef: "readonly",
        $toRef: "readonly",
      }
    };
    Copy after login
When responsive syntactic sugar is enabled, these macro functions are globally available and do not need to be imported manually. You can also explicitly introduce
    vue/macros
  1. in the vue file, so that there is no need to configure tsconfig.json and eslintrc in the second and third steps.
    import { $ref } from 'vue/macros'
    
    let count = $ref(0)
    Copy after login
  2. Deprecated experimental feature

    Responsive syntax sugar was once an experimental feature and has been deprecated, please read
  • Deprecated reason

    .

  • It will be removed from Vue core in a future minor version update. If you want to continue using it, please use the
  • Vue Macros

    plug-in.

  • Reason for abandonment

You Yuxi personally gave the reason for abandonment 2 weeks ago (10:05 am GMT 8, February 21, 2023) , the translation is as follows:

As many of you already know, we have officially abandoned this RFC by consensus of the team.

reason

The original goal of Reactivity Transform was to improve the developer experience by providing a cleaner syntax when dealing with reactive state. We're releasing it as an experimental product to gather feedback from real-world usage. Despite these proposed benefits, we discovered the following issues:

  • Losing .value makes it harder to tell what is being tracked and which line triggered the reactive effect . This problem is less obvious in small SFCs, but in large code bases the mental overhead becomes more obvious, especially if the syntax is also used outside of SFC .

  • Due to (1), some users choose to only use Reactivity Transform inside SFC, which creates inconsistencies and context switching costs between different mental models. So the dilemma is that using it only inside SFC will lead to inconsistencies, but using it outside SFC will hurt maintainability.

  • Since there will still be external functions that expect to use raw references, conversion between reactive variables and raw references is inevitable. This ended up adding more learning content and additional mental load, which we noticed was more confusing for beginners than the normal Composition API.

The most important thing is the potential risk of fragmentation. Although this is an explicit opt-in, some users have expressed strong opposition to the proposal due to concerns that they will have to work with different code bases where some have opted in to use it and others No. This is a legitimate concern because Reactivity Transform requires a different mental model and distorts JavaScript semantics (variable assignments can trigger reactive effects).

All things considered, we feel that using this as a stable feature will cause more problems than benefits and is therefore not a good trade-off.

Migration Plan

Message

  • Although Reactivity Transform will be removed from the official package, I think it is a good attempt.
  • Well written. I like detailed RFCs and objective evaluation based on user feedback. The final conclusion makes sense. Don’t let perfect be the enemy of good.
  • Although I enjoy the convenience of this feature, I did find this potential fragmentation issue in actual use. There may be some reluctance to remove this feature in a future release, but engineers should take it seriously. ?
  • Do you remove all functions or just the ref.value part that does the conversion? What about reactive props Deconstruction, is it here to stay?
  • I have been using it for a medium sized e-commerce website without any issues. I understand the rationale behind removing it, but in practice I've found it to be a really big improvement. So my question is: what now?
  • Is it recommended that people who hate .value now avoid ref() if possible and use reactive() like before?
  • .value is the necessary complexity. Just like any other reactive library xxx.set().
  • It should be easy to create a package that transforms all the Reactivity Transform code, right? I also like to do things the recommended way.
  • ...

Recommended study: "vue.js video tutorial"

The above is the detailed content of Note that Vue's reactive syntactic sugar has been deprecated!. 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)

Vue3+TS+Vite development skills: how to optimize SEO Vue3+TS+Vite development skills: how to optimize SEO Sep 10, 2023 pm 07:33 PM

Vue3+TS+Vite development skills: How to perform SEO optimization SEO (SearchEngineOptimization) refers to optimizing the structure, content and keywords of the website to rank it higher in search engines, thereby increasing the website's traffic and exposure. . In the development of modern front-end technologies such as Vue3+TS+Vite, how to optimize SEO is a very important issue. This article will introduce some Vue3+TS+Vite development techniques and methods to help

Vue3+TS+Vite development skills: how to optimize cross-domain requests and network requests Vue3+TS+Vite development skills: how to optimize cross-domain requests and network requests Sep 09, 2023 pm 04:40 PM

Vue3+TS+Vite development skills: How to optimize cross-domain requests and network requests Introduction: In front-end development, network requests are a very common operation. How to optimize network requests to improve page loading speed and user experience is one of the issues that our developers need to think about. At the same time, for some scenarios that require sending requests to different domain names, we need to solve cross-domain issues. This article will introduce how to make cross-domain requests and optimization techniques for network requests in the Vue3+TS+Vite development environment. 1. Cross-domain request solution

Vue3+TS+Vite development skills: how to carry out front-end security protection Vue3+TS+Vite development skills: how to carry out front-end security protection Sep 09, 2023 pm 04:19 PM

Vue3+TS+Vite development skills: How to carry out front-end security protection. With the continuous development of front-end technology, more and more companies and individuals are beginning to use Vue3+TS+Vite for front-end development. However, the security risks that come with it have also attracted people's attention. In this article, we will discuss some common front-end security issues and share some tips on how to protect front-end security during the development process of Vue3+TS+Vite. Input validation User input is often one of the main sources of front-end security vulnerabilities. exist

Vue3+TS+Vite development skills: how to encrypt and store data Vue3+TS+Vite development skills: how to encrypt and store data Sep 10, 2023 pm 04:51 PM

Vue3+TS+Vite development tips: How to encrypt and store data. With the rapid development of Internet technology, data security and privacy protection are becoming more and more important. In the Vue3+TS+Vite development environment, how to encrypt and store data is a problem that every developer needs to face. This article will introduce some common data encryption and storage techniques to help developers improve application security and user experience. 1. Data Encryption Front-end Data Encryption Front-end encryption is an important part of protecting data security. Commonly used

PHP and Vue: a perfect pairing of front-end development tools PHP and Vue: a perfect pairing of front-end development tools Mar 16, 2024 pm 12:09 PM

PHP and Vue: a perfect pairing of front-end development tools. In today's era of rapid development of the Internet, front-end development has become increasingly important. As users have higher and higher requirements for the experience of websites and applications, front-end developers need to use more efficient and flexible tools to create responsive and interactive interfaces. As two important technologies in the field of front-end development, PHP and Vue.js can be regarded as perfect tools when paired together. This article will explore the combination of PHP and Vue, as well as detailed code examples to help readers better understand and apply these two

Questions frequently asked by front-end interviewers Questions frequently asked by front-end interviewers Mar 19, 2024 pm 02:24 PM

In front-end development interviews, common questions cover a wide range of topics, including HTML/CSS basics, JavaScript basics, frameworks and libraries, project experience, algorithms and data structures, performance optimization, cross-domain requests, front-end engineering, design patterns, and new technologies and trends. . Interviewer questions are designed to assess the candidate's technical skills, project experience, and understanding of industry trends. Therefore, candidates should be fully prepared in these areas to demonstrate their abilities and expertise.

C# development experience sharing: front-end and back-end collaborative development skills C# development experience sharing: front-end and back-end collaborative development skills Nov 23, 2023 am 10:13 AM

As a C# developer, our development work usually includes front-end and back-end development. As technology develops and the complexity of projects increases, the collaborative development of front-end and back-end has become more and more important and complex. This article will share some front-end and back-end collaborative development techniques to help C# developers complete development work more efficiently. After determining the interface specifications, collaborative development of the front-end and back-end is inseparable from the interaction of API interfaces. To ensure the smooth progress of front-end and back-end collaborative development, the most important thing is to define good interface specifications. Interface specification involves the name of the interface

Vue3+TS+Vite development skills: how to develop widgets and plug-ins Vue3+TS+Vite development skills: how to develop widgets and plug-ins Sep 10, 2023 pm 07:30 PM

Vue3 is the latest version of Vue.js, which introduces many new features and improvements, allowing developers to build flexible web applications more efficiently. In Vue3, TypeScript (TS) can be seamlessly integrated with Vue, providing us with powerful type checking capabilities. Vite is a lightweight, ES module-based development tool with fast cold start time and fast hot module updates. This article will introduce you how to use Vue3, TS and Vite to make widgets and

See all articles