当前位置: 首页 > news >正文

vue3 学习记录

文章目录

      • props
        • 组合式组件 使用<script setup \>
        • 组合式组件 没有使用 <script setup\>
        • 选项式组件 this
      • emits
        • 组合式组件 使用<script setup \>
        • 组合式组件 没有使用 <script setup\>
        • 选项式组件 this
      • v-model 组件数据绑定
        • 单个model
        • 多个model
        • 实现 model 修饰符

props

组合式组件 使用<script setup >
<script setup>// const props = defineProps(['title']) // 以数组方式const props = defineProps({title: String,}) // 以对象方式,声明类型console.log(props.title)
</script>
<template><span>{{title}}</span>
</template>
组合式组件 没有使用 <script setup>
<script>export default{props: ['title'],setup(props){console.log(props.title);}}
</script>
选项式组件 this
<script>export default {props: ['foo'],created() {// props 会暴露到 `this` 上console.log(this.foo)}}
</script>

emits

组合式组件 使用<script setup >
<script setup>import {ref} from 'vue'const formValue = ref({})// const emit= defineEmits(['submit']) // 以数组方式const emit = defineEmits({submit(payload: { email: string, password: string }) {//通过返回值为 `true` 还是为 `false` 来判断验证是否通过if (email && password) {return true} else {console.warn('Invalid submit event payload!')return false}}})// 以对象方式,声明类型function buttonClick() {emit('submit', formValue.value)}
</script>
<template><button @click="emit('submit', formValue)">提交</button >
</template>
组合式组件 没有使用 <script setup>
<script>export default{emits: ['inFocus', 'submit'],setup(props, ctx) {ctx.emit('submit')}}
</script>
选项式组件 this
<script>export default {data: ()=>{return {formValue: {}}},emits: {click: null, // 没有校验// 校验 submit 事件submit: ({ email, password }) => {if (email && password) {return true} else {console.warn('Invalid submit event payload!')return false}}},methods: {submit() {this.$emit('submit',this.formValue)}},}
</script>
<template><button @click="$emit('submit', formValue)">提交</button >
</template>

v-model 组件数据绑定

单个model
// Parent.vue
<Child v-model="countModel" />// Child.vue
<script setup>const model= defineModel()function buttonClick() {model.value++}
</script>
<template><input v-model="model" /><div>Parent bound v-model is: {{ model }}</div>
</template>
多个model
// Parent.vue
<Child v-model:first-name="first"v-model:last-name="last" 
/>// Child.vue
<script setup>const firstName = defineModel('firstName')const lastName = defineModel('lastName')
</script>
实现 model 修饰符
// Parent.vue
<Child v-model.capitalize="myText" />// Child.vue
<script setup>const [model, modifiers]= defineModel({set(value){if(modifiers.capitalize){return value.charAt(0).toUpperCase() + value.slice(1)}else{return value}}})
</script>
<template><input type="text" v-model="model" />
</template>
http://www.lryc.cn/news/386421.html

相关文章:

  • spring boot jar 启动报错 Zip64 archives are not supported
  • BASH and SH in SHELL scripts
  • Qt Creator创建一个用户登录界面
  • 等保测评练习卷14
  • 学懂C#编程:常用高级技术——学会C#多线程开发(三):学会线程池的使用
  • maven-gpg-plugin插件
  • Linux——echo命令,管道符,vi/vim 文本编辑器
  • CISCN--西南半决赛--pwn
  • DIYGW UniApp低代码可视化平台:高效、灵活、安全的应用开发新途径
  • Python爬虫技术及其原理探秘
  • 堆和栈的区别及应用场景
  • vant的dialog触发了其他overlay
  • Linux驱动开发笔记(十二)并发与竞争
  • 【Mac】Listen 1 for Mac(最强的音乐搜索工具)软件介绍
  • nginx 1024 worker_connections are not enough while connecting to upstream
  • 在Ubuntu 16.04上安装和配置Elasticsearch的方法
  • C#给SqlSugar封装一个单例类
  • Postman接口测试工具的原理及应用详解(六)
  • 【算法 之插入排序 原理及案例】
  • 第一节:如何开发第一个spring boot3.x项目(自学Spring boot 3.x的第一天)
  • JS逆向:由 words 、sigBytes 引发的一系列思考与实践
  • 计算机的错误计算(十五)
  • 制作img文件
  • GB28181视频汇聚平台EasyCVR接入Ehome设备视频播放出现异常是什么原因?
  • Java利用poi实现word,excel,ppt,pdf等各类型文档密码检测
  • 顺序表与链表学习笔记
  • 2.SQL注入-字符型
  • 在Ubuntu 14.04上安装和配置Elasticsearch的方法
  • C++:inline关键字nullptr
  • 数字信号处理实验三(IIR数字滤波器设计)