网站首页 > 技术文章 正文
<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 |
猜你喜欢
- 2024-10-02 Vue3,Vuex,集中式状态(数据)管理,四个模块:state、getter
- 2024-10-02 Vue核心知识:8.4 如何在组件中去使用vuex的值和方法?
- 2024-10-02 Netflix 开源危机管理工具 Dispatch,真香
- 2024-10-02 Vuex基本使用(vuex的基本使用)
- 2024-10-02 vue组件间传递参数的几种方式(vue组件之间的参数传递)
- 2024-10-02 vue前端实现与安卓和ios之间的通信
- 2024-10-02 2024前端面试题(三)(2021前端最新面试题)
- 2024-10-02 Vue.js应用的四种AJAX请求数据模式以及优缺点
- 2024-10-02 总结4个方面优化Vue项目(如何优化vue项目)
- 2024-10-02 Node + Express + Mysql + Vue 全栈开发项目:Todo List
- 05-11CSS:前端必会的flex布局,我把布局代码全部展示出来了
- 05-11Moti:React Native 动画库的新标杆
- 05-11前端开发避坑指南:每天都能用的 CSS3/Less/Sass 实战技巧
- 05-11HarmonyOS:ArkTS 多态样式自学指南
- 05-115 分钟快速上手图形验证码,防止接口被恶意刷量!
- 05-11网页五指棋游戏
- 05-11告别长文焦虑!AI帮你“秒划重点”,文章秒变知识卡片
- 05-11鸿蒙NEXT小游戏开发:数字华容道
- 最近发表
- 标签列表
-
- cmd/c (64)
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- sqlset (64)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- chromepost (65)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- org.redisson (64)
- js数组插入 (83)
- gormwherein (64)
- linux删除一个文件夹 (65)
- mac安装java (72)
- outofmemoryerror是什么意思 (64)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)