通过路由跳转传值
import { useRouter } from "vue-router";
setup() {
const router = useRouter();
router.push({ name: 'nextindex', query: { id: it.id } })
}
import { useRoute } from "vue-router";
setup() {
const route = useRoute();
let id = route.query.id;
console.log(id);
}
父向子传值
父组件
<goods-list :cid="id" />
子组件
props: ['cid'],
setup(props) {
console.log(props.cid)
}
子向父传值
setup(props,context){
context.emit('click-receive','向父组件传的值')
}
<template>
<!--自定义事件click-receive-->
<div @click-receive="parentEmit"></div>
</template>
setup() {
function parentEmit(val){
console.log(val)
}
return{
parentEmit
}
}
</script>
使用provide、inject实现后代传值
先辈组件
import { provide } from "vue";
export default {
setup() {
provide("values", "我是传的值");
}
}
后代组件
import { inject } from "vue";
export default {
setup() {
const values = inject("values");
console.log(values);
}
}
|