ref指令

1、案例1

<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>ref指令</title>
</head>

<body>
    <div id="app">
        <button @click="showText">按钮</button>
        <p ref="hello">{{msg}}</p>
    </div>
    <script src="../js/vue.js" type="text/javascript"></script>
    <script>
        const vm = new Vue({
            el: "#app",
            data: {
                msg: "你好吗"
            },
            methods: {
                showText() {
                    //this.$refs.hello 获取p标签
                    //alert p标签的内容
                    alert(this.$refs.hello.textContent);
                }
            }
        });
    </script>
</body>

</html>
原文地址:https://www.cnblogs.com/liuyang-520/p/12491741.html