是什么导致Meteor中的“模板未定义”?

时间:2021-05-14 18:50:02

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应用程序正在抛出一个模板未加载时定义的错误。该应用程序与样板项目(meteor create)基本相同,只是拆分为服务器/客户端/公共目录。

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?

我做错了什么,或者这是一个时间错误?

5 个解决方案

#1


9  

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}}调用模板,如上面的代码所示。

#2


27  

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.

您需要在.js文件中确保哪些调用模板包含在if(Meteor.isClient){}中,否则模板全局变量由于某种原因将不可用。

#3


5  

If this in a package make sure you have templating in your api use list ie

如果在包中确保你在api使用列表中有模板,即

api.use('templating', 'client');

That ensures your code is run once the Template object is instantiated.

这可确保在实例化Template对象后运行代码。

#4


0  

Try Template.hello.this to pass the data to {{this}}

尝试使用Template.hello.this将数据传递给{{this}}

#5


0  

This is an initialisation problem. I am using Meteor 1.0 and I solved the problem by addingMeteor.startup(function () {} or an if block to Meteor.isClient.

这是一个初始化问题。我正在使用Meteor 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.

客户端:服务器上未加载任何名为client的目录。类似于将代码包装在if(Meteor.isClient){...}中。在生产模式下,客户端上加载的所有文件都会自动连接和缩小。在开发模式下,每个文件都是单独发送的,以便于调试。 Meteor应用程序中的HTML文件与服务器端框架的处理方式略有不同。 Meteor会扫描目录中的所有HTML文件,以获取三个*元素:,和

But without initialisation, this fails with a "Template not found error".

但是如果没有初始化,则会因“找不到模板错误”而失败。

#1


9  

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}}调用模板,如上面的代码所示。

#2


27  

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.

您需要在.js文件中确保哪些调用模板包含在if(Meteor.isClient){}中,否则模板全局变量由于某种原因将不可用。

#3


5  

If this in a package make sure you have templating in your api use list ie

如果在包中确保你在api使用列表中有模板,即

api.use('templating', 'client');

That ensures your code is run once the Template object is instantiated.

这可确保在实例化Template对象后运行代码。

#4


0  

Try Template.hello.this to pass the data to {{this}}

尝试使用Template.hello.this将数据传递给{{this}}

#5


0  

This is an initialisation problem. I am using Meteor 1.0 and I solved the problem by addingMeteor.startup(function () {} or an if block to Meteor.isClient.

这是一个初始化问题。我正在使用Meteor 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.

客户端:服务器上未加载任何名为client的目录。类似于将代码包装在if(Meteor.isClient){...}中。在生产模式下,客户端上加载的所有文件都会自动连接和缩小。在开发模式下,每个文件都是单独发送的,以便于调试。 Meteor应用程序中的HTML文件与服务器端框架的处理方式略有不同。 Meteor会扫描目录中的所有HTML文件,以获取三个*元素:,和

But without initialisation, this fails with a "Template not found error".

但是如果没有初始化,则会因“找不到模板错误”而失败。