码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • vue3.0项目实战系列文章 - 使用周期函数


    系列文章目录

    第一章 

    论vue3.0和vue2.0区别之编程方式及例子详解

    第二章

    同一台电脑 实现 vue-cli2和vue-cli3同时并存 及 常见命令

    第三章

    vue3.0项目实战 - ElementUI框架版

    第四章

    【TypeScript】在vue3中遇到的问题及解决方案,未完待续

    第五章

    vue3.0项目实战系列文章 - 登录页面

    第六章

    vue3.0项目实战系列文章 - 使用周期函数


    目录

    系列文章目录

    前言

    一、新的调试钩子函数

    二、使用周期函数


    前言

    从Vue2转换到Vue3

    这个方便的Vue2到Vue3的生命周期映射直接来自于Vue3 Composition API 文档,我认为这是一种最有用的方式来查看事情将如何变化,以及我们如何使用它们。

    • beforeCreate -> use setup()
    • created -> use setup()
    • beforeMount -> onBeforeMount
    • mounted -> onMounted
    • beforeUpdate -> onBeforeUpdate
    • updated -> onUpdated
    • beforeDestroy -> onBeforeUnmount
    • destroyed -> onUnmounted
    • errorCaptured -> onErrorCaptured

    一、新的调试钩子函数

    • onRenderTracked
    • onRenderTriggered

    这两个事件都带有一个DebuggerEvent,它使我们能够知道是什么导致了Vue实例中的重新渲染。

    1. export default {
    2. onRenderTriggered(e) {
    3. debugger
    4. // 检查哪个依赖项导致组件重新呈现
    5. }
    6. }

    二、使用周期函数

    注意

    使用

    1.与Vue3中的大多数功能一样,生命周期钩子是我们必须导入到项目中的东西,这是为了帮助使项目尽可能轻巧。

    2.路由跳转

    template中:

     

        返回首页

    3.toRefs转换为响应式数据:如下代码

    简写导出对象

    完整代码

    1. <template>
    2. <div class="error-page">
    3. <div class="error-handle">
    4. <router-link to="/">
    5. <el-button type="primary" size="large">返回首页el-button>
    6. router-link>
    7. <el-button class="error-btn" type="primary" size="large" @click="goBack">返回上一页el-button>
    8. <p>{{id}}--{{name}}--{{num}}p>
    9. <el-button type="primary" @click="change1">点击el-button>
    10. div>
    11. div>
    12. template>
    13. <script>
    14. import {
    15. useRouter
    16. } from "vue-router";
    17. import {
    18. reactive,
    19. toRefs,
    20. computed,
    21. onBeforeMount,
    22. onMounted,
    23. onBeforeUpdate,
    24. onUpdated,
    25. onBeforeUnmount,
    26. onUnmounted,
    27. onActivated,
    28. onDeactivated,
    29. onErrorCaptured
    30. } from 'vue';
    31. export default {
    32. name: "403",
    33. setup() {
    34. // reactive 数据双向绑定
    35. const data = reactive({
    36. id: 1,
    37. name: '名称',
    38. num: computed(() => data.id += 5) //计算属性
    39. });
    40. const router = useRouter();
    41. const goBack = () => {
    42. router.go(-1);
    43. };
    44. // 方法
    45. const methods = {
    46. change1() {
    47. alert("点击")
    48. },
    49. };
    50. // 在组件内容被渲染到页面之前自动执行的函数
    51. onBeforeMount(() => {
    52. //alert("onBeforeMount")
    53. });
    54. // 在组件内容被渲染到页面之后自动执行的函数
    55. onMounted(() => { // 绑定后自动执行
    56. //alert("onMounted")
    57. });
    58. // 当data中的数据发生变化前执行
    59. onBeforeUpdate(() => {
    60. //alert("onBeforeUpdate");
    61. });
    62. // 当data中的数据发生变化后执行
    63. onUpdated(() => {
    64. //alert("onUpdated");
    65. });
    66. // 解除绑定前执行的函数
    67. onBeforeUnmount(() => {
    68. //alert("onBeforeUnmount");
    69. });
    70. // 解除绑定后执行的函数
    71. onUnmounted(() => {
    72. //alert("onUnmounted");
    73. });
    74. onActivated(() => {
    75. // ...
    76. })
    77. onDeactivated(() => {
    78. // ...
    79. })
    80. onErrorCaptured(() => {
    81. // ...
    82. })
    83. return {
    84. // toRefs转换为响应式数据
    85. ...toRefs(data),
    86. ...methods,
    87. goBack,
    88. };
    89. }
    90. };
    91. script>
    92. <style scoped>
    93. style>

  • 相关阅读:
    【JVM笔记】方法区的内部结构与运行时常量池
    项目引入jar从私服Nexus 拉去遇到的一个问题
    Requests库POST请求
    <PLC><西门子><工控>西门子博图V18中使用SCL语言编写一个CRC16-modbus校验程序
    Python|小游戏之猫捉老鼠!!!
    java 自带命令
    如何阅读一份源代码?
    C Primer Plus(6) 中文版 第7章 C控制语句:分支和跳转 7.6 循环辅助:continue和break
    JavaEE 文件操作
    攻防演练中怎样做目标网情搜集
  • 原文地址:https://blog.csdn.net/Hei_lovely_cat/article/details/126053730
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号