Vue3.2 使用笔记总结【自学复习使用】

本文详细介绍了Vue3.2的新特性,包括setup语法简化,无需return即可在template中使用,defineExpose用于显式暴露子组件数据,对await的直接支持,以及useRoute、useRouter、store、生命周期钩子等的使用,并提供了丰富的示例和实践指导。
摘要由CSDN通过智能技术生成

1、起初 Vue3.0 暴露变量必须 return 出来,template中才能使用;
2、Vue3.2 中 只需要在 script 标签上加上 setup 属性,组件在编译的过程中代码运行的上下文是在 setup() 函数中,无需return,template可直接使用。

一、文件结构

<template>
 // Vue2中,template标签中只能有一个根元素,在Vue3中没有此限制
</template>
<script setup>
</script>
<style lang="scss" scoped>
  // 支持CSS变量注入v-bind(color)
</style>

二、data

<script setup>
import {
    reactive, ref, toRefs} from 'vue';

// ref声明响应式数据,用于声明基本数据类型
const name = ref('Tom')
// 修改
name.value = 'Herry';

// reactive声明响应式数据,用于声明引用数据类型
const state = reactive({
   
	name: 'Herry',
	sex: '男'
})
// 修改
state.name = 'Tom'

 // 使用toRefs解构
  const {
   name, sex} = toRefs(state)
  // template可直接使用{
   {name}}、{
   {sex}}
</script>

三、method

<template>
  // 调用方法
  <button @click='changeName'>按钮</button>  
</template>

<script setup>
  import {
    reactive } from 'vue'

  const state = reactive({
   
    name: 'Jery'
  })

  // 声明method方法
  const changeName = () => {
   
    state.name = 'Tom'
  }  
</script>

四、computed

<script setup>
import {
   computed, ref} from 'vue';

const count = ref(1);

// 通过computed获得doubleCount
const doubleCount = computed(()=> {
   
	return count.value * 2;
})
</script>

五、watch

<script setup>
import {
   watch, reactive} from 'vue';

const state = reactive({
   
	count: 1
});

const changCount = () => {
   
	state.count = state.count * 2;
}

watch(
	() => state.count,
	(newVal, oldVal) => {
   
		console.log(state.count)
      	console.log(`watch监听变化前的数据:${
     oldVal}`)
      	console.log(`watch监听变化后的数据:${
     newVal}`)
	},
	{
   
      immediate: true, // 立即执行
      deep: true // 深度监听
    }
)
</script>
  • 25
    点赞
  • 125
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Vue3.2是一款非常流行的JavaScript框架,它让在前端领域开发变得更加的便捷。Vue3.2版本支持Websocket,让与服务器的交互变得更加的高效、实时,并且有效地减少互联网应用程序的延迟时间。 Websocket是一种不同于HTTP协议的网络通信协议,它在浏览器和服务器之间建立了一条双向通信的通道,而不是像HTTP协议那样只支持单向请求和响应交互。通过使用Websocket,前端客户端和后端服务端之间可以实现实时通信,并且可以将数据通过单个连接实时传输给所有连接的客户端,减少了网络带宽的利用。 在Vue3.2使用Websocket可以使用vue-native-websocket插件,这个插件便于在Websocket应用中发送和接收消息。Vue-native-websocket提供一个WebSocket插件,基于WebSocket属性和方法封装了Vue插件API,让我们更方便地在Vue组件中使用Websocket。具体操作可以参考下面的方法: 1.在项目中安装vue-native-websocket库 ```javascript npm install --save vue-native-websocket ``` 2.在Vue项目入口文件main.js中引入插件并配置 ```javascript import VueNativeSock from 'vue-native-websocket'; Vue.use(VueNativeSock, 'ws://your-websocket-url', { format: 'json', store: store, reconnection: true, reconnectionAttempts: 5, reconnectionDelay: 3000 }) ``` 3.使用插件 ```html //使用v-on指令,和函数名连接起来,当websocket连接成功时执行myCustomEventHandler <div v-on:websocket="myCustomEventHandler"> ``` vue-native-websocket支持很多方法,包括websocket.onopen、websocket.onmessage、websocket.onclose等。可以通过设置监听回调函数来进行响应。 总的来说,Vue3.2vue-native-websocket让Websocket的使用变得更加简单,并且可以提高应用系统的性能和实时性。在实际实施中,我们仍然需要遵循开发规范,设计和测试异步处理,确保应用程序的正常运行。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值