【发布时间】:2016-05-04 00:57:01
【问题描述】:
我是 Meteor 的新手,我刚刚在我的项目中安装了 Iron 路由器。我的 application.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。
This is the jQuery plugin,我在 isMobile 模板助手中使用。
在我的 application.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>
这是我的 router.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
└── ...
【问题讨论】:
-
将 console.log(isMobile) 作为 isMobile 辅助函数的第一行。如果它不是“未定义的”,那么 isMobile jQuery 插件就可以工作。我试过了,它应该可以工作,所以我的猜测是其他问题。要获得具体答案,需要您的示例项目。
标签: javascript jquery templates meteor meteor-helper