Meteor 1.2.1 版本 服务器代码中的 Meteor.method({}) 不起作用

Meteor 1.2.1 version Meteor.method({}) inside server code does not work

本文关键字:Meteor 不起作用 method 版本 服务器 代码      更新时间:2023-09-26

客户端单击sendlogmessage调用事件,但服务器端程序内的方法不调用sendlogmessage

console.log没有用。谁能帮我找到答案?

if (Meteor.isClient) {
    Template.mytemplate.events({
        "click": function () {
            Meteor.call('sendLogMessage');
        }
    })
}    
if (Meteor.isServer) {
    Meteor.methods({
        'sendLogMessage': function(){
            console.log("Hello world");
        }
    });
}

你的代码正在工作。您只需要指定要单击的应触发事件的元素。像这样:

HTML
<template name="mytemplate">
    <p id="clicable">Click me!</p>
</template>
JAVASCRIPT
Template.mytemplate.events({
    "click #clicable": function() {
        Meteor.call('sendLogMessage');
    }
})

HTML
<template name="mytemplate">
    <p class="clicable">Click me!</p>
</template>
JAVASCRIPT
Template.mytemplate.events({
    "click .clicable": function() {
        Meteor.call('sendLogMessage');
    }
})