Vue3基础语法(⼀)

本文详细介绍了Vue3中的基础语法,包括模板语法如Mustache表达式、v-once指令、v-text和v-html的使用,以及v-bind的多种绑定方式。同时,讲解了如何绑定class和style,以及事件处理v-on的基本用法和参数传递,还涉及动态绑定属性和对象。文章通过实例代码展示了这些概念在实际应用中的操作。
摘要由CSDN通过智能技术生成

02——Vue3基础语法(⼀)


上一节中的问题:
在这里插入图片描述

答案:
在这里插入图片描述
前端面试之彻底搞懂this指向

在这里插入图片描述

VSCode代码片段

在这里插入图片描述

在这里插入图片描述
https://snippet-generator.app

在这里插入图片描述
在这里插入图片描述

模板语法

在这里插入图片描述

Mustache

<body>
    <div id="app"></div>
    <template id="my-app">
        <!-- mustache的基本使用 -->
        <h2>{{message}}</h2>
        <!-- 是一个表达式 -->
        <h2>{{counter * 100}}</h2>
        <h2>{{message.split(" ").reverse().join(" ")}}</h2>
        <!-- 也可以调用函数 -->
        <h2>{{getReverseMessage()}}</h2>
        <!-- 三元运算符 -->
        <h2>{{ isShow ? "阿司匹林" : ""}}</h2>
        <button @click="toggle">切换</button>
    </template>

    <script src="../js/vue.js"></script>
    <script>
        const App = {
            template: '#my-app',
            data() {
                return {
                    message: "hello world",
                    counter: 10,
                    isShow: true
                }
            },
            methods: {
                getReverseMessage() {
                    return this.message.split(" ").reverse().join(" ")
                },
                toggle() {
                    this.isShow = !this.isShow
                }
            },
        }

        Vue.createApp(App).mount("#app")
    </script>
</body>

v-once指令

在这里插入图片描述

<body>
    <div id="app"></div>
    <template id="my-app">
        <h2>{{counter}}</h2>
        <h2 v-once>v-once: {{counter}}</h2>
        <div v-once>
            <h2>{{counter}}</h2>
        </div>
        <button @click="increment">+1</button>
    </template>

    <script src="../js/vue.js"></script>
    <script>
        const App = {
            template: '#my-app',
            data() {
                return {
                    counter: -1
                }
            },
            methods: {
                increment() {
                    this.counter++
                }
            },
        }

        Vue.createApp(App).mount("#app")
    </script>
</body>

以上代码,点击+1 只有第一行的counter变化, 下面两行一直都是显示是-1

v-text

在这里插入图片描述
不过还是推荐使用Mustache语法

v-html

在这里插入图片描述
<h2 v-html="message"></h2>

v-pre

在这里插入图片描述

v-bind

在这里插入图片描述

<body>
    <div id="app"></div>
    <template id="my-app">
        <!-- v-bind的基本使用 -->
        <img v-bind:src="imgUrl" alt="">
        <a v-bind:href="link">百度一下</a>
        <!-- v-bind 提供的一个语法糖 -->
        <img :src="imgUrl" alt="">
    </template>

    <script src="../js/vue.js"></script>
    <script>
        const App = {
            template: '#my-app',
            data() {
                return {
                    imgUrl: "https://pics7.baidu.com/feed/bf096b63f6246b601b80a47592b08f4a500fa2f7.jpeg?token=5ca864652fbb35921a2127578f40c7b3",
                    link: "http://www.baidu.com"
                }
            }
        }

        Vue.createApp(App).mount("#app")
    </script>
</body>

绑定class

在这里插入图片描述

对象语法
<style>
    .active {
        color: red;
    }

    .why {
        font-size: 25px;
    }
</style>
-------------------------------------------------------------------------------------
<body>
    <div id="app"></div>
    <template id="my-app">
        <div :class="className">hhhhhhhhh</div>
        <!-- 对象语法:{'类名' : boolean} -->
        <div :class="{'active': isActive}">呵呵哈哈哈</div>
        <button @click="toggle">切换</button>

        <!-- 类名的引号不写也正常 -->
        <div :class="{active: isActive}">类名没引号</div>

        <!-- 也可以有多个键值对 -->
        <div :class="{'active': isActive, 'title': isTitle}">嗯嗯</div>

        <!-- 默认的class和动态的class结合 -->
        <div class="abc cba" :class="{'active': isActive, 'title': isTitle}">test</div>

        <!-- 将对象放到一个单独的属性 -->
        <div :class="classObj">单独的属性</div>

        <!-- 将返回的对象放到一个methods(computed)方法中 -->
        <div :class="getClassObj()">方法</div>
    </template>

    <script src="../js/vue.js"></script>
    <script>
        const App = {
            template: '#my-app',
            data() {
                return {
                    className: 'why',
                    isActive: true,
                    isTitle: true,
                    classObj: {
                        active: true,
                        title: true
                    }
                }
            },
            methods: {
                toggle() {
                    this.isActive = !this.isActive
                },
                getClassObj() {
                    return {
                        active: true,
                        title: true
                    }
                }
            },
        }

        Vue.createApp(App).mount("#app")
    </script>
</body>

数组语法
<div id="app"></div>
<template id="my-app">
    <div :class="['abc', title]"></div>
    <div :class="['abc', title, isActive ? 'actve' : '']"></div>
    <div :class="['abc', title, {'active': isActive}]"></div>
</template>

<script src="../js/vue.js"></script>
<script>
    const App = {
        template: '#my-app',
        data() {
            return {
                isActive: true,
                title: 'cba'
            }
        }
    }

    Vue.createApp(App).mount("#app")
</script>

绑定style

在这里插入图片描述


<body>
    <div id="app"></div>
    <template id="my-app">
        <div :style="{color: finalColor, fontSize: '50px'}">hhhhhh</div>
        <div :style="styleObj">绿色</div>
        <div :style="getStyleObj()">eeee</div>
        <!-- 数组 -->
        <div :style="[styleObj2, styleObj]">ttttt</div>
    </template>

    <script src="../js/vue.js"></script>
    <script>
        const App = {
            template: '#my-app',
            data() {
                return {
                    finalColor: 'red',
                    styleObj: {
                        color: 'yellow',
                        backgroundColor: 'red',
                        fontSize: '22px'
                    },
                    styleObj2: {
                        fontWeight: 700,
                        textDecoration: 'underline',
                        padding: '10px'
                    }
                }
            },
            methods: {
                getStyleObj() {
                    return {
                        color: 'skyblue',
                        fontSize: '50px',
                        fontWeight: 700
                    }
                }
            },
        }

        Vue.createApp(App).mount("#app")
    </script>
</body>

在这里插入图片描述

动态绑定属性

在这里插入图片描述在这里插入图片描述

绑定一个对象

在这里插入图片描述

v-on

在这里插入图片描述

基本使用

<body>
    <div id="app"></div>
    <template id="my-app">
        <!-- 完整写法v-on:监听的事件"methods中的方法"-->
        <button v-on:click="btnClick">按钮</button>
        <div class="area" v-on:mousemove="mouseMove"></div>
        <!-- 语法糖@ -->
        <button @click="btnClick">按钮</button>
        <!-- 绑定一个表达式 -->
        <button @click="counter++">{{counter}}</button>
        <!-- 绑定一个对象 -->
        <div class="area" v-on="{click: btnClick, mousemove: mouseMove}"></div>
    </template>

    <script src="../js/vue.js"></script>
    <script>
        const App = {
            template: '#my-app',
            data() {
                return {
                    counter: 100
                }
            },
            methods: {
                btnClick() {
                    console.log("鼠标点击事件")
                },
                mouseMove() {
                    console.log("鼠标移动")
                }
            },
        }

        Vue.createApp(App).mount("#app")
    </script>
</body>

参数传递

在这里插入图片描述

修饰符

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lalaxuan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值