账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    vue router局部组件模拟单页面切换过程,组件中的事件方法无法销毁问题
    • ༺ۣۖꪑۣۖ༒ۣꫀۣۖ༻2018-09-27 00:00
    • 11
    44
    0
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>无标题文档</title>
    </head>
    
    <body style="height:2000px">
        <div id="app">
            <router-view></router-view>
        </div>
        <script src="vue.js"></script>
        <script src="vue-router.js"></script>
        <script>
            var componentA = {
                template: `<div>hello world</div>`,
                methods: {
                    change: function() {
                        console.log( "componentA" );    
                    }    
                },
                created: function() {
                    var _this = this;
                    window.addEventListener( "scroll", function() {
                        _this.change();        
                    }, false );
                }
            };
            var componentB = {
                template: `<div>hello life</div>`,
                methods: {
                    change: function() {
                        console.log( "componentB" );    
                    }    
                },
                created: function() {
                    var _this = this;
                    window.addEventListener( "scroll", function() {
                        _this.change();        
                    }, false );    
                }    
            };
            var vm = new Vue( {
                el: "#app",
                router: new VueRouter( {
                    routes: [
                        { path: '/a', component: componentA },
                          { path: '/b', component: componentB }
                    ]    
                } )    
            } );
        </script>
    </body>
    </html>
    

    最近遇到一个类似上面例子的问题,这是一个简单模拟单页面切换的例子,但是会发现在切换过程中,随之鼠标的滚动,两个局部组件的方法都会被触发。这种情况该如何解决?以到达对应的页面,只触发对应组件的方法。路过的大神麻烦看下,谢谢

    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    • 从诂 普通会员 1楼
      502 Bad Gateway

      502 Bad Gateway


      nginx
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部