哈尔滨餐饮网站建设,自己做网站 需要哪些,中国楼市未来发展趋势,商城网站源代码文章目录 一、defineProps() 和 defineEmits()二、defineModel() 的双向绑定2.1、基础示例2.2、定义类型2.3、声明prop名称2.4、其他声明2.5、绑定多个值2.6、修饰符和转换器2.7、修饰符串联 一、defineProps() 和 defineEmits()
组件之间通讯#xff0c;通过 props 和 emits… 文章目录 一、defineProps() 和 defineEmits()二、defineModel() 的双向绑定2.1、基础示例2.2、定义类型2.3、声明prop名称2.4、其他声明2.5、绑定多个值2.6、修饰符和转换器2.7、修饰符串联 一、defineProps() 和 defineEmits()
组件之间通讯通过 props 和 emits 进行通讯是单向数据流 子组件不能改变父组件传递给它的 prop 属性推荐的做法是它抛出事件通知父组件自行改变绑定的值。
为了在声明 props 和 emits 选项时获得完整的类型推导支持我们可以使用 defineProps 和 defineEmits API它们将自动地在 script setup 中可用
父组件
templatedivChildMy v-model:countcount /{{ count }}/div
/templatescript setupimport ChildMy from ./child.vueimport { ref } from vue const count ref(1)
/script子组件
templatediv{{ props.count }}button clickupdatedCountchild btn/button/div
/templatescript setup
const props defineProps([count]);
const emit defineEmits([update:count]);const updatedCount () {emit(update:count, props.count 1)
}
/scriptdefineProps 和 defineEmits 都是只能在
二、defineModel() 的双向绑定
这个宏可以用来声明一个双向绑定 prop通过父组件的 v-model 来使用。
在底层这个宏声明了一个 model prop 和一个相应的值更新事件。如果第一个参数是一个字符串字面量它将被用作 prop 名称否则prop 名称将默认为 “modelValue”。在这两种情况下你都可以再传递一个额外的对象它可以包含 prop 的选项和 model ref 的值转换选项。
defineModel() 的双向绑定是在编译之后创建了一个model的ref变量以及一个modelValue的props并且watch了props中的modelValue当子组件中的modelValue更新时会触发update:modelValue事件当父组件接收到这个事件时候同时更新父组件的变量。
2.1、基础示例
父组件
templatedivChildMy v-modelmessage/{{ message }}/div
/templatescript setupimport ChildMy from ./child.vueimport { ref } from vue const message ref(hello)
/script子组件
templatediv{{ message }}button clickupdatedMsgchild btn/button/div
/templatescript setup
const message defineModel()const updatedMsg () {message.value world
}
/script2.2、定义类型
子组件
templatediv{{ message }}button clickupdatedMsgchild btn/button/div
/templatescript setup
const message defineModel({ type: String })const updatedMsg () {message.value world
}
/script2.3、声明prop名称
父组件
templatedivChildMy v-model:countcount/{{ count }}/div
/templatescript setupimport ChildMy from ./child.vueimport { ref } from vue const count ref(1)
/script子组件
templatediv{{ count }}button clickupdatedCountchild btn/button/div
/templatescript setup
const count defineModel(count)
const updatedCount () {count.value
}
/script2.4、其他声明
子组件
templatediv{{ count }}button clickupdatedCountchild btn/button/div
/templatescript setup
const count defineModel(count, { type: Number, default: 0 , required: true})
const updatedCount () {count.value
}
/script2.5、绑定多个值
父组件
templatedivChildMy v-model:countcount v-model:personperson /{{ person }} - {{ count }}/div
/templatescript setup
import ChildMy from ./components/child.vue
import { ref,reactive } from vue
const count ref(1)
const person reactive ({name: Lucy,age: 11})
/script子组件
templatediv{{ person }} - {{ count }}button clickupdatedDatachild btn/button/div
/templatescript setup
const person defineModel(person)
const count defineModel(count)const updatedData () {count.value person.value.age 22person.value.name lilei
}
/script2.6、修饰符和转换器
为了获取 v-model 指令使用的修饰符我们可以像这样解构 defineModel() 的返回值
const [modelValue, modelModifiers] defineModel()当存在修饰符时我们可能需要在读取或将其同步回父组件时对其值进行转换。我们可以通过使用 get 和 set 转换器选项来实现这一点
父组件
templatedivChildMy v-model.trimmessage/{{ message }}/div
/templatescript setupimport ChildMy from ./child.vueimport { ref } from vue const message ref( hello )
/script子组件
templatediv{{ message }}button clickupdatedMsgchild btn/button/div
/templatescript setup
const [message, modelModifiers] defineModel({set(value) {if (modelModifiers.trim) {valuevalue?.trim()}return value}
})const updatedMsg () {message.value world
}
/script2.7、修饰符串联
父组件
templatedivChildMy v-model.trim.lowercasemessage/{{ message }}/div
/templatescript setupimport ChildMy from ./child.vueimport { ref } from vue const message ref(Hello)
/script子组件
templatediv{{ message }}button clickupdatedMsgchild btn/button/div
/templatescript setup
const [message, modelModifiers] defineModel({get(value) {if (modelModifiers.trim) {valuevalue?.trim()}if (modelModifiers.lowercase) {valuevalue?.toLowerCase();}return value},set(value) {if (modelModifiers.trim) {valuevalue?.trim()}if (modelModifiers.lowercase) {valuevalue?.toLowerCase();}return value}
})const updatedMsg () {message.value World
}
/script