异步函数回调中的异常:TypeError:window.scrollTo不是函数(Meteor应用程序)

时间:2016-02-25 00:24:04

标签: javascript meteor

我正在尝试使用scrollTo(0,0)运行Meteor.js应用程序,以使我的每条铁路线都显示在页面顶部:

的客户机/ LIB / router.js:

    Router.route('/services', {
        name: 'services',
        template: 'services',
      onAfterAction: function () {
        scrollTop();
      }  
    });

Router.route('/inquiry', function() {
    this.layout('inquiry');
    onAfterAction: function () {
      scrollTop();
  } 
});

    function scrollTop() {
        window.scrollTo(0, 0);
    }

控制台错误:

meteor.js:880 Exception in callback of async function: TypeError: window.scrollTo is not a function
    at scrollTop (http://localhost:3000/app/client/lib/router.js?942b5705d17b5d736fe545b9dd17f3ea42238776:45:12)
    at Router.route.onAfterAction (http://localhost:3000/app/client/lib/router.js?942b5705d17b5d736fe545b9dd17f3ea42238776:17:9)
    at RouteController.runHooks (http://localhost:3000/packages/iron_router.js?c564289eeaa191561eba900052037432ebfcbe4a:265:7)
    at RouteController._runRoute (http://localhost:3000/packages/iron_router.js?c564289eeaa191561eba900052037432ebfcbe4a:551:8)
    at Function.Route.dispatch (http://localhost:3000/packages/iron_router.js?c564289eeaa191561eba900052037432ebfcbe4a:848:18)
    at route (http://localhost:3000/packages/iron_router.js?c564289eeaa191561eba900052037432ebfcbe4a:705:11)
    at boundNext (http://localhost:3000/packages/iron_middleware-stack.js?3370bd57ef7b310cca3f5dddb11b77fafdcfc1eb:418:31)
    at http://localhost:3000/packages/meteor.js?9730f4ff059088b3f7f14c0672d155218a1802d4:999:22
    at dispatch (http://localhost:3000/packages/iron_middleware-stack.js?3370bd57ef7b310cca3f5dddb11b77fafdcfc1eb:442:3)
    at http://localhost:3000/packages/iron_router.js?c564289eeaa191561eba900052037432ebfcbe4a:385:13

3 个答案:

答案 0 :(得分:2)

您的第二条路线不正确,您已将功能样式设置与i-r的对象样式混合。

Router.route('/inquiry', function() {
    this.layout('inquiry');
    onAfterAction: function () {
      scrollTop();
  } 
});

应该是:

Router.route('/inquiry', {
  template: 'inquiry',
  onAfterAction: function () {
    scrollTop();
  }
});

答案 1 :(得分:0)

除了像@Michel这样的语法错误,你可能需要

onAfterAction: function () {
    if (this.ready()) {
        scrollTop();
    }
}

确保呈现模板

答案 2 :(得分:0)

当我使用.scroll(0,0)而不是.scrollTo(0,0)时,这个错误似乎消失了。我不确定为什么会这样,但错误消失了,功能就在那里使用滚动