Vue3学习笔记

仅用于本人个人学习记录

v- 开头都是vue 的指令

v-text 用来显示文本

v-html 用来展示富文本

v-if 用来控制元素的显示隐藏(切换真假DOM)

v-else-if    v-else 

v-show 用来控制元素的显示隐藏(display none block Css切换)

v-on 简写@ 用来给元素添加事件

v-bind 简写:  用来绑定元素的属性Attr

简写动态绑定

<template>
 <div :class="['hello']" >
  hello word
 </div>
 <div :style="style">
  你好!
 </div>
</template>

<script setup lang="ts">
const style =  {
 color:'red'
}
</script>

<style lang="scss">
.hello {
  color: yellow;
}
</style>

v-model 双向绑定

<template>
<input type="text" v-model="a">
<div>{{ a }}</div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
const a = ref('hello word')

</script>

<style lang="scss">

</style>

v-for 用来遍历元素

<li v-for="item in items">
  {{ item.message }}
</li>

v-once 性能优化只渲染一次

v-memo 性能优化会有缓存具体请看我的掘金
 

阻止冒泡

@click.stop

<template>
 <div class="hello" @click="parent">
  <el-button type="warning" @click.stop="children">hello</el-button>
 </div>
</template>

<script setup lang="ts">
const parent = () => {
  console.log('我是父级')
}
const children = () => {
  console.log('我是子级')
}
</script>

ref 和 reactive 都是响应式

watch 监听

const x = ref(0)
const y = ref(0)

// 单个 ref
watch(x, (newX) => {
  console.log(`x is ${newX}`)
})

// getter 函数
watch(
  () => x.value + y.value,
  (sum) => {
    console.log(`sum of x + y is: ${sum}`)
  }
)

// 多个来源组成的数组
watch([x, () => y.value], ([newX, newY]) => {
  console.log(`x is ${newX} and y is ${newY}`)
})

watch(
  () => obj.count,
  (count) => {
    console.log(`count is: ${count}`)
  }
)

watch(
  () => state.someObject,
  (newValue, oldValue) => {
    // 注意:`newValue` 此处和 `oldValue` 是相等的
    // *除非* state.someObject 被整个替换了
  },
  { deep: true }
)

watch(source, (newValue, oldValue) => {
  // 立即执行,且当 `source` 改变时再次执行
}, { immediate: true })

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值