Vue3中的模板語(yǔ)法和vue指令
1 模板插值語(yǔ)法
- 在script 聲明一個(gè)變量可以直接在template 使用用法為{{變量名稱}}
- 模板語(yǔ)法是可以編寫條件運(yùn)算的
- 運(yùn)算也是支持的
- 操作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 用來(lái)顯示文本
- v-html 用來(lái)展示富文本
- v-if 用來(lái)控制元素的顯示隱藏(切換真假DOM)
- v-else-if 表示 v-if 的“else if 塊”??梢枣?zhǔn)秸{(diào)用
- v-else v-if條件收尾語(yǔ)句
- v-show 用來(lái)控制元素的顯示隱藏(display none block Css切換)
- v-on 簡(jiǎn)寫@ 用來(lái)給元素添加事件
- v-bind 簡(jiǎn)寫: 用來(lái)綁定元素的屬性Attr
- v-model 雙向綁定
- v-for 用來(lái)遍歷元素
v-on修飾符
冒泡案例:
<template> <div @click="parent">parent <div @click.stop="child">child</div> </div> </template> <script setup lang="ts"> const child = () => { console.log('child'); // 點(diǎn)擊后不會(huì)答應(yīng)parent,因?yàn)楸蛔柚沽? } 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="style">綁定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' // 實(shí)時(shí)監(jiān)聽 const message = ref("message") </script> <style> .active { color: red; } .other { color: blue; } .h { height: 300px; border: 1px solid #ccc; } </style>
到此這篇關(guān)于Vue3中的模板語(yǔ)法和vue指令的文章就介紹到這了,更多相關(guān)vue3模板語(yǔ)法和vue指令內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue scroller返回頁(yè)面記住滾動(dòng)位置的實(shí)例代碼
這篇文章主要介紹了vue scroller返回頁(yè)面記住滾動(dòng)位置的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-01-01vue組件中節(jié)流函數(shù)的失效的原因和解決方法
這篇文章主要介紹了vue組件中節(jié)流函數(shù)的失效和解決方法,幫助大家更好的理解和學(xué)習(xí)vue框架,感興趣的朋友可以了解下2020-12-12Vue使用xlsx模塊實(shí)現(xiàn)讀寫Excel文檔內(nèi)容
在前端項(xiàng)目開發(fā)中,讀寫Excel文件的需求變得越來(lái)越常見,本文將詳細(xì)介紹如何在Vue項(xiàng)目中利用FileReader對(duì)象和xlsx模塊來(lái)讀取Excel文件的內(nèi)容,需要的可以參考下2024-03-03vue-print-nb實(shí)現(xiàn)頁(yè)面打印功能實(shí)例(含分頁(yè)打印)
在項(xiàng)目中,有時(shí)需要打印頁(yè)面的表格,在網(wǎng)上找了一個(gè)打印組件vue-print-nb,用了還不錯(cuò),所以下面這篇文章主要給大家介紹了關(guān)于vue-print-nb實(shí)現(xiàn)頁(yè)面打印功能的相關(guān)資料,需要的朋友可以參考下2022-08-08vue elementUI表格控制顯示隱藏對(duì)應(yīng)列的方法
這篇文章主要為大家詳細(xì)介紹了vue elementUI表格控制顯示隱藏對(duì)應(yīng)列的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Django+Vue.js搭建前后端分離項(xiàng)目的示例
本篇文章主要介紹了Django+Vue.js搭建前后端分離項(xiàng)目的示例,具有一定參考價(jià)值,有興趣的可以了解一下2017-08-08Mint UI實(shí)現(xiàn)A-Z字母排序的城市選擇列表
這篇文章主要為大家詳細(xì)介紹了Mint UI實(shí)現(xiàn)A-Z字母排序的城市選擇列表,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12