美文网首页
Vue3_04(模板语法 & vue指令)

Vue3_04(模板语法 & vue指令)

作者: BingJS | 来源:发表于2022-03-23 15:17 被阅读0次

模板插值语法

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

<template>
  <div>{{ message }}</div>
</template>
<script setup lang="ts">
const message = "hello"
</script>
<style>
</style>

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

<template>
  <div>{{ message == 0 ? 'hello' : 'hi' }}</div>
</template>
<script setup lang="ts">
const message:number = 1
</script>
<style>
</style>

运算也是支持的

<template>
  <div>{{ message  + 1 }}</div>
</template>
<script setup lang="ts">
const message:number = 1
</script>
<style>
</style>

操作API 也是支持的

<template>
  <div>{{ message.split(',') }}</div>
</template>
<script setup lang="ts">
const message:string = "我,是,小,明"
</script>
<style>
</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修饰符 冒泡案例:

<template>
  <div @click="parent">
    <div @click.stop="child">child</div>
  </div>
</template>
<script setup lang="ts">
const child = () => {
  console.log('child');
}
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']">12323</div>
</template>
<script setup lang="ts">
const flag: boolean = false;
</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>
<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="style">2222</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("v-model")
</script>
<style>
.active {
  color: red;
}
.other {
  color: blue;
}
.h {
  height: 300px;
  border: 1px solid #ccc;
}
</style>

相关文章

  • Vue3_04(模板语法 & vue指令)

    模板插值语法 在script声明一个变量可以直接在template 使用用法为{{变量名称}} 模板语法是可以编写...

  • 01Vue基本使用与模板语法

    Vue基本使用与模板语法 一. 基本使用 Hello World快速入门 二. 模板语法 指令 概述 指令的本质就...

  • Vue 成长之旅 | Vue基础用法一

    一、Vue 基础使用 二 、 Vue的指令与过滤器 1、指令的概念 指令: 是vue为开发者提供的模板语法 , 用...

  • # vue模板语法 ( v-text 和 {{}} 区别)

    vue模板语法 插值 文本 1.“Mustache”语法插值: html: js: 2.v-text指令html ...

  • 3.vue模板语法

    vue模板语法 指令 (Directives) 是带有 v- 前缀的特殊特性。指令特性的值预期是单个 JavaSc...

  • Comparison Between Vue 1 and Vue

    模板语法 插值 Vue 2 提供了 v-once 指令进行一次插值,替代了 Vue 1 的 {{ * msg }}...

  • vue第四节

    模板语法 在vue中,我们使用mustache插值({{}})来将数据渲染在模板中 使用v-once指令可以控制只...

  • vue模板语法与指令

    vue模板语法篇 插值 在vue项目中,要动态的显示某个值你只需要使用双大括号{{}},当vue监测到值发生改变,...

  • Vue/Es5,6,7

    Vue模板语法 模板的理解:动态的html页面包含了一些JS语法代码大括号表达式指令(以v-开头的自定义标签属性)...

  • Vue基础使用

    简介 创建vue实例模板语法计算属性指令事件处理器表单控件生命周期 Vue实例 Vue组件介绍 组件系统是将一个大...

网友评论

      本文标题:Vue3_04(模板语法 & vue指令)

      本文链接:https://www.haomeiwen.com/subject/vqmorrtx.html