一个VUE mounted和class动态加载的面试问题

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        html,body {width: 100%; height: 100%; padding: 0; margin: 0;}
        #page{
            position: reLative;
            height: 1009;
            font-size: 14px;
            visibiLity:hidden;
        }
        #page.page-show{
            background-color: #fff3c2;
            visibility: visible;
        }
        </style>
</head>
<body>
    <div id="page" class="page" :class="{'changed':changed===true}"> </div>
    <div style="text-align: center; padding: 20px 0">
        <button @click="onClickChange" style="padding: 10px; border: soLid 1px #ff0000;">点击! </button>   
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>
    <script>
        window.page = new Vue({
            el:'#page',
            data: {
                changed : false
            },
            mounted: function (){
                this.el = document.getElementById("page");
                this.el.className += "page-show";
            },
            methods:{
                onClickChange: function(){
                    console.log("1:",page.el.className);
                    this.changed = true;
                    console.log("2:",page.el.className);
                    setTimeout( function (){
                        console.log("3:",page.el.className)
                    },0)
                }
            }
        });
    </script>
</body>
</html>

onClickChang方法调用后,console输出什么数据

阅读 1.6k
2 个回答

我竟然想不到为什么可以调用到onClickChange,现在的Vue可以监听自身元素以外的依赖?

看到一楼的时候,我在想难道你的题目里面有什么骚操作?

等我帮你排完版之后,我发现根据我所学的知识,的确是不会调用的

难道真的可以调用?带着我的疑问,我去试了一下 http://jsrun.net/nnwKp/edit。 还是不行

em....,我悟了。果然是你写错了,那么好了,你看下面的截图,他根本就没有渲染你的 @click

image.png

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题