javascript 是什么导致 Meteor 中的“模板未定义”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10442419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What causes "Template is not defined" in Meteor?
提问by mwcz
This extremely simple Meteor app is throwing a Template is not defined
error on load. The app is essentially identical to the boilerplate project (meteor create
), just split into server/client/public directories.
这个极其简单的 Meteor 应用程序Template is not defined
在加载时抛出错误。该应用程序本质上与样板项目 ( meteor create
)相同,只是拆分为 server/client/public 目录。
Meteor seems to be trying to render the Handlebars template
tags before the global Template object is actually ready. By the time I can get to the JS console and type "Template", it is there.
Meteor 似乎试图template
在全局模板对象实际准备好之前呈现 Handlebars标签。当我可以进入 JS 控制台并输入“模板”时,它就在那里。
Have I done something wrong, or is this a timing bug?
我做错了什么,还是这是一个计时错误?
采纳答案by randompast
Hm, perhaps this will solve your issue:
嗯,也许这会解决你的问题:
Note that the body tag includes the template name but not the template:
请注意,body 标签包含模板名称,但不包含模板:
<body>
{{> hello}}
</body>
<template name="hello">
{{greet}}
</template>
Also, note that ".greet" refers to {{greet}}:
另外,请注意“.greet”指的是{{greet}}:
if (Meteor.isClient) {
Template.hello.greet = function () {
return "Hey!";
};
}
So, the issue was that you can't have a template inside the body. Instead, the body calls the template with {{> hello}} as in the above code.
所以,问题是你不能在体内有一个模板。相反,主体像上面的代码一样使用 {{> hello}} 调用模板。
回答by KJW
You need to make sure in your .js file which calls the Template is wrapped in if (Meteor.isClient){}
, otherwise the Template
global var won't be available for some reason.
您需要确保在调用 Template 的 .js 文件中包含在 中if (Meteor.isClient){}
,否则由于Template
某种原因全局变量将不可用。
回答by Joe
If this in a package make sure you have templating in your api use list ie
如果这在一个包中,请确保您的 api 使用列表中有模板,即
api.use('templating', 'client');
api.use('模板', '客户端');
That ensures your code is run once the Template object is instantiated.
这可确保在实例化 Template 对象后运行您的代码。
回答by ivac
Try Template.hello.this to pass the data to {{this}}
尝试 Template.hello.this 将数据传递给 {{this}}
回答by Vinaya Kumar Thimmappa
This is an initialisation problem. I am using Meteor 1.0 and I solved the problem by adding
Meteor.startup(function () {}
or an if
block to Meteor.isClient
.
这是一个初始化问题。我使用流星1.0和我解决通过将问题
Meteor.startup(function () {}
或if
块Meteor.isClient
。
This may be a bug, because the documentation on special directories says as below (as of today):
这可能是一个错误,因为特殊目录的文档如下(截至今天):
Client: Any directory named client is not loaded on the server. Similar to wrapping your code in if (Meteor.isClient) { ... }. All files loaded on the client are automatically concatenated and minified when in production mode. In development mode, each file is sent individually for easier debugging. HTML files in a Meteor application are treated quite a bit differently from a server-side framework. Meteor scans all the HTML files in your directory for three top-level elements:
<head>
,<body>
, and<template>
. The head and body sections are separately concatenated into a single head and body, which are transmitted to the client on initial page load.
客户端:任何名为客户端的目录都不会加载到服务器上。类似于将您的代码包装在 if (Meteor.isClient) { ... } 中。在生产模式下,客户端上加载的所有文件都会自动连接和缩小。在开发模式下,每个文件都是单独发送的,以便于调试。Meteor 应用程序中的 HTML 文件与服务器端框架的处理方式有很大不同。流星扫描目录中的所有HTML文件三个顶级元素:
<head>
,<body>
,和<template>
。head 和 body 部分分别连接成一个单独的 head 和 body,它们在初始页面加载时传输到客户端。
But without initialisation, this fails with a "Template not found error".
但是如果没有初始化,这会因“找不到模板错误”而失败。