Vue子组件没法传值给父组件,一直报错

请教下各位大佬@choose=changeEvent到底哪里定义错了,一直想不通

<!DOCTYPE html>
<html lang = "en" >
    <head >
        <meta charset = "UTF-8" >
        <title >Title</title >
        <style >

        </style >
    </head >
    <body >
        <div id="app">
            <ul>
               <school v-for="item ,index in schoolList" :school-name="item"
                       :key="'avc'+index" :index="index" @choose="changeEvent"> </school>
            </ul>


            <h2>选中的学校是{{chooseSchool}}</h2>
        </div>

        <script src="../js/vue.js" type="text/javascript" charset="utf-8"></script>
        <script type="text/javascript" >
            Vue.component("school",{
                props:["schoolName"],
                template:
               ` <li>
                <h3>学校名称:{{schoolName}}</h3>
                <button v-on:click="chooseEvent(schoolName)" >选择学校</button>
                </li>`,

                methods:{
                    chooseEvent:function (schoolName) {

                        this.$emit('choose',schoolName);

                    },

                },
            });


            const app=new Vue({
                el:"#app",
                data:{
                    chooseSchool:"",
                    schoolList:["清华大学","北京大学","东京大学"],
                    methods:{
                        changeEvent:function (data) {
                            console.log("hahha");
                            this.chooseSchool=data;

                        }
                    }
                }
            })
        </script >
    </body >
</html >

1个回答

methods 写在了data里面,写在外面就好了

 data:{
                    chooseSchool:"",
                    schoolList:["清华大学","北京大学","东京大学"],
                    methods:{
                        changeEvent:function (data) {
                            console.log("hahha");
                            this.chooseSchool=data;

                        }
                    }
                }
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问