CanJS 在组件外部单击影响组件

标签 canjs donejs

我想在<some-component>中添加一个事件监听器对按钮使用react。

<some-component></some-component>
<button class="click">click here</button>

我确信这真的很简单。我对 CanJS 很陌生并且正在研究它。

<can-component tag="some-component">
<style type="less">
    <!-- stuff -->
</style>
<template>
    <!-- stuff -->
</template>
<script type="view-model">
import $ from 'jquery';
import Map from 'can/map/';
import 'can/map/define/';

export default Map.extend({
  define: {
    message: {
      value: 'This is the side-panels component'
    }
  }
});
</script>
</can-component>

我尝试添加 $('body').on('click', '.click', function() {});到该组件,它似乎不起作用。阅读了大量文档,但我仍然缺少一些基本的理解。

更新

我尝试过这个:

<some-component-main>
    <some-component></some-component>
    <button class="click">click here</button>
</some-component-main>

事件监听器位于 some-component-main

events: {
  ".click click": function(){
    console.log("here I am");
  }
},

但这也不起作用。

最佳答案

<some-component-main>
    <some-component></some-component>
    <button class="click">click here</button>
</some-component-main>

在 some-component-main 中使用事件监听器

events: {
  ".click click": function(){
    console.log("here I am");
  }
},

当我意识到以数字结尾的组件会导致其他问题阻碍它时,这确实起作用了。

关于CanJS 在组件外部单击影响组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39295698/

相关文章:

javascript - 如何使用jquery动态添加一行到ejs中的表中?

javascript - can.Construct 和 can.Construct.extend 之间的区别

javascript - CanJS findOne 按名字

javascript - CanJS 表格点击事件

javascript - 如何使用 {can.route} 监听空白路线并使用react?

javascript - jQuery:让第二个函数等待执行,直到第一个函数完成