ref communication

<input ref="mytext"/> 
this.$refs.mytext
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Examples</title>
</head>
<body>
    <div id="box">
        <input type="text" ref="mytext">
        <button @click="handleAdd">add</button>
        <child ref="mychild"></child>
    </div>

    <script src="http://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script type="text/javascript">
        //子组件
        Vue.component("child", {
            template: `<div>
            child  
          </div>`,

            data() {
                return {
                    childname: "子组件的状态"
                }
            },
            methods: {
                add(data) {
                    console.log("子组件的方法", data)
                }
            }
        })

        var vm = new Vue({
            el: "#box",
            data: {

            },
            methods: {
                handleAdd() {
                    console.log("1111", this.$refs.mychild.childname)
                    this.$refs.mychild.add("孩子听话");
                }
            },
        })
        /*
          1. ref放在标签上, 拿到的是原生节点
          2. ref放在组件上, 拿到的是组件对象
        */
    </script>
</body>
</html>
Published 20 original articles · won praise 0 · Views 92

Guess you like

Origin blog.csdn.net/qq_46606159/article/details/105030747