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

vue3 script setup 语法糖

ztj100 2024-11-26 11:15 42 浏览 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

相关推荐

SpringBoot整合SpringSecurity+JWT

作者|Sans_https://juejin.im/post/5da82f066fb9a04e2a73daec一.说明SpringSecurity是一个用于Java企业级应用程序的安全框架,主要包含...

「计算机毕设」一个精美的JAVA博客系统源码分享

前言大家好,我是程序员it分享师,今天给大家带来一个精美的博客系统源码!可以自己买一个便宜的云服务器,当自己的博客网站,记录一下自己学习的心得。开发技术博客系统源码基于SpringBoot,shiro...

springboot教务管理系统+微信小程序云开发附带源码

今天给大家分享的程序是基于springboot的管理,前端是小程序,系统非常的nice,不管是学习还是毕设都非常的靠谱。本系统主要分为pc端后台管理和微信小程序端,pc端有三个角色:管理员、学生、教师...

SpringBoot+LayUI后台管理系统开发脚手架

源码获取方式:关注,转发之后私信回复【源码】即可免费获取到!项目简介本项目本着避免重复造轮子的原则,建立一套快速开发JavaWEB项目(springboot-mini),能满足大部分后台管理系统基础开...

Spring Boot的Security安全控制——认识SpringSecurity!

SpringBoot的Security安全控制在Web项目开发中,安全控制是非常重要的,不同的人配置不同的权限,这样的系统才安全。最常见的权限框架有Shiro和SpringSecurity。Shi...

前同事2024年接私活已入百万,都是用这几个开源的SpringBoot项目

前言不得不佩服SpringBoot的生态如此强大,今天给大家推荐几款优秀的后台管理系统,小伙伴们再也不用从头到尾撸一个项目了。SmartAdmin...

值得学习的15 个优秀开源的 Spring Boot 学习项目

SpringBoot算是目前Java领域最火的技术栈了,除了书呢?当然就是开源项目了,今天整理15个开源领域非常不错的SpringBoot项目供大家学习,参考。高富帅的路上只能帮你到这里了,...

开发企业官网就用这个基于SpringBoot的CMS系统,真香

前言推荐这个项目是因为使用手册部署手册非常...

2021年超详细的java学习路线总结—纯干货分享

本文整理了java开发的学习路线和相关的学习资源,非常适合零基础入门java的同学,希望大家在学习的时候,能够节省时间。纯干货,良心推荐!第一阶段:Java基础...

jeecg-boot学习总结及使用心得(jeecgboot简单吗)

jeecg-boot学习总结及使用心得1.jeecg-boot是一个真正前后端分离的模版项目,便于二次开发,使用的都是较流行的新技术,后端技术主要有spring-boot2.x、shiro、Myb...

后勤集团原料管理系统springboot+Layui+MybatisPlus+Shiro源代码

本项目为前几天收费帮学妹做的一个项目,JavaEEJSP项目,在工作环境中基本使用不到,但是很多学校把这个当作编程入门的项目来做,故分享出本项目供初学者参考。一、项目描述后勤集团原料管理系统spr...

白卷开源SpringBoot+Vue的前后端分离入门项目

简介白卷是一个简单的前后端分离项目,主要采用Vue.js+SpringBoot技术栈开发。除了用作入门练习,作者还希望该项目可以作为一些常见Web项目的脚手架,帮助大家简化搭建网站的流程。...

Spring Security 自动踢掉前一个登录用户,一个配置搞定

登录成功后,自动踢掉前一个登录用户,松哥第一次见到这个功能,就是在扣扣里边见到的,当时觉得挺好玩的。自己做开发后,也遇到过一模一样的需求,正好最近的SpringSecurity系列正在连载,就结...

收藏起来!这款开源在线考试系统,我爱了

大家好,我是为广大程序员兄弟操碎了心的小编,每天推荐一个小工具/源码,装满你的收藏夹,每天分享一个小技巧,让你轻松节省开发效率,实现不加班不熬夜不掉头发,是我的目标!今天小编推荐一款基于Spr...

Shiro框架:认证和授权原理(shiro权限认证流程)

优质文章,及时送达前言Shiro作为解决权限问题的常用框架,常用于解决认证、授权、加密、会话管理等场景。本文将对Shiro的认证和授权原理进行介绍:Shiro可以做什么?、Shiro是由什么组成的?举...

取消回复欢迎 发表评论: