Javascript 无法从dojo中的模板中的按钮获取事件

Javascript 无法从dojo中的模板中的按钮获取事件,javascript,dojo,Javascript,Dojo,我有一个带有按钮的非常简单的模板,我在js文件中使用该模板,但我无法获取事件,在控制台上,我能够“从1”获取第一个控制台输出,但无法获取其他两个控制台输出,控制台显示以下错误: "TypeError: undefined is not a function↵ at declare.postCreate (http://localhost:8383/mysite/pages/pathsubscriber/widget/subscribersPage.js:30:29) 这是我的Subsc

我有一个带有按钮的非常简单的模板,我在js文件中使用该模板,但我无法获取事件,在控制台上,我能够“从1”获取第一个控制台输出,但无法获取其他两个控制台输出,控制台显示以下错误:

"TypeError: undefined is not a function↵    at declare.postCreate (http://localhost:8383/mysite/pages/pathsubscriber/widget/subscribersPage.js:30:29) 
这是我的SubscriberPage.js:

define([
    "dojo/_base/declare",
    "dijit/_WidgetBase",
    "dijit/_OnDijitClickMixin",
    "dijit/_TemplatedMixin",
    "dojo/text!./templates/subscribersPage.html",
    "dijit/form/Form",
    "dijit/form/TextBox",
    "dijit/form/Button",
    "dojo/on",
    "dojo/dom",
    "dojo/_base/lang"
], function( declare, _WidgetBase, _OnDijitClickMixin, _TemplatedMixin,
        template, form, textbox, button, on, dom, lang) {

    console.log("from 1");
    return declare([_WidgetBase, _OnDijitClickMixin, _TemplatedMixin
    ], {
        templateString: template,
        postCreate: function() {
            this.inherited(arguments);

            this.formButton.on('click', lang.hitch(this, function(event) {
                console.log("form 2");
            }));

            on(this.formButton, 'click', lang.hitch(this, function(event) {
                console.log("from 3");
            }));

        }

    });
});
这是我的模板:

<div>

    <input type="text" name="field1" data-dojo-type="dijit/form/TextBox" />

      <input type="password" name="field2" data-dojo-type="dijit/form/TextBox" />

  <button  type="button"  data-dojo-attach-point = "formButton" data-dojo-type="dijit/form/Button" >Login</button>

   </div> 

登录

我是dojo新手,从一天开始我就无法解决问题。

当您在自己的小部件中使用小部件时,您还应该继承dijit/\u WidgetsInTemplateMixin

此外,您不应该使用
dojo/on
模块来注册小部件事件处理程序,而应该使用小部件上应有的
on()
函数(只要您使用
dijit/\u WidgetsInTemplateMixin
模块),例如:

this.formButton.on("click", lang.hitch(this, function(event) {
    console.log("form 2");
}));
<button  type="button" data-dojo-attach-point="formButton" data-dojo-type="dijit/form/Button" data-dojo-attach-event="onClick:myFunction">Login</button>
甚至更短的是使用
数据dojo attach event
属性,例如:

this.formButton.on("click", lang.hitch(this, function(event) {
    console.log("form 2");
}));
<button  type="button" data-dojo-attach-point="formButton" data-dojo-type="dijit/form/Button" data-dojo-attach-event="onClick:myFunction">Login</button>
登录

在这种情况下,当您单击按钮时,小部件中的
myFunction()
函数将被调用。

我认为您需要将
\u WidgetsInTemplateMixin
类添加到js文件
define()
调用,如
define(…,“dijit/\u TemplatedMixin”,“dijit/\u WidgetsInTemplateMixin”,…)
。原因是您的模板包含dojo小部件。我对dojo也是新手。文件不够精确和清晰。希望它能帮上忙我错过了dijit/_WidgetsTemplateMixin,谢谢你指出这一点。