gpt4 book ai didi

javascript - 使用铁路由器后 meteor 模板助手不工作

转载 作者:行者123 更新时间:2023-11-30 16:19:01 26 4
gpt4 key购买 nike

我是 Meteor 的新手,我刚刚在我的项目中安装了 iron router。我的 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 block 不起作用。

在我开始使用 iron router 之前它工作正常。

我在这里错过了什么?

注意,这是我的项目结构树:

├── 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');
});

同时检查浏览器中的控制台,它可能有一些来自 Iron Router 的错误

关于javascript - 使用铁路由器后 meteor 模板助手不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35031179/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com