百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术分类 > 正文

vue3 script setup 语法糖

ztj100 2024-11-26 11:15 13 浏览 0 评论

Loong Panda


<script setup> 是在单文件组件 (SFC) 中使用 组合式 API 的编译时语法糖。相比于普通的 <script> 语法,它有更简洁的代码,由于处于同一作用域,因此的运行时性能会更好。


响应式变量

<script setup>
	// vue3和vue2最大的区别就是响应式变量
  import { ref, toRef, toRefs, reactive, onMounted } from 'vue';
  const val1 = ref(0); // 创建任意数据类型的响应式变量
  const val2 = reactive({ num: 1 }); // 创建引用类型的响应式对象
  const { newVal } = toRefs(val2); // 将响应式对象转换为普通对象
  const val3 = toRef(val2, 'num'); // 将响应式对象num2中num字段创建为一个新的响应式变量
  onMounted(() => {
    val1.value = Date.now(); // 修改ref创建的变量
    val2.num = Date.now() // 修改reactive创建的变量
  });
</script>

函数

<template>
	<el-button size="small" @click="onClick">点击</el-button>
</template>
<script setup>
  
  function onClick() {
    // ....
  };
</script>

计算属性(computed)

<script setup>
  import { computed, ref } from 'vue'
  const num = ref(1)
  const calc = computed(() => {
  	return num.value * 2
  })
</script>

观察属性(watch)

<script setup>
  import { watch, reactive } from 'vue'
  const obj = reactive({
  	count: 1
  })
  // 监听count
  watch(
  	() => obj.count,
  	(newVal, oldVal) => {
      
    },
    {
      immediate: true, // 立即执行
      deep: true // 深度监听
    }
  )
</script>

父子组件传值 props 和 emit


子组件
  
<template>
  <span>{{ dataId }}</span> // 展示父级传递过来的参数
  <el-button size="small" @click="onUpdate">更新</el-button>
</template>
<script setup>

  // 声明 props
  const props = defineProps({
    dataId: {
      type: String,
      default: ''
    }
  })
  // 声明 emit 事件,事先需要声明好事件名,如on-update
  const emit = defineEmits(['on-update'])

  const onUpdate = () => {
    // 执行 on-update 事件
    emit('on-update', Date.now())
  }
</script>

父组件

<template>
	<child :data-id="chidDataId" @on-update="onUp"></child>
</template>
<script setup>
  import child from './child.vue';
  import { ref } from 'vue';
  const chidDataId = ref(100)

  // 接收子组件触发的方法
  function onUp(name) {
  	//
  }
</script>

七、双向绑定 v-model

子组件

<template>

<span @click="changeInfo">我叫{{ modelValue }},今年{{ age }}岁</span>

</template>

<script setup>

// import { defineEmits, defineProps } from 'vue'

// defineEmits和defineProps在<script setup>中自动可用,无需导入

// 需在.eslintrc.js文件中【globals】下配置【defineEmits: true】、【defineProps: true】

defineProps({

modelValue: String,

age: Number

})

const emit = defineEmits(['update:modelValue', 'update:age'])

const changeInfo = () => {

// 触发父组件值更新

emit('update:modelValue', 'Tom')

emit('update:age', 30)

}

</script>

父组件

<template>

// v-model:modelValue简写为v-model

// 可绑定多个v-model

<child v-model="state.name" v-model:age="state.age"></child>

</template>

<script setup>

import child from './child.vue'

import { reactive } from 'vue'

const state = reactive({

name: 'Jerry',

age: 20

})

</script>


路由

<script setup>
  import { useRoute, useRouter } from 'vue-router'

  const route = useRoute();
  const router = useRouter();

	// 路由跳转
  router.push('/home')
  
	// 获取路由实例,及路由信息
  console.log(route.query)
  
</script>


路由守卫

<script setup>
  import { onBeforeRouteLeave, onBeforeRouteEnter } from 'vue-router'

  onBeforeRouteLeave((to, from, next) => {
    next()
  })
  onBeforeRouteEnter((to, from, next) => {
  	next()
  })
</script>


全局状态管理器

<script setup>
  import { useStore } from 'vuex'
  const store = useStore()

  // 获取state
  store.state.xxx
  // 触发mutations的方法
  store.commit('fnName')
  // 触发actions的方法
  store.dispatch('fnName')
  // 获取Getters
  store.getters.xxx
</script>

原型挂载/绑定与使用

----绑定--------------------------------------------------------------------
// main.js
import { createApp } from 'vue'
import App from './App.vue'
const app = createApp(App)
// 获取原型
const prototype = app.config.globalProperties
// 绑定参数
prototype.$ajax = ajax

----使用--------------------------------------------------------------------

<script setup>
  import { getCurrentInstance } from 'vue'
  const { proxy } = getCurrentInstance()

  proxy.$ajax(url, {})
    .then(res => {
  		// .....
  	})
</script>

生命周期

vue3和vu2周期最大的区别是vue3用setup替代了beforeCreate 和 created,通俗的说fa就是说原来在beforeCreate 和 created写的代放在码在setup里就行了。其他的周期使用前缀“on”,

比如: onMounted(() => {})


选项式 API

Hook inside setup

beforeCreate

Not needed*

created

Not needed*

beforeMount

onBeforeMount

mounted

onMounted

beforeUpdate

onBeforeUpdate

updated

onUpdated

beforeUnmount

onBeforeUnmount

unmounted

onUnmounted

errorCaptured

onErrorCaptured

renderTracked

onRenderTracked

renderTriggered

onRenderTriggered

activated

onActivated

deactivated

onDeactivated

相关推荐

Whoosh,纯python编写轻量级搜索工具

引言在许多应用程序中,搜索功能是至关重要的。Whoosh是一个纯Python编写的轻量级搜索引擎库,可以帮助我们快速构建搜索功能。无论是在网站、博客还是本地应用程序中,Whoosh都能提供高效的全文搜...

如何用Python实现二分搜索算法(python二分法查找代码)

如何用Python实现二分搜索算法二分搜索(BinarySearch)是一种高效的查找算法,适用于在有序数组中快速定位目标值。其核心思想是通过不断缩小搜索范围,每次将问题规模减半,时间复杂度为(O...

路径扫描 -- dirsearch(路径查找器怎么使用)

外表干净是尊重别人,内心干净是尊重自己,干净,在今天这个时代,应该是一种极高的赞美和珍贵。。。----网易云热评一、软件介绍Dirsearch是一种命令行工具,可以强制获取web服务器中的目录和文件...

78行Python代码帮你复现微信撤回消息!

来源:悟空智能科技本文约700字,建议阅读5分钟。本文基于python的微信开源库itchat,教你如何收集私聊撤回的信息。...

从零开始学习 Python!2《进阶知识》 Python进阶之路

欢迎来到Python学习的进阶篇章!如果你说已经掌握了基础语法,那么这篇就是你开启高手之路的大门。我们将一起探讨面向对象编程...

白帽黑客如何通过dirsearch脚本工具扫描和收集网站敏感文件

一、背景介绍...

Python之txt数据预定替换word预定义定位标记生成word报告(四)

续接Python之txt数据预定替换word预定义定位标记生成word报告(一)https://mp.toutiao.com/profile_v4/graphic/preview?pgc_id=748...

假期苦短,我用Python!这有个自动回复拜年信息的小程序

...

Python——字符串和正则表达式中的反斜杠(&#39;\&#39;)问题详解

在本篇文章里小编给大家整理的是关于Python字符串和正则表达式中的反斜杠('\')问题以及相关知识点,有需要的朋友们可以学习下。在Python普通字符串中在Python中,我们用'\'来转义某些普通...

Python re模块:正则表达式综合指南

Python...

Python中re模块详解(rem python)

在《...

python之re模块(python re模块sub)

re模块一.re模块的介绍1.什么是正则表达式"定义:正则表达式是一种对字符和特殊字符操作的一种逻辑公式,从特定的字符中,用正则表达字符来过滤的逻辑。(也是一种文本模式;)2、正则表达式可以帮助我们...

MySQL、PostgreSQL、SQL Server 数据库导入导出实操全解

在数字化时代,数据是关键资产,数据库的导入导出操作则是连接数据与应用场景的桥梁。以下是常见数据库导入导出的实用方法及代码,包含更多细节和特殊情况处理,助你应对各种实际场景。一、MySQL数据库...

Zabbix监控系统系列之六:监控 mysql

zabbix监控mysql1、监控规划在创建监控项之前要尽量考虑清楚要监控什么,怎么监控,监控数据如何存储,监控数据如何展现,如何处理报警等。要进行监控的系统规划需要对Zabbix很了解,这里只是...

mysql系列之一文详解Navicat工具的使用(二)

本章内容是系列内容的第二部分,主要介绍Navicat工具的使用。若查看第一部分请见:...

取消回复欢迎 发表评论: