vue3 学习笔记11 -- 模板语法和指令

news/2024/8/26 15:31:43 标签: 学习, 笔记, vue.js

vue3 学习笔记11 – 模板语法和指令

模板语法

文本插值:使用双大括号 {{ }} 插入文本。
<script setup>
    const message = ref("Hello Vue3!!")
</script>
<template>
  <p>{{message}}</p>
</template>
指令

以 v- 开头的特殊属性,如 v-if, v-for, v-bind, v-on

  • 循环渲染列表:v-for
<template>
  <div>
   
    <ul>
      <li v-for="item in items">{{ item }}</li>
    </ul>
   
  </div>
</template>
 
<script setup>
import { ref } from 'vue'
const items = ref(['Vue', 'Vite', 'JavaScript'])
</script>
  • 事件处理:v-on:click 或简写为 @click。
<template>
  <button @click="increment">+1</button>
</template>
 
<script setup>
import { ref } from 'vue'
 
const count = ref(0)
 
function increment() {
  count.value++
}
</script>
```vue
* 双向绑定:v-model。
* 条件渲染:v-if, v-else-if, v-else, v-show
```vue
<template>
  <div v-if="type === 'A'">
    Type A
  </div>
  <div v-else-if="type === 'B'">
    Type B
  </div>
  <div v-else>
    Other type
  </div>
  <div v-show="type=='A'">使用 CSS 的 display 属性实现</div>
</template>
 
<script setup>
import { ref } from 'vue'
 
const type = ref('A')
</script>
  • 类绑定:v-bind:class 或简写为 :class。
<template>
  <div :class="{ active: isActive, 'text-success': hasSuccess }">
    Class binding
  </div>
</template>
 
<script setup>
import { ref } from 'vue'
 
const isActive = ref(true)
const hasSuccess = ref(false)
</script>
  • 样式绑定:v-bind:style 或简写为 :style。
<template>
  <div :style="{ color: activeColor, fontSize: fontSize + 'px' }">
    Style binding
  </div>
</template>
 
<script setup>
import { ref } from 'vue'
 
const activeColor = ref('red')
const fontSize = ref(20)
</script>
  • 更新元素的文本内容:v-text
<template>
 <span v-text="msg"></span>
    <!-- 等同于 -->
    <span>{{msg}}</span>
</template>
 
<script setup>
import { ref } from 'vue'
 
const msg = ref('hello')
</script>
  • 更新元素的 innerHTML:v-html
<template>
 <span v-html="msg"></span>
</template>
 
<script setup>
import { ref } from 'vue'
 
let msg = '<font color="red">msg</font>'
</script>
  • 用于具名插槽分发内容:v-slot
// A文件
<template>
    <div class="container">
        <header>
            <!-- 标题内容放这里 -->
            <slot name="header"></slot>
        </header>
        <main>
            <!-- 主要内容放这里 -->
            <slot></slot>
        </main>
        <footer>
            <!-- 底部内容放这里 -->
            <slot name="footer"></slot>
        </footer>
    </div>
</template>

// B组件
<template>
  <commonSlot>
    <template #footer>
        <div class="box">底部内容放这里</div>
    </template>

    <template v-slot:header>
        <div class="box">标题内容放这里</div>
    </template>
    
    <template #default>
        <div class="box">主要内容放这里</div>
    </template>
  </commonSlot>
</template>
<script setup lang=ts>
import commonSlot from './a.vue;
</script>
  • 一次性插值,性能优化:v-once

下述代码中,message的内容将在页面上显示一次,5秒钟后改变,但页面上的内容不会更新,仍然显示为"Hello, once!"。这是因为v-once已经在第一次渲染时将内容"Hello, once!"和数据绑定一起存储起来,后续即使数据变化,也不会更新DOM。

<template>
  <div>
    <!-- 使用v-once绑定的内容不会更新,即便message的值改变 -->
    <p v-once>This will never change: {{ message }}</p>
  </div>
</template>
 
<script>
import { ref } from 'vue';
 
export default {
  setup() {
    const message = ref('Hello, once!');
 
    // 在某个时刻改变message的值,页面上的内容不会更新
    setTimeout(() => {
      message.value = 'Hello, still once!';
    }, 5000);
 
    return { message };
  }
};
</script>
  • 跳过该元素和其子元素的编译过程:v-pre

{{ this will not be compiled }} 会被当作静态文本显示在页面上,而不是由 Vue 进行处理。

<template>
  <span v-pre>{{ this will not be compiled }}</span>
</template>

http://www.niftyadmin.cn/n/5558993.html

相关文章

Java 实验六:异常处理

一、实验目的 1、掌握异常处理方法&#xff0c;掌握自定义异常。 二、实验环境 1、windows11; 2、JDK1.8,集成开发环境Eclipse。 三、实验内容 1、自定义一个异常类ScoreException继承Exception&#xff1a;编程实现从键盘录入成绩并对成绩进行验证&#xff0c;如果录入的…

Qt纯代码绘制一个等待提示Ui控件

等待样式控件是我们在做UI时出场率还挺高的控件之一&#xff0c;通常情况下有如下的几种实现方式&#xff1a;1、自定义绘图&#xff0c;然后重写paintEvent函数&#xff0c;在paintEvent中绘制等待图标&#xff0c;通过QTimer更新绘制达到转圈圈的效果。2、 获取一张gif的资源…

2024图纸加密软件推荐|十款CAD图纸加密软件排行榜

在数字化时代&#xff0c;图纸和设计文件的保护变得尤为重要。无论是建筑蓝图、机械CAD图纸还是电子电路图&#xff0c;这些文件往往包含了企业的核心知识产权和商业机密。因此&#xff0c;选择一款可靠且高效的图纸加密软件来保障信息安全&#xff0c;成为了众多设计团队和企业…

基于搜索二叉树的停车收费管理系统

系统效果&#xff1a;录入汽车信息 查看汽车信息 收费信息查看 查询车库车辆 代码展示&#xff1a; //SearchBinaryTree.h #pragma once #include<iostream> #include<string> #include<time.h> #include<Windows.h> using namespace std;template<…

pgsql(guass)可获取到对应的表名称、字段名称、注释、字段类型

pgsql可获取到对应的表名称、字段名称、注释、字段类型(GUASS的也是适用) SELECT c.relname as 表名,a.attname as 字段名,format_type(a.atttypid,a.atttypmod) as 类型,a.attnotnull as 非空, col_description(a.attrelid,a.attnum) as 注释 FROM pg_class as c,pg_attri…

AI金融投资002:批量下载巨潮资讯基金招募说明书

文章目录 一、介绍二、输入内容三、输出内容一、介绍 打开巨潮资讯的基金招募说明书页面: http://www.cninfo.com.cn/new/fullt 动态网页,返回json数据: "adjunctUrl": "finalpage/2024-06-08/1220300147.PDF",{"classifiedAnnouncements"…

ubuntu在代码中添加异常信号捕获防止异常退出(可用于多线程程序)

1.异常信号捕获&#xff1a; 在代码运行处理大批量数据时&#xff0c;往往不想因为某一个数据的文件损坏或异常导致代码的运行退出或异常重启&#xff0c;影响系统运行的稳定性。而这些异常又不能够被try catch捕获到&#xff0c;因此在某些特殊应用中&#xff0c;需要进行异常…

服务器数据恢复—RAID5阵列重建重建导致数据丢失的数据恢复案例

服务器数据恢复环境&故障&#xff1a; 一台服务器&#xff0c;有一组由5块硬盘组建的raid5磁盘阵列。 服务器在运行过程中一块有磁盘掉线&#xff0c;由于raid5阵列支持一块磁盘掉线的特性&#xff0c;服务器还在正常工作。不久之后服务器出现故障&#xff0c;管理员在不了…