Note that Vue's reactive syntactic sugar has been deprecated!
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.
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.
<template> <button @click="increment">{{ count }}</button> </template>
ref to define the
count variable and
increment method:
let count = ref(0) function increment() { count.value++ }
let count = $ref(0) function increment() { count++ }
- Vue’s responsive syntax sugar is a compile-time conversion step,
- $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 countThe variable needs to be a
reactive variable. Reactive variables can be accessed and reassigned like ordinary variables, but these operations will become - ref
with
.valueafter compilation. So the code in the above example will also be compiled into the syntax defined using
ref.
Each reactive API that returns - ref
has a corresponding macro function prefixed with
$. Including the following APIs:
- ref -> $ref
- computed -> $computed
- shallowRef -> $shallowRef
- customRef -> $customRef
- toRef -> $toRef
- $()
- 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
- $$()
- 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<{ count: number }>()
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
- , will be applied to SFC and js(x)/ts(x) files.
// vite.config.js export default { plugins: [ vue({ reactivityTransform: true }) ] }
Copy after login
- 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
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) => {
config.module
.rule('vue')
.use('vue-loader')
.tap((options) => {
return {
...options,
reactivityTransform: true
}
})
}
}</pre><div class="contentsignin">Copy after login</div></div>
If
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>
- tsconfig.json
- 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
- eslintrc.cjs
- 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
- vue/macros
- in the vue file, so that there is no need to configure
tsconfig.json
andeslintrc
in the second and third steps.import { $ref } from 'vue/macros' let count = $ref(0)
Copy after loginDeprecated 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 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.
reasonThe 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 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 ... Recommended study: "vue.js video tutorial"
.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 .
ref.value
part that does the conversion? What about reactive props
Deconstruction, is it here to stay? .value
now avoid ref()
if possible and use reactive()
like before? .value
is the necessary complexity. Just like any other reactive library xxx.set()
.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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 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. 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 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. 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

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.

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 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
