• Vue常见的实现tab切换的两种方法


    目录

    方法一:事件绑定+属性绑定

    效果图

     完整代码

    方法二:属性绑定+ 动态组件 component标签

    效果图

    完整代码 


    方法一:事件绑定+属性绑定

    效果图

     完整代码

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>tabtitle>
    7. <style>
    8. *{
    9. margin: 0;
    10. padding: 0;
    11. }
    12. h2{
    13. width: 500px;
    14. height: 400px;
    15. background: #9fe4d9;
    16. text-align: center;
    17. line-height: 400px;
    18. display: none;
    19. }
    20. #app .kk{
    21. width: 500px;
    22. height: 50px;
    23. display: flex;
    24. justify-content: space-between;
    25. }
    26. #app{
    27. width: 500px;
    28. margin: 50px auto;
    29. }
    30. span{
    31. flex: 1;
    32. text-align: center;
    33. line-height: 50px;
    34. background: #ccc;
    35. }
    36. .on{
    37. background: pink;
    38. }
    39. #app .onn{
    40. display: block;
    41. }
    42. style>
    43. head>
    44. <body>
    45. <div id="app">
    46. <div class="kk">
    47. <span :class =" n==1 && 'on'" @click.self="n=1">1span>
    48. <span :class =" n==2 && 'on'" @click.self="n=2">2span>
    49. <span :class =" n==3 && 'on'" @click.self="n=3">3span>
    50. <span :class =" n==4 && 'on'" @click.self="n=4">4span>
    51. <span :class =" n==5 && 'on'" @click.self="n=5">5span>
    52. div>
    53. <h2 :class =" n==1 && 'onn'">1h2>
    54. <h2 :class =" n==2 && 'onn'">2h2>
    55. <h2 :class =" n==3 && 'onn'">3h2>
    56. <h2 :class =" n==4 && 'onn'">4h2>
    57. <h2 :class =" n==5 && 'onn'">5h2>
    58. div>
    59. body>
    60. html>
    61. <script type="module">
    62. import {createApp} from './js/vue.esm-browser.js';
    63. createApp({
    64. data() {
    65. return {
    66. n:1
    67. }
    68. },
    69. methods:{
    70. },
    71. }).mount('#app')
    72. script>

    方法二:属性绑定+ 动态组件 component标签

                该组件具有一个is属性,is属性的值 是 要渲染组件的名字,即为is属性的值是哪一个组件名,

                component 标签就会渲染哪一个组件

                缺点:component 可以动态渲染组件的内容,但是每一个切换,都会重新渲染组件内容,降低渲染效率

                使用keep-alive 标签(组件),可以缓存曾经渲染过的组件,从而提高渲染效率

    效果图

    完整代码 

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>动态组件title>
    7. <style>
    8. *{
    9. margin: 0;
    10. padding: 0;
    11. }
    12. .wp{
    13. width: 440px;
    14. height: 30px;
    15. margin: 20px auto;
    16. display: flex;
    17. background: #f0f0f0;
    18. }
    19. .wp span{
    20. flex: 1;
    21. height: 30px;
    22. text-align: center;
    23. line-height: 30px;
    24. cursor: pointer;
    25. }
    26. .wp span.on{
    27. background: pink;
    28. color: #fff;
    29. }
    30. h1{
    31. width: 440px;
    32. margin: 0 auto;
    33. }
    34. style>
    35. head>
    36. <body>
    37. <div id="app">
    38. <div class="wp">
    39. <span :class="num==1&&'on'" @click="num=1">水浒传span>
    40. <span :class="num==2&&'on'" @click="num=2">红楼梦span>
    41. <span :class="num==3&&'on'" @click="num=3">西游记span>
    42. <span :class="num==4&&'on'" @click="num=4">三国演义span>
    43. div>
    44. <keep-alive>
    45. <component :is="'comp'+num">component>
    46. keep-alive>
    47. div>
    48. body>
    49. html>
    50. <script type="module">
    51. import { createApp } from './js/vue.esm-browser.js';
    52. let comp1={
    53. template:'

      水浒传

      '
    54. }
    55. let comp2={
    56. template:'

      红楼梦

      '
    57. }
    58. let comp3={
    59. template:`
    60. 西游记

    61. {{n}}

    62. `,
    63. data() {
    64. return {
    65. n:100,
    66. }
    67. },
    68. }
    69. let comp4={
    70. template:'

      三国演义

      '
    71. }
    72. let aa = {
    73. template:'

      金瓶梅

      '
    74. }
    75. createApp({
    76. data() {
    77. return {
    78. num:1,
    79. }
    80. },
    81. components:{
    82. comp1,comp2,comp3,comp4,aa
    83. }
    84. }).mount('#app')
    85. script>

  • 相关阅读:
    原生IP是什么?如何测试代理是不是原生IP?
    Nginx实现负载均衡
    Python2.7 pip安装tensorflow-1.15
    C++PrimerPlus 第六章 分支语句和逻辑运算符(编程练习)
    Redis基础知识:redis有16个数据库、redis是单线程的!
    java基于Springboot+vue 音乐播放器论坛交流管理系统 element 前后端分离
    二、HTTP协议基础
    经典OJ题:随机链表的复制
    深眸科技自研轻辙视觉引擎,以AI机器视觉赋能杆号牌识别与分拣
    智能生活 App SDK 开发入门教程【内附代码段 】
  • 原文地址:https://blog.csdn.net/qq_52799985/article/details/134634411