Vue 3.x基础
模版
| <template> |
| // html |
| </template> |
| |
| <script setup> |
| |
| |
| </script> |
| |
| <style> |
| // css |
| </style> |
setup API
变量(响应式)
| |
| let refValue = ref(1) |
| console.log(refValue.value) |
| |
| |
| let reactiveValue = reactive({ a: 1, b: 2 }) |
| console.log(reactiveValue) |
| |
| |
| const { a, b } = toRefs(reactiveValue) |
| console.log(a, b) |
函数
| |
| const changeValue = (v) => { |
| refValue.value = v |
| console.log(v) |
| } |
| |
| changeValue(1) |
生命周期
选项式 API |
Setup API |
调用时机 |
beforeCreate |
Not needed* |
# |
created |
Not needed* |
# |
beforeMount |
onBeforeMount(常用) |
在挂载开始之前被调用:相关的 render 函数首次被调用。 |
mounted |
onMounte(常用) |
组件挂载时调用 |
beforeUpdate |
onBeforeUpdate |
数据更新之前调用,此时DOM还未更新 |
updated |
onUpdated |
DOM 重新渲染和打补丁,此时DOM已更新,不要在该钩子函数里更新数据。 |
beforeUnmount |
onBeforeUnmount |
在卸载组件实例之前调用。此时Vue实例仍是正常的。 |
unmounted |
onUnmounted |
卸载组件实例后调用,组件实例的所有指令、事件侦听器都被移、子组件实例都会被卸载。 |
activated |
onActivated |
被 keep-alive 缓存的组件激活时调用。 |
deactivated |
onDeactivated |
被 keep-alive 缓存的组件停用时调用。 |
errorCaptured |
onErrorCaptured |
当捕获一个来自子孙组件的错误时被调用,此钩子可以返回 false 以阻止该错误继续向上传播。 |
renderTracked |
onRenderTracked |
# |
renderTriggered |
onRenderTriggered |
# |
注意:因为 setup 是++围绕 beforeCreate 和 created++ 生命周期钩子运行的,所以不需要显式地定义它们。换句话说,在这些钩子中编写的任何代码都应该直接在 setup 函数中编写。
| <script setup> |
| onMounted(() => { |
| console.log('Component is mounted!') |
| }) |
| |
| </script> |
计算属性、侦听器
| <script setup> |
| |
| const ageIs18 = computed(() => { |
| return age.value === 18 |
| }) |
| |
| |
| watch(count, (nV, oV) => { |
| console.log(nV, oV) |
| }) |
| </script> |
路由
| <!-- router.js --> |
| |
| import { createRouter, createWebHashHistory } from 'vue-router' |
| |
| const routes = [ |
| { |
| path: '/', |
| redirect: '/home' |
| |
| } |
| ... |
| ] |
| |
| |
| const router = createRouter({ |
| history: createWebHashHistory(), |
| routes, |
| }) |
| |
| export default router |
| <!-- main.js --> |
| |
| import { createApp } from 'vue' |
| import router from './router' |
| ... |
| |
| const app = createApp(App) |
| app.use(router) |
| <!-- pages/home.vue --> |
| |
| <script setup> |
| import { useRouter } from 'vue-router' |
| |
| |
| const router = useRouter() |
| router.push({ path: '/next' }) |
| |
| |
| const route = useRoute() |
| const query = route.query |
| </script> |
pinia
Vuex替代品,全局数据管理器
| <!-- pinia/user.js --> |
| |
| import { defineStore } from 'pinia' |
| import { ref, computed, reactive } from 'vue'; |
| |
| |
| |
| export const usePiniaStore = defineStore('user', () => { |
| |
| const _this = window.$this |
| |
| |
| const userId = ref('111122') |
| const userData = ref(null) |
| |
| |
| const getUser = async () => { |
| const res = await _this.$api.getId() |
| userData.value = res.data |
| } |
| |
| |
| const userName = computed(() => userData.value.id + ' ---- ') |
| |
| |
| return { userData, userName, getUser, userId } |
| }) |
| <!-- pages/user.vue --> |
| |
| <script setup> |
| import { usePiniaStore } from '../pinia/user.js' |
| |
| |
| const useStore = usePiniaStore() |
| |
| const { userData, userName, userId } = storeToRefs(useStore) |
| |
| useStore.getUser() |
| |
| userId.value = 'aaa' |
| console.log(`userStore:`, userId.value, userData.value) |
| </script> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端