父组件为子组件传值为undefined

来源:1-5 项目作业

TKXZ

2023-06-28 10:39:12

父组件:

<template>
    <TopArea :title="新建收货地址" :topFunctionText="保存" />
</template>

<script>
import TopArea from './TopArea.vue'

export default {
    name: 'CreateAddress',
    components: { TopArea },
    setup() {
       
    },
}
</script>

<style lang="scss" scoped>

</style>

子组件:

<template>
    <div class="top">
        <div class="iconfont backIcon" @click="handleBack">&#xe665;</div>
        <div class="top__title">{{ title ? title : '管理收货地址' }}</div>
        <div class="top__create" @click="() => {topFunction ? topFunction() : handleCreateAddress()}">
            {{topFunctionText ? topFunctionText : '新建'}}
        </div>
    </div>
</template>

<script>
import { useRouter } from 'vue-router'

//跳转页面逻辑
const goPages = () => {
    const router = useRouter();

    const handleBack = () => {
        router.back();
    }
    const handleCreateAddress = () => {
        router.push({name: 'CreateAddress'});
    }

    return { handleBack, handleCreateAddress }
}

export default {
    name: 'TopArea',
    props: [ 'title', 'topFunction', 'topFunctionText' ],
    setup(props) {
        const { handleBack, handleCreateAddress } = goPages();
        console.log(props)
       
        return { handleBack, handleCreateAddress }
    },
}
</script>

<style lang="scss" scoped>
@import '../../style/variables.scss';

.top {
    display: flex;
    line-height: .44rem;
    background-color: $bg-color;
    padding: 0 .18rem;
    .backIcon {
        font-size: .18rem;
        color: #B6B6B6;
        transform: rotate(180deg);
    }
    &__title {
        flex: 1;
        text-align: center;
        font-size: .16rem;
        color: $content-font-color;
    }
    &__create {
        font-size: .14rem;
        color: $content-font-color;
    }
}
</style>

子组件无法使用父组件传递的值

写回答

1回答

好帮手慕小李

2023-06-28

同学你好,建议如下:

父组件:

https://img.mukewang.com/climg/649babfd096fcefe12570967.jpg

子组件:

https://img.mukewang.com/climg/649bac1909b1027e10130925.jpg

结果如下:

https://img.mukewang.com/climg/649bac2a09f88cb604450363.jpg

https://img.mukewang.com/climg/649bac31093fd29904990351.jpg

祝学习愉快!

1

0 学习 · 17877 问题

查看课程