• vue(12)


    目录

    消息订阅与发布(pubsub)

     TodoList案例消息订阅与发布

    nextTick


    数据以形参的形式传递

    消息订阅与发布(pubsub)

    1.一种组件间通信的方式,用于任意组件间通信

    2.使用步骤

    1)安装pubsub:

    npm i pubsub-js

    2)引入

    import pubsub from 'pubsub-js'

    3)接收数据:A组件想接收数据,则在A组件中订阅消息,订阅的回调在A组件自身

    1. methods(){
    2. demo(data){..........}
    3. }
    4. ............
    5. mounted(){
    6. this.pid=pubsub.subscribe('xxx',this.demo)
    7. //订阅消息
    8. }

    4)提供数据:

    pubsub.publish('xxx',数据)

    5.最好在beforeDestory钩子中,用pubsub.unsubscribe(pid) 去取消订阅

    school.vue

    1. <script>
    2. import pubsub from 'pubsub-js'
    3. export default {
    4. name: "School",
    5. data() {
    6. return {
    7. name: "尚硅谷",
    8. address: "北京",
    9. }
    10. },
    11. methods:{
    12. demo(msgName,data){
    13. console.log('hello消息收到了',data,this);
    14. }
    15. },
    16. mounted(){
    17. // console.log('school',this);
    18. // this.$bus.$on('hello',(data)=>{
    19. // console.log("我是school组件,收到了数据",data)
    20. // })
    21. // this.pubId=pubsub.subscribe('hello',(msgName,data)=>{
    22. // console.log('有人发布了hello消息,hello消息的回调执行了',msgName,data);
    23. // })
    24. this.pubId=pubsub.subscribe('hello',this.demo)
    25. },
    26. beforeDestroy(){
    27. // this.$bus.$off('hello')
    28. pubsub.unsubscribe(this.pubId)
    29. },
    30. }
    31. script>
    32. <style scoped>
    33. .school {
    34. background-color: skyblue;
    35. padding: 5px;
    36. }
    37. style>

    student.vue

    1. <script>
    2. import pubsub from 'pubsub-js'
    3. export default {
    4. name: "Student",
    5. data() {
    6. return {
    7. name: "张三",
    8. sex: "男"
    9. };
    10. },
    11. mounted(){
    12. // console.log('student',this.x);
    13. },
    14. methods:{
    15. sendStudentName(){
    16. // 触发事件
    17. // this.$bus.$emit('hello',this.name)
    18. pubsub.publish('hello',666)
    19. }
    20. }
    21. };
    22. script>
    23. <style lang="less" scoped>
    24. .student {
    25. background-color: pink;
    26. padding: 5px;
    27. margin-top: 30px;
    28. }
    29. style>

     TodoList案例消息订阅与发布

    需要数据的地方订阅消息,提供数据的地方发布消息

    nextTick

    语法:

    this.$nextTick(回调函数)

    作用:在下一次DOM更新结束后执行其指定的回调

    什么时候用:当噶便数据后,要基于更新后的新DOM进行某些操作时,要在nextTick所指定的回调函数中执行

    myitem.vue

    1. <script>
    2. import pubsub from "pubsub-js";
    3. export default {
    4. name: "MyItem",
    5. // 声明接收todo对象
    6. props: ["todo"],
    7. methods: {
    8. // 勾选、取消勾选
    9. handleCheck(id) {
    10. // console.log(id);
    11. // 通知App组件将对应的todo对象的done取反,数据在哪里,操作对象的数据在哪里
    12. // this.checkTodo(id);
    13. this.$bus.$emit("checkTodo", id);
    14. },
    15. // 删除数据
    16. handleDelete(id) {
    17. // 根据用户的反应来确定是否进行
    18. if (confirm("确定删除吗?")) {
    19. // deleteTodo是父组件的函数名
    20. // this.deleteTodo(id)
    21. // console.log(id);
    22. // 借助全局事件总线,自定义事件的事件名
    23. // this.$bus.$emit('deleteTodo',id)
    24. // 订阅消息的消息名
    25. pubsub.publish("deleteTodo", id);
    26. }
    27. },
    28. // 编辑,目的是为了使文字变成输入框
    29. handleEdit(todo) {
    30. if (todo.hasOwnProperty("isEdit")) {
    31. todo.isEdit = true;
    32. } else {
    33. console.log("todo身上没有isEdit");
    34. this.$set(todo, "isEdit", true);
    35. }
    36. // nextTick(下一轮)所指定的回调会在dom节点更新之后再执行
    37. this.$nextTick(function () {
    38. this.$refs.inputTitle.focus();
    39. });
    40. // 把文字变成输入框后,就让输入框获取焦点
    41. // 但是这句代码不能实现,是因为
    42. // 这样只能生硬的添加属性,不能使页面发声改变
    43. // todo.isEdit=true
    44. },
    45. // 失去焦点回调(真正执行修改逻辑)
    46. handleBlur(todo, e) {
    47. todo.isEdit = false;
    48. // console.log('updateTodo',todo.id,e.target.value);
    49. // 判断它输入是否为空
    50. if (!e.target.value.trim()) return alert("输入不能为空");
    51. this.$bus.$emit("updateTodo", todo.id, e.target.value);
    52. },
    53. },
    54. };
    55. script>
    56. <style>
    57. /* item */
    58. li {
    59. list-style: none;
    60. height: 36px;
    61. line-height: 36px;
    62. padding: 0 5px;
    63. border-bottom: 1px solid #ddd;
    64. }
    65. li label {
    66. float: left;
    67. cursor: pointer;
    68. }
    69. li label li input {
    70. vertical-align: middle;
    71. margin-right: 6px;
    72. position: relative;
    73. top: -1px;
    74. }
    75. li button {
    76. float: right;
    77. display: none;
    78. margin-top: 3px;
    79. }
    80. li:before {
    81. content: initial;
    82. }
    83. li:last-child {
    84. border-bottom: none;
    85. }
    86. li:hover {
    87. background-color: #ddd;
    88. }
    89. li:hover button {
    90. display: block;
    91. }
    92. style>
  • 相关阅读:
    Linux cat命令详解使用:高效文本内容管理
    Kafka: Windows环境-单机部署和伪集群、集群部署
    【活动回顾】ABeam News | 庆祝ABeam德硕与毕博中国战略合作十周年,关系再升级
    Android中如何通过perfetto抓取trace
    前端跨域方案看这篇就够了
    面试题:Java中为什么只有值传递?
    busybox命令裁剪
    list(链表)容器(二)
    DevOps转型的意义:加速创新、提高效率
    线程的六种状态
  • 原文地址:https://blog.csdn.net/m0_62520946/article/details/127606137