The problem of passing values between Vue parent and child components

question

the parent component passes a value to the child component, and the child component receives the modified value. Why did it report an error when writing a click event to that value

event runs when clicking changeTitle event, but why does the error report

clipboard.png

related codes

APP.vue

<template>
    <div id="app">
        <index v-bind:list="list" v-bind:Title="Title"></index>
        <other  v-bind:Title="Title"></other>
        <footer class="footer display-flex">
            <a href="javacript:void(0);" class="box-flex" v-for="(footer,index) in Footers" :key="footer.id" 
            :class="{active:index == num}" @click="nav(index)">{{footer}}</a>
        </footer>
    </div>
</template>

<script>
    import index from "./components/index"
    import other from "./components/other"
    export default {
        name: "App",
        data() {
            return {
                Footers: ["", "", "", ""],
                isTrue: false,
                num:0,
                Title:"APP.vue",
                list: [{
                        image: "/static/image/nav-icon (1).png",
                        name: ""
                    },
                    {
                        image: "/static/image/nav-icon (2).png",
                        name: ""
                    },
                    {
                        image: "/static/image/nav-icon (3).png",
                        name: ""
                    }
                    ,
                    {
                        image: "/static/image/nav-icon (4).png",
                        name: ""
                    }
                    ,
                    {
                        image: "/static/image/nav-icon (5).png",
                        name: ""
                    }
                ]
            }
        },
        methods: {
            nav(index) {
                this.num=index
            }
        },
        components: {
            index,
            other
        }
    }
</script>

index.vue

<template>
    <div class="index">
        <h1 >{{title1}}</h1>
        <p @click="changeTitle">{{Title}}

<img src="../assets/image/banner (1).jpg" /> <ul class="list"> <li class="list-item" v-for="item in list" :key="item.id"> <span class="item-icon"> <img v-bind:src="item.image"/> </span>

{{item.name}}

</li> </ul> </div> </template> <script> export default { name: "index", // props:["list"], props: { list: { type: Array, required: true }, Title:{ type:String } }, data() { return { title1: "", } }, methods: { changeTitle:function () { this.Title="" } }, } </script>

other.vue

<template>
    <div class="other">
        

<h2>{{Title}}</h2> </div> </template> <script> export default { name:"other", props: { Title: { type: String, }, }, data() { return { } }, } </script>
Jun.27,2021

the data flow of the props of the component in the Vue2 is changed to one-way flow, the data transferred outside the component can only be passively received within the component, and the props data transmitted from the outer layer cannot be modified within the component.
means that you cannot directly modify the values in props.

changeTitle() {
    this.$emit('changeTitle', titlevalue)
}

then change title
in the parent component and then change it in the child component


child components cannot directly modify the prop value passed by the parent component.
there are two solutions:

  1. ide/components-props.html-sharp%E5%8D%95%E5%90%91%E6%95%B0%E6%8D%AE%E6%B5%81" rel=" nofollow noreferrer "> single data flow
  2. ide/components.html-sharp%E9%80%9A%E8%BF%87%E4%BA%8B%E4%BB%B6%E5%90%91%E7%88%B6%E7%BA%A7%E7%BB%84%E4%BB%B6%E5%8F%91%E9%80%81%E6%B6%88%E6%81%AF" rel=" nofollow noreferrer "> parent-child component event communication

subcomponents do not modify props data. Because props is passed down from the parent component, you change it in the child component, and then if the value of the parent component also changes, then this property should take the value changed by the parent component or the value changed by the child component itself. Recommended practice: save the props value to data

export default {
        name: 'index',
        props: {          
            Title:{
                type:String
            }
        },
        data() {
            return {
                myTitle:this.Title
            }
        },
        methods: {
            changeTitle:function () {
                this.myTitle="'
            }
        },
    }
</script>

report this error should be that you cannot directly change the value passed by the parent to the child component title, if you want to change this value, it should be passed through the event, passed to the parent, the parent to change the title, and then the child component changes to maintain the one-way flow of data.
ide/components.html-sharp%E4%BD%BF%E7%94%A8-events-%E5%90%91%E7%88%B6%E7%BB%84%E4%BB%B6%E5%8F%91%E9%80%81%E6%B6%88%E6%81%AF" rel=" nofollow noreferrer "> https://vuefe.cn/v2/guide/com.

Menu