流星模板助手在使用铁路由器后不起作用
Meteor template helper not working after using iron router
我是Meteor的新手,我刚刚在我的项目中安装了铁路由器。我的应用程序.js文件中有这个:
if (Meteor.isClient) {
Template.nav.helpers({
isMobile: function(){
if(isMobile.tablet) {
return false;
} else if(isMobile.phone) {
return true;
} else {
return false;
}
}
});
}
isMobile
助手将在我的nav
模板中使用,如下所示:
<template name="nav">
...
{{#if isMobile}}
{{> mobile_nav}}
{{else}}
{{> desktop_nav}}
{{/if}}
...
</template>
我在这里做的是,我正在加载 2 组不同的导航。如果用户使用的是台式机或平板电脑,我将显示desktop_nav
模板,当用户使用手机时,我将显示mobile_nav
。
这是我在isMobile
模板助手中使用的jQuery插件。
在我的应用程序.html文件中,我有这个:
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
<title>Example</title>
</head>
<body>
</body>
<template name="main">
{{> nav}}
{{> yield}}
{{> footer}}
</template>
这是我的路由器.js文件:
Router.configure({
layoutTemplate: 'main'
});
Router.route('/', 'home');
Router.route('/about', 'about');
我现在的问题是,isMobile
模板助手不起作用。nav
模板已加载,但if else
块不起作用。
在我开始使用铁路由器之前,它工作正常。
我在这里错过了什么?
注意,这是我的项目结构树:
├── application.css.scss
├── application.html
├── application.js
├── client
│ ├── javascripts
│ │ ├── isMobile.min.js
│ │ └── router.js
│ ├── stylesheets
│ │ └── ...
│ └── views
│ ├── about.html
│ ├── home.html
│ ├── home.js
│ └── layouts
│ ├── desktop_nav.html
│ ├── footer.html
│ ├── mobile_nav.html
│ └── nav.html
└── public
├── fonts
│ └── ...
└── images
└── ...
您应该将模板呈现到布局中。这是一个很好的教程,可以帮助你。
http://iron-meteor.github.io/iron-router/#rendering-templates-into-regions-with-javascript
所以像这样:
Router.route('/', function () {
this.render('main');
});
还要检查浏览器中的控制台,它可能有一些来自铁路由器的错误
问题解决了。这是由于我没有初始化基金会。添加后,我的菜单工作正常:
Template.desktop_nav.onRendered(function(){
$(document).foundation();
});
Template.mobile_nav.onRendered(function(){
$(document).foundation();
});
要记住的另一件重要事情是,$(this).foundation();
不起作用。只有$(document).foundation();
有效。我不知道在这种情况下$(this)
和$(document)
有什么区别。
感谢维勒姆和3thanZ的帮助。我真的很感激!
可能是 isMobile
jQuery 插件干扰了您的isMobile
助手函数吗?您是否尝试将isMobile
帮助程序函数重命名为其他名称?也许从您的帮助程序函数中执行console.log(isMobile.tablet, isMobile.phone)
以确保它被调用?
- UI 路由器参数 URL 不起作用,只是重定向到 /
- Angular 1.3+ui路由器+生成器ng poly嵌入嵌套(?)视图不起作用
- 角度 UI 路由器嵌套视图不起作用
- AngularJS和UI路由器:为什么$state.go()和$location.path()在交互式控制台中不起作用
- AngularJS路由器代码不起作用
- MeteorJS包装异步在铁路由器内不起作用
- 我的 ui 路由器链接不起作用,角度
- 流星模板助手在使用铁路由器后不起作用
- 反应路由器 v2.0 浏览器历史记录不起作用
- UI路由器状态的多个视图不起作用.未报告错误
- react路由器尾部斜线不起作用
- 主路由器在路由“”的末尾附加一个#"导致dom中引用锚点在Backbone/Require应用程序中不起作用
- Angular ui路由器在服务中保存到状态名称不起作用
- Angular ui路由器-重定向不'根本不起作用
- 反应路由器是活动方法不起作用
- 角度 UI 路由器 - 状态查询字符串参数不起作用
- 基本的React路由器示例不起作用
- Angular UI路由器使用go函数在状态之间传递数据不起作用
- Angularjs的嵌套路由器不起作用
- ExpressJS >路由器中间件>通过重定向传递变量 req.foo 不起作用