gpt4 book ai didi

javascript - 为什么没有定义 meteor 模板对象?

转载 作者:数据小太阳 更新时间:2023-10-29 04:38:33 26 4
gpt4 key购买 nike

我一直在按照教程制作一个简单的论坛,最终将所有代码放在一起后,它告诉我“模板未定义”

forum.html代码

<head>
<title>Forum</title>
</head>
<body>
{{> form}}
{{> posts}}
</body>

<template name="posts">
<h1>Posts</h1>
<ul>
{{#each posts}}
<li>
<h3>{{title}}</h3>
<p>{{body}}</p>
</li>
{{/each}}
</ul>
</template>


<template name="form">
<form>
<label>Post Title:
<input type="text" id="title" />
</label>
<label>Post Body:
<textarea id="body"></textarea>
</label>
<input type="submit" value="Submit" id="submit"/>
</form>
</template>

forum.js代码:

var Posts = new Meteor.Collection('posts');
if (Meteor.isClient) {
Template.posts.helpers({
Posts: function() {
return Posts.find();
}
});
}

Template.form.events = {
'click #submit': function(event){
event.preventDefault();
var title = $('#title').val();
var body = $('#body').val();
Posts.insert({
title: title,
body: body
});
$('#title, #body').val('');
}
};

这是我从 meteor 得到的一些输出

W20150211-02:01:42.086(0)? (STDERR)           
W20150211-02:01:42.088(0)? (STDERR) /home/ubuntu/.meteor/packages/meteor-tool/.1.0.40.1ef5dzv++os.linux.x86_64+web.browser+web.cordova/meteor-tool-os.linux.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:173
W20150211-02:01:42.088(0)? (STDERR) throw(ex);
W20150211-02:01:42.088(0)? (STDERR) ^
W20150211-02:01:42.091(0)? (STDERR) ReferenceError: Template is not defined
W20150211-02:01:42.091(0)? (STDERR) at app/forum.js:10:1
W20150211-02:01:42.091(0)? (STDERR) at app/forum.js:23:3
W20150211-02:01:42.091(0)? (STDERR) at /home/ubuntu/workspace/forum/.meteor/local/build/programs/server/boot.js:205:10
W20150211-02:01:42.092(0)? (STDERR) at Array.forEach (native)
W20150211-02:01:42.092(0)? (STDERR) at Function._.each._.forEach (/home/ubuntu/.meteor/packages/meteor-tool/.1.0.40.1ef5dzv++os.linux.x86_64+web.browser+web.cordova/meteor-tool-os.linux.x86_64/dev_bundle/server-lib/node_modules/underscore/underscore.js:79:11)
W20150211-02:01:42.092(0)? (STDERR) at /home/ubuntu/workspace/forum/.meteor/local/build/programs/server/boot.js:116:5
=> Exited with code: 8
=> Your application is crashing. Waiting for file change.

最佳答案

您的代码有 2 个问题:

  • 模板定义在服务器上不可用,因此您需要将 Template.form 定义包装在 Meteor.isClient 条件中,或者更好的是,将您的使用 clientserver 目录的代码。

  • 正确的事件映射定义需要使用此语法:Template.form.events({...}); 而不是 Template.form.events={... };

关于javascript - 为什么没有定义 meteor 模板对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28445385/

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