当前位置: 首页 > news >正文

Vue3核心语法速成

一、细微变化
由vue2的选项式变成了组合式,但是vue3可以写vue2的写法
并且vue3不必要只有一个根组件了

可以这样定义组合式

export default {name: 'App',components: {HelloWorld}setup(){}
}

也可以这样定义(一般推荐这种),这个setup属性是专为组合式API提供的语法糖

<script setup>
import HelloWorld from './components/HelloWorld.vue'export default {name: 'App',components: {HelloWorld}}

二、相关语法

1、reactive和shallowReactive(主要处理对象类型)

shallowReactive和Reactive都是vue.js中的响应式AP!。Reactive用于将对象转换为响应式对象,使其具有自动追踪依赖项和触发更新的能力。而shallowReactive只会让对象的第一层属性具有响应式能力,而不会递归地将嵌套属性转换为响应式对象。这意味着如果对象具有嵌套属性,shallowReactive只会在第一层级别上自动追踪更改,而不会递归地监视内部更改。

reactive能改变age和friends里的二级数据

<template><div class="hello"><h1>{{ myData }}</h1><button @click="clickHandler">修改</button></div>
</template><script setup>
import { reactive } from 'vue';const myData=new reactive({name:'张三',age:18,friends:['韩梅梅','李雷']})function clickHandler() { myData.friends.push('小明')myData.age++}</script><!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {margin: 40px 0 0;
}
ul {list-style-type: none;padding: 0;
}
li {display: inline-block;margin: 0 10px;
}
a {color: #42b983;
}
</style>

shallowReactive只能改变age不能改变friends中的数据

此时不能改变friends中的数据

<template><div class="hello"><h1>{{ myData }}</h1><button @click="clickHandler">修改</button></div>
</template><script setup>
import {shallowReactive } from 'vue';const myData= shallowReactive({name:'张三',age:18,friends:['韩梅梅','李雷']})function clickHandler() { myData.friends.push('小明')
}</script><!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {margin: 40px 0 0r;
}
ul {list-style-type: none;padding: 0;
}
li {display: inline-block;margin: 0 10px;
}
a {color: #42b983;
}
</style>

此时可以改变age的数值

<template><div class="hello"><h1>{{ myData }}</h1><button @click="clickHandler">修改</button></div>
</template><script setup>
import {shallowReactive } from 'vue';const myData= shallowReactive({name:'张三',age:18,friends:['韩梅梅','李雷']})function clickHandler() { myData.age++
}</script><!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {margin: 40px 0 0r;
}
ul {list-style-type: none;padding: 0;
}
li {display: inline-block;margin: 0 10px;
}
a {color: #42b983;
}
</style>

2、ref(主要处理基本数据类型,也能处理对象类型)

<template><div class="hello"><h1>{{ count }}</h1><button @click="clickHandler">修改</button></div>
</template><script setup>
import {ref } from 'vue';const count=ref(0)function clickHandler(){count.value++
}</script><!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {margin: 40px 0 0r;
}
ul {list-style-type: none;padding: 0;
}
li {display: inline-block;margin: 0 10px;
}
a {color: #42b983;
}
</style>

3、readonly和shallowReadonly(非响应式数据)

readonly根属性和内部属性都是只读的

<template><div class="hello"><h1>{{ myData }}</h1><button @click="clickHandler">修改</button></div>
</template><script setup>
import {readonly } from 'vue';const myData=readonly({name:'张三',age:18,friends:['韩梅梅','李雷']})function clickHandler() { myData.age++}</script><!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {margin: 40px 0 0r;
}
ul {list-style-type: none;padding: 0;
}
li {display: inline-block;margin: 0 10px;
}
a {color: #42b983;
}
</style>

shallowReadonly根属性是只读的内部属性是不读的

访问friends里的数据是没有反应的

<template><div class="hello"><h1>{{ myData }}</h1><button @click="clickHandler">修改</button></div>
</template><script setup>
import { shallowReadonly } from 'vue';const myData=shallowReadonly({name:'张三',age:18,friends:['韩梅梅','李雷']})function clickHandler() { myData.friends.push('王五')}</script><!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {margin: 40px 0 0r;
}
ul {list-style-type: none;padding: 0;
}
li {display: inline-block;margin: 0 10px;
}
a {color: #42b983;
}
</style>

4、computed

以下例子中渲染三次,但是有缓存,就是只执行了一次

<template><div class="hello"><h1>{{ getLen }}</h1><h1>{{ getLen }}</h1><h1>{{ getLen }}</h1><button @click="clickHandler">修改</button></div>
</template><script setup>
import { computed, ref } from 'vue';const content=ref('这是一段测试内容')
const getLen=computed(()=>{console.log('计算属性执行了')return content.value.length
})
function clickHandler() { content.value+='!'}</script><!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {margin: 40px 0 0r;
}
ul {list-style-type: none;padding: 0;
}
li {display: inline-block;margin: 0 10px;
}
a {color: #42b983;
}
</style>

5、watch(监听)

watch是一个选项 API,通常在组件的选项中使用。需要显式地指定要监听的数据和回调函数。

可以用来监听ref对象、reactive对象、函数、数组

<template><div class="hello"><h1>{{ myData }}</h1><button @click="clickHandler">修改</button></div>
</template><script setup>
import { reactive, watch} from 'vue';const myData=reactive({name:'张三',age:18,friends:['韩梅梅','李雷']})watch(()=>myData.age,(newVal,oldVal)=>{console.log(newVal,oldVal)})function clickHandler() { myData.age++}</script><!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {margin: 40px 0 0r;
}
ul {list-style-type: none;padding: 0;
}
li {display: inline-block;margin: 0 10px;
}
a {color: #42b983;
}
</style>

6、watchEffect

watchEffect是一个函数 API,在组件的 setup 函数或生命周期函数中使用。不需要显式地指定要监听的数据,它会自动追踪函数内部使用的响应式数据。

<template><div class="hello"><h1>{{ myData }}</h1><button @click="clickHandler">修改</button></div>
</template><script setup>
import { reactive,ref, watchEffect} from 'vue';const count=ref(0)
const myData=reactive({name:'张三',age:18,friends:['韩梅梅','李雷']})watchEffect(()=>{console.log('count的值为:'+count.value+',age的值为:'+myData.age)})function clickHandler() { myData.age++}</script><!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {margin: 40px 0 0r;
}
ul {list-style-type: none;padding: 0;
}
li {display: inline-block;margin: 0 10px;
}
a {color: #42b983;
}
</style>

http://www.dtcms.com/a/172759.html

相关文章:

  • 慢sql处理流程和常见案例
  • 20250505下载VLC for Android
  • git上常用的12个月份对应的英语单词以及月份英语缩写形式
  • 矩阵快速幂 快速求解递推公式
  • 二重指针和二维数组
  • 力扣119题解
  • 机场围界报警系统的研究与应用
  • 深入理解 CSS Flex 布局:代码实例解析
  • WMS仓库管理系统:Java+Vue,含源码及文档,集成仓储全流程管控,实现库存精准、作业高效、数据透明
  • 苹果公司正在与亚马逊支持的初创公司Anthropic展开合作
  • 【数据结构】第八章:排序
  • 网络编程套接字(一)
  • C语言数据在内存中的存储详解
  • 标题:试验台铁地板:革新之路
  • Untiy基础学习(六)MonoBehaviour基类的简单介绍
  • QT聊天项目DAY08
  • 下载core5compat 模块时,被禁止,显示 - servese replied: Forbbidden. -->换镜像源
  • 文旅行业淡旺季明显,如何做好人力资源规划?​
  • cgi技术初识
  • Python实现自动驾驶中的车道检测算法:从理论到实践
  • Debezium MySqlValueConverters详解
  • 【Java ee初阶】多线程(7)
  • 学习路线(python)
  • 大模型基础(四):transformers库(上):pipline、模型、分词器
  • [Linux] 笔记本访问b站,Linux内核打印的调用栈
  • 五一假期作业
  • android-ndk开发(4): linux开发机有线连接android设备
  • Go小技巧易错点100例(二十九)
  • 关于毕业论文,查重,AIGC
  • 【漫话机器学习系列】240.真正类率(True Positive Rate,TPR)