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

目录

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

效果图

完整代码

[方法二:属性绑定+ 动态组件 component标签](#方法二:属性绑定+ 动态组件 component标签)

效果图

完整代码


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

效果图

完整代码

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>tab</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        h2{
            width: 500px;
            height: 400px;
            background: #9fe4d9;
            text-align: center;
            line-height: 400px;
            display: none;
        }
        #app .kk{
            width: 500px;
            height: 50px;
            display: flex;
            justify-content: space-between;
            
        }
        #app{
            
            width: 500px;
            margin: 50px auto;
        }
        span{
            flex: 1;
            text-align: center;
            line-height: 50px;
            background: #ccc;
        }
        .on{
            background: pink;
        }
        #app .onn{
            display: block;
        }
        
    </style>
</head>
<body>
    <div id="app">
        <div class="kk">
        <span :class =" n==1 && 'on'" @click.self="n=1">1</span>
        <span :class =" n==2 && 'on'" @click.self="n=2">2</span>
        <span :class =" n==3 && 'on'" @click.self="n=3">3</span>
        <span :class =" n==4 && 'on'" @click.self="n=4">4</span>
        <span :class =" n==5 && 'on'" @click.self="n=5">5</span>
        </div>
        
        <h2 :class =" n==1 && 'onn'">1</h2>
        <h2 :class =" n==2 && 'onn'">2</h2>
        <h2 :class =" n==3 && 'onn'">3</h2>
        <h2 :class =" n==4 && 'onn'">4</h2>
        <h2 :class =" n==5 && 'onn'">5</h2>

    </div>
</body>
</html>
<script type="module">
import {createApp} from './js/vue.esm-browser.js';
createApp({
    data() {
        return {
           n:1
        }  
    },
    methods:{
         
       },
}).mount('#app')
</script>

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

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

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

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

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

效果图

完整代码

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>动态组件</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .wp{
            width: 440px;
            height: 30px;
            margin: 20px auto;
            display: flex;
            background: #f0f0f0;
        }
        .wp span{
            flex: 1;
            height: 30px;
            text-align: center;
            line-height: 30px;
            cursor: pointer;
        }
        .wp span.on{
            background: pink;
            color: #fff;
        }
        h1{
            width: 440px;
            margin: 0 auto;

        }
    </style>
</head>
<body>
    <div id="app">
        <div class="wp">
            <span :class="num==1&&'on'" @click="num=1">水浒传</span>
            <span :class="num==2&&'on'" @click="num=2">红楼梦</span>
            <span :class="num==3&&'on'" @click="num=3">西游记</span>
            <span :class="num==4&&'on'" @click="num=4">三国演义</span>
        </div>

        <!-- 
            动态组件 使用标签 component 
            该组件具有一个is属性,is属性的值 是 要渲染组件的名字,即为is属性的值是哪一个组件名,
            component 标签就会渲染哪一个组件

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

            使用keep-alive 标签(组件),可以缓存曾经渲染过的组件,从而提高渲染效率
         -->
         <keep-alive>
        <component :is="'comp'+num"></component>
         </keep-alive>
    </div>
</body>
</html>
<script type="module">
 import { createApp } from './js/vue.esm-browser.js';     
 let comp1={
    template:'<h1>水浒传</h1>'
 }
 let comp2={
    template:'<h1>红楼梦</h1>'
 }
 let comp3={
    template:`
    <h1>西游记</h1>
    <p>{{n}}</p>
    <button @click = "n++">点击++</button>
    `,
    data() {
        return {
            n:100,
        }
    },
 }
 let comp4={
    template:'<h1>三国演义</h1>'
 }
 let aa = {
    template:'<h1>金瓶梅</h1>'
 }
 createApp({
    data() {
        return {
            num:1,

        }
    },

    components:{
        comp1,comp2,comp3,comp4,aa
    }
 }).mount('#app')
</script>
相关推荐
让开,我要吃人了2 小时前
HarmonyOS开发实战(5.0)实现二楼上划进入首页效果详解
前端·华为·程序员·移动开发·harmonyos·鸿蒙·鸿蒙系统
Passion不晚2 小时前
Vue vs React vs Angular 的对比和选择
vue.js·react.js·前端框架·angular.js
everyStudy3 小时前
前端五种排序
前端·算法·排序算法
甜兒.4 小时前
鸿蒙小技巧
前端·华为·typescript·harmonyos
她似晚风般温柔7896 小时前
Uniapp + Vue3 + Vite +Uview + Pinia 分商家实现购物车功能(最新附源码保姆级)
开发语言·javascript·uni-app
Jiaberrr8 小时前
前端实战:使用JS和Canvas实现运算图形验证码(uniapp、微信小程序同样可用)
前端·javascript·vue.js·微信小程序·uni-app
everyStudy8 小时前
JS中判断字符串中是否包含指定字符
开发语言·前端·javascript
城南云小白8 小时前
web基础+http协议+httpd详细配置
前端·网络协议·http
前端小趴菜、8 小时前
Web Worker 简单使用
前端
web_learning_3218 小时前
信息收集常用指令
前端·搜索引擎