模板插值语法

在script 声明一个变量可以直接在template 使用用法为{{变量名称}}

  1. <template>
  2. <div>{{ message }}</div>
  3. </template>
  4. <script setup lang="ts">
  5. const message = "我是小满"
  6. </script>
  7. <style>
  8. </style>

模板语法是可以编写条件运算的

  1. <template>
  2. <div>{{ message == 0 ? '我是小满0' : '我不是小满other' }}</div>
  3. </template>
  4. <script setup lang="ts">
  5. const message:number = 1
  6. </script>
  7. <style>
  8. </style>

运算也是支持的

  1. <template>
  2. <div>{{ message + 1 }}</div>
  3. </template>
  4. <script setup lang="ts">
  5. const message:number = 1
  6. </script>
  7. <style>
  8. </style>

操作API 也是支持的

  1. <template>
  2. <div>{{ message.split(',') }}</div>
  3. </template>
  4. <script setup lang="ts">
  5. const message:string = "我,是,小,满"
  6. </script>
  7. <style>
  8. </style>

指令
v- 开头都是vue 的指令
v-text 用来显示文本
v-html 用来展示富文本
v-if 用来控制元素的显示隐藏(切换真假DOM)
v-else-if 表示 v-if 的“else if 块”。可以链式调用
v-else v-if条件收尾语句
v-show 用来控制元素的显示隐藏(display none block Css切换)
v-on 简写@ 用来给元素添加事件
v-bind 简写: 用来绑定元素的属性Attr
v-model 双向绑定
v-for 用来遍历元素
v-on修饰符 冒泡案例

  1. <template>
  2. <div @click="parent">
  3. <div @click.stop="child">child</div>
  4. </div>
  5. </template>
  6. <script setup lang="ts">
  7. const child = () => {
  8. console.log('child');
  9. }
  10. const parent = () => {
  11. console.log('parent');
  12. }
  13. </script>

阻止表单提交案例

  1. <template>
  2. <form action="/">
  3. <button @click.prevent="submit" type="submit">submit</button>
  4. </form>
  5. </template>
  6. <script setup lang="ts">
  7. const submit = () => {
  8. console.log('child');
  9. }
  10. </script>
  11. <style>
  12. </style>

v-bind 绑定class 案例 1

  1. <template>
  2. <div :class="[flag ? 'active' : 'other', 'h']">12323</div>
  3. </template>
  4. <script setup lang="ts">
  5. const flag: boolean = false;
  6. </script>
  7. <style>
  8. .active {
  9. color: red;
  10. }
  11. .other {
  12. color: blue;
  13. }
  14. .h {
  15. height: 300px;
  16. border: 1px solid #ccc;
  17. }
  18. </style>

v-bind 绑定class 案例 2

  1. <template>
  2. <div :class="flag">{{flag}}</div>
  3. </template>
  4. <script setup lang="ts">
  5. type Cls = {
  6. other: boolean,
  7. h: boolean
  8. }
  9. const flag: Cls = {
  10. other: false,
  11. h: true
  12. };
  13. </script>
  14. <style>
  15. .active {
  16. color: red;
  17. }
  18. .other {
  19. color: blue;
  20. }
  21. .h {
  22. height: 300px;
  23. border: 1px solid #ccc;
  24. }
  25. </style>

v-bind 绑定style案例

  1. <template>
  2. <div :style="style">2222</div>
  3. </template>
  4. <script setup lang="ts">
  5. type Style = {
  6. height: string,
  7. color: string
  8. }
  9. const style: Style = {
  10. height: "300px",
  11. color: "blue"
  12. }
  13. </script>
  14. <style>
  15. </style>

v-model 案例

  1. <template>
  2. <input v-model="message" type="text" />
  3. <div>{{ message }}</div>
  4. </template>
  5. <script setup lang="ts">
  6. import { ref } from 'vue'
  7. const message = ref("v-model")
  8. </script>
  9. <style>
  10. .active {
  11. color: red;
  12. }
  13. .other {
  14. color: blue;
  15. }
  16. .h {
  17. height: 300px;
  18. border: 1px solid #ccc;
  19. }
  20. </style>

————————————————
版权声明:本文为CSDN博主「小满zs」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq1195566313/article/details/122773486