javascript - v-on :click inside a Vue Component does not work

标签 javascript vue.js vue-component

我的应用程序中有一个简单的 Vue 侧边栏实例,其中列出了侧边栏的所有菜单。我有一个像这样的模板的本地组件。

template:'<div><li class="custom-erp-menu-list" v-on:click="toggleOpenChild">'+
    '<a href="#">'+
        '<span>'+
            '<img src="assets/images/dollar-bills.svg" class="custom-erp-module-list-icon custom-erp-user-icons" width="18" height="18" alt="">'+
        '</span>'+
        '<span class="custom-erp-menu-parent">Purchase Order</span>'+
    '</a>'+
    '<ul class="nav custom-erp-menu-child-dropdown" id="purchase-order-child">'+
        '<li><a href="page-profile.html" class="custom-erp-menu-child">Profile</a></li>'+
        '<li><a href="page-login.html" class="custom-erp-menu-child">Login</a></li>'+
        '<li><a href="page-lockscreen.html" class="custom-erp-menu-child">Lockscreen</a></li>'+
    '</ul>'+
'</li></div>'

有一些奇怪的错误,上面写着

property or method "toggleOpenChild" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property

您可以查看DEMO在这里。

最佳答案

您的 toggleOpenChild 应放置在组件的方法中,如下所示:

components: {
  "side-bar-modules": {
    template:
      '<div><li class="custom-erp-menu-list" v-on:click="toggleOpenChild">' +
      '<a href="#">' +
      "<span>" +
      '<img src="assets/images/dollar-bills.svg" class="custom-erp-module-list-icon custom-erp-user-icons" width="18" height="18" alt="">' +
      "</span>" +
      '<span class="custom-erp-menu-parent">Purchase Order</span>' +
      "</a>" +
      '<ul class="nav custom-erp-menu-child-dropdown" id="purchase-order-child">' +
      '<li><a href="page-profile.html" class="custom-erp-menu-child">Profile</a></li>' +
      '<li><a href="page-login.html" class="custom-erp-menu-child">Login</a></li>' +
      '<li><a href="page-lockscreen.html" class="custom-erp-menu-child">Lockscreen</a></li>' +
      "</ul>" +
      "</li></div>",
    data: function() {
      return {
        user: []
      };
    },
    methods: {
      //function to close/open the child elements
      //when the parent menu is clicked.
      toggleOpenChild: function(event) {
        var currentParent = $(event.currentTarget)
          .find(".custom-erp-menu-parent")
          .text();
        var childListID = currentParent.toLowerCase().replace(/ /g, "-");
        $(".custom-erp-menu-list > ul")
          .not($("#" + childListID + "-child"))
          .slideUp()
          .removeClass("custom-erp-menu-child-open");
        if ($("#" + childListID + "-child").is(":hidden")) {
          $("#" + childListID + "-child")
            .slideDown(300)
            .toggleClass("custom-erp-menu-child-open");
        } else {
          $("#" + childListID + "-child")
            .slideUp(300)
            .toggleClass("custom-erp-menu-child-open");
        }
      }
    }
  }
}

这是更新的 fiddle :

https://jsfiddle.net/cgxnLajf/1/

关于javascript - v-on :click inside a Vue Component does not work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52880755/

相关文章:

vue.js - VueJS 路由器守卫实现访问控制机制

javascript - 当 React.Dispatch<React.SetStateAction<string>> 不被接受时,如何为 setState 函数定义 TypeScript 类型?

javascript - 在按钮内单击子元素时不会触发父 onClick

vue.js - 未应用 Vuetify v-data-table 自定义 header 类样式

javascript - Vue 用 v-if 设置 2 个条件

javascript - Vue - 将 html 音频播放器转换为组件

javascript - 防止 Rails 将特殊字符转换为 HTML 实体

javascript - 离开悬停后如何将 p 标签保持在先前的位置?

javascript - 如何在几分钟/几秒/次后清除JavaScript变量值

服务器上的 Vue.js 空白页面但在本地主机上没问题