首页 > web前端 > Vue.js > 正文

Vue3中的模板语法和vue指令怎么使用

王林
发布: 2023-05-18 15:49:06
转载
1222人浏览过

1 模板插值语法

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

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

  • 运算也是支持的

  • 操作API 也是支持的

<template>
  {{ message }}
    {{ message2==0 ? '我是老大' : '我笑的' }}
    {{ message2 + 1 }}
    {{ message.split('').map(v => `4546$v`) }}
</template>

<script setup lang="ts">
const message = "我是唐少"
const message2:number = 1
</script>
<style>
</style>
登录后复制

2 指令

  • 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修饰符

冒泡案例:

<template>
  <div @click="parent">parent
    <div @click.stop="child">child</div>
  </div>
</template>
  
<script setup lang="ts">
const child = () => {
  console.log('child');
 // 点击后不会答应parent,因为被阻止了
}
const parent = () => {
  console.log('parent');
}
  
</script>
登录后复制

阻止表单提交案例:

<template>
  <form action="/">
    <button @click.prevent="submit" type="submit">submit</button>
  </form>
</template>
<script setup lang="ts">
const submit = () => {
  console.log('child');
  
}
</script>
<style>
</style>
登录后复制

v-bind 绑定class 案例 1:

<template>
  <div :class="[flag ? 'active' : 'other', 'h']">456789</div>
</template>
<script setup lang="ts">
const flag: boolean = false;// 改成true后切换不同的效果
</script>
  
<style>
.active {
  color: red;
}
.other {
  color: blue;
}
.h {
  height: 300px;
  border: 1px solid #ccc;
}
</style>
登录后复制

v-bind 绑定class 案例 2:

<template>
  <div :class="flag">{{flag}}</div>
</template>
 // 直接绑定cls
<script setup lang="ts">
type Cls = {
  other: boolean,
  h: boolean
}
const flag: Cls = {
  other: false,
  h: true
};
</script>
<style>
.active {
  color: red;
}
.other {
  color: blue;
}
.h {
  height: 300px;
  border: 1px solid #ccc;
}
</style>
登录后复制

v-bind 绑定style案例:

<template>
  <div :>绑定style</div>
</template>
<script setup lang="ts">
type Style = {
  height: string,
  color: string
}
const style: Style = {
  height: "300px",
  color: "blue"
}
</script>
<style>
</style>
登录后复制

v-model 案例:

<template>
  <input v-model="message" type="text" />
  <div>{{ message }}</div>
</template>
<script setup lang="ts">
import { ref } from 'vue' // 实时监听
const message = ref("message")
</script>
  
<style>
.active {
  color: red;
}
.other {
  color: blue;
}
.h {
  height: 300px;
  border: 1px solid #ccc;
}
</style>
登录后复制

以上就是Vue3中的模板语法和vue指令怎么使用的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
相关标签:
vue
来源:亿速云网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号