vue查缺补漏

之前学习过vue基础,在工作上使用的时候也没有什么问题。最近在看30分钟学会Vue之核心语法,发现有一些不常用的、但挺重要的都快忘掉了,在此补漏一下。

搭建演示环境

创建index.html 导入 vue.min.js文件

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="app">
    <p>{{ msg }}</p>
</div>
<script src="./vue.min.js"></script>
<script>
    const vm = new Vue({
        el: '#app',
        data() {
            return {
                msg: 'Hello World',
            }
        }
    })
</script>
</body>
</html>

1. 内容指令

复制代码
    <p v-text="msg">1111</p>
    <p v-html="msg">1111</p>

不显示1111,显示msg内容 v-html 解析msg中的html标签

2. console.log 打印

复制代码
console.log(newVal,oldVal);

console打印可以直接进行参数拼接,并不需要+进行连接

3. for 遍历对象打印属性

复制代码
   <p v-for="(item,key,index) in 5">item:{{ item }},key:{{ key }},index:{{ index }}</p>
   <p v-for="(item,key,index) in obj">item:{{ item }},key:{{ key }},index:{{ index }}</p>

    obj: {name: 'gggg', age: 1111}

item:1,key:0,index:

item:2,key:1,index:

item:3,key:2,index:

item:4,key:3,index:

item:5,key:4,index:

item:gggg,key:name,index:0

item:1111,key:age,index:1

4. 放到标签上进行文本提示

复制代码
    <p title="这是内容">{{ msg }}</p>

5. input自动去空格

复制代码
        <input type="text" v-model.trim="inputVal"/> 

6. style、class绑定

在uniapp开发的时候,也会使用 当时云里雾里的。

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="app">

    <!--  前面两种指定style中定义的内容,必须使用'字符串',用,分割-->
    <!-- key:value  key 自动转为字符串 -->
    <div v-bind:class="{ textColorCls: textColorFlag,bgColorCls: bgColorFlag}">111111</div>
    <!-- 直接使用字符串,内嵌的对象,或者三元表达式 -->
    <div v-bind:class="[ 'textColorCls', {bgColorCls: bgColorFlag},textSizeFlag? 'textSizeCls' : '']">111111</div>
    <!-- 使用data中定义的 包含style的集合 -->
    <div v-bind:class="textColorClassObj">2222</div>
    <!-----------------------------不支持 {textSizeClassObj:  textSizeFlag}, --------------------------------------->
    <div v-bind:class="[textColorClassObj,  bgColorFlag ? bgColorClassObj:'']">2222
    </div>
    <div v-bind:style="{ color: textColor, fontSize: fontSize + 'px' }">333333</div>
    <div v-bind:style="textColorStyleObj">44444</div>
    <!-----------------------------不支持{textSizeStyleObj:true} --------------------------------------->
    <div v-bind:style="[{ background: bgColor}, textColorFlag ? textColorStyleObj:'']">
        44444
    </div>


</div>
<script src="./vue.min.js"></script>

<style>
    .textColorCls {
        color: red;
    }

    .bgColorCls {
        background: #ff0;
    }

    .textSizeCls {
        font-size: 30px;
    }
</style>

<script>
    const vm = new Vue({
        el: '#app',
        data() {
            return {
                textColor: 'red',
                bgColor: 'yellow',
                fontSize: 30,
                bgColorFlag: true,
                textSizeFlag: true,
                textColorFlag: true,
                //这两个不能混用
                textColorClassObj: {
                    textColorCls: true //只能指定class,并且必须写'显示'逻辑.
                },
                bgColorClassObj: {
                    bgColorCls: true //只能指定class,并且必须写'显示'逻辑.
                },
                textSizeClassObj: {
                    textSizeCls: true //只能指定class,并且必须写'显示'逻辑.
                },
                textColorStyleObj: {
                    color: 'red',
                },
                textSizeStyleObj: {
                    'font-size': 30 + 'px',
                },

            }
        },
    })
</script>
</body>
</html>
相关推荐
用户629605932470527 分钟前
从“浏览器根本区分不了”到真正实现:刷新不退出,关闭标签页自动退出,前端判断浏览器关闭和刷新
前端
qetfw29 分钟前
MWU:Vue 3 + FastAPI 的 MaaFramework 跨平台 WebUI 源码
前端·vue.js·python·fastapi·开源项目·效率工具
mfxcyh30 分钟前
Vue3+Ant Design Vue 实现手动异步文件上传(含大小校验、弹窗上传)
前端·javascript·vue.js
wordbaby31 分钟前
Web端热敏标签打印完整解决方案(QZ Tray + 译维A42)
前端
swipe35 分钟前
03|Axios 请求进了后端之后:Controller、Request、Response 是怎么接住它的?
前端·后端·全栈
meilindehuzi_a41 分钟前
Vue3进阶基础:指令修饰符、v-model表单绑定、动态样式、计算属性与侦听器实战
前端·javascript·vue.js
csdn2015_1 小时前
vue 前端运行命令
前端·javascript·vue.js
swipe1 小时前
02|从 `pnpm dev` 到 Spring Boot 启动:后端服务到底怎么跑起来?
前端·后端·全栈
swipe1 小时前
01|前端人第一次打开 Spring Boot 项目,应该先看哪里?
前端·后端·全栈
Cobyte1 小时前
根据浏览器解析 HTML 的规范实现 HTML 解析器
前端·javascript·vue.js