吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1513|回复: 4
收起左侧

[求助] vue3 子组件的值如何随着父组件的值改变而改变呢?

[复制链接]
楚子沦i 发表于 2021-9-25 12:19
首先说一下为啥不发悬赏区,因为我的cb是负的,所以发到求助区来了。

我想让我的子组件的值随着父组件值的修改而修改,但是我通过watch监听不到,不知道该咋整,求大佬帮忙瞅瞅。

其实就是我通过点击来修改ifshowinfo,然后传值到父组件,之后父组件再将修改好的ifshowinfo传递给另一个子组件,但是另一个子组件只能接受最初的ifshowinfo,我想让他随时都能监听到值的修改,咋整呢?

[HTML] 纯文本查看 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no">
    <script src="https://unpkg.com/vue@next"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        ul li {
            list-style: none;
        }
        #app {
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            display: flex;
            flex-direction: column;
        }
        .header {
            flex: 1;
            position: absolute;
            height: 5%;
            text-align: center;
            width: 100%;
        }
        .footer {
            flex: 1;
            height: 5%;
            position: absolute;
            bottom: 0;
            left: 0;
            right: 0;
        }
        .footer ul {
            display: flex;
        }
        .footer ul li {
            flex: 1;
            background: white;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .shoplist {
            position: absolute;
            margin-top: 2rem;
            overflow:auto;
            height: 90%;
        }
        .shoplist ul li div img {
            width: 10rem;
            height: 10rem;
        }
        .shoplist ul {
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
        }
    </style>
</head>
<body>
    <div id="app">
        <shoptitle :ifshowShopInfo="ifshowShopInfo"></shoptitle>
        <shoplist @ifshowshopinfo="getshow"></shoplist>
        <footerlist></footerlist>
    </div>
    <script>
        const app = Vue.createApp({
            data() {
                return {
                    ifshowShopInfo: ''
                }
            },
            methods: {
                getshow(data){
                    this.ifshowShopInfo = data
                }
            }
        })
        app.component("shoptitle", {
            props: {
                ifshowShopInfo: {
                    type: Object
                }
            },
            template: `<div class="header" @click="djbtn">
            <h2 v-if="!ifshowShopInfo">主页</h2>
            <h2 v-if="ifshowShopInfo">商品详情页</h2>
        </div>`,
            data(){
                return{
                    ifshow: this.ifshowShopInfo || ""
                }
            },
            methods: {
                djbtn(){
                    console.log(this.ifshowShopInfo)
                }
            },
            watch: {
                ifshowShopInfo(val) {
                    this.ifshow = val
                },
                ifshow(val) {
                    if (this.ifshowShopInfo !== val) {
                        console.log(this.ifshowShopInfo)
                    }
                    else {
                        console.log(this.ifshowShopInfo)
                    }
                }
            }
        })
        app.component("shoplist", {
            data() {
                return {
                    shoplist: [
                        {name: "德芙",price: "¥69.90",src: "df.jpg"},
                        {name: "德芙",price: "¥69.90",src: "df.jpg"},
                        {name: "德芙",price: "¥69.90",src: "df.jpg"},
                        {name: "德芙",price: "¥69.90",src: "df.jpg"},
                        {name: "德芙",price: "¥69.90",src: "df.jpg"},
                        {name: "德芙",price: "¥69.90",src: "df.jpg"},
                        {name: "德芙",price: "¥69.90",src: "df.jpg"},
                        {name: "德芙",price: "¥69.90",src: "df.jpg"}
                    ],
                    ifshowShopInfo: false,
                    shopInfo: {name: "德芙",price: "¥69.90",src: "df.jpg"}
                }
            },
            methods: {
                showShopInfo(){
                    this.ifshowShopInfo = true
                    this.$emit("ifshowshopinfo",this.ifshowShopInfo)
                }
            },
            template: `<div class="shoplist">
            <ul>
              <li v-for="(item,index) in shoplist" :key="index" v-if="!ifshowShopInfo" @click="showShopInfo">
                <div><img :src="item.src" alt=""></div>
                <div>{{item.name}} <span>{{item.price}}</span></div>
              </li>
            </ul>
            <div v-if="ifshowShopInfo">
              <img :src="shopInfo.src" alt="">
              <p>{{shopInfo.name}} <span>{{shopInfo.price}}</span></p>
            </div>
            </div>`
        })
        app.component("footerlist", {
            data() {
                return {
                    footerlist: [1,2,3,4]
                }
            },
            template: `<div class="footer">
            <ul>
              <li v-for="(item,index) in footerlist" :key="index">{{item}}</li>
            </ul>
            </div>`
        })
        const vm = app.mount("#app")
    </script>
</body>
</html>

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| 楚子沦i 发表于 2021-9-26 10:53
解决了,是props中传值的变量名应该是全小写。
247700432 发表于 2021-9-25 12:34
如果同一个父组件的子组件你可以用 this.$emit 来将值传递给父组件, 子组件用props属性接收父组件的参数变化。  如果要是跨组件了,建议你去看看vuex,状态管理可以无视组件,全局有效。
jiangteddy 发表于 2021-9-25 13:23
wangad123 发表于 2021-9-25 22:57
父组件写ifshowShopInfo方法
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 22:39

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表