繁体   English   中英

webpack-pug js function 不是 function

[英]webpack-pug js function is not a function

我是 webpack 和哈巴狗的新手。 做一个小任务和

  • 我在 drop.js 中写了一个 function dropDown() 将它导出到 index.js 文件中,
  • 试图把它放在 PUG 文件中,但是:
  • 控制台写“未定义函数”或“函数不是函数”。
  • 请任何人都可以通过正确定义js function来帮助我解决这个问题

这是我的 webpack.config 的链接 在此处输入链接描述

这是我的 json 文件在此处输入链接描述

在 PUG 文件中,我像这样使用 function:

 .searchbox-drop
    button(href="#" data-dropdown='drop1' onclick='dropDown()' aria-controls='drop1' aria-expanded=false class='dropbtn') Вce
      +image('triangle','searchbox-drop__icon' )  

在 index.js 中

import $ from "jquery";
import 'bootstrap';
import './styles/index.scss';
import {dropDown} from './drop.js';





window.dropDown = dropDown();

在 drop.js 中

export function dropDown(){ 
function show() {
  document.getElementById('myDropdown').classList.toggle('show');
}

//close dropdown id the user cliks outside of it
window.onclick = function(e){
if(!e.target.matches('.dropbtn')){
    var myDropdown = document.getElementById('myDropdown');
    if(myDropdown.classList.contains('show')){
      myDropdown.classList.remove('show');
    }
  }
}

}

这是 CONFIG 文件中 PUG 插件的一部分:

new HtmlWebpackPlugin({
filename: 'index.pug' ,
minify: false,
scriptloading:'blocking',
inject:'body'
}),
new HtmlWebpackPugPlugin()

这是 index.pug

include pug/libs/_libs
include pug/_mixins

doctype html
html(lang='en')
include pug/_head
body
include pug/_header
block content

我不知道我做了什么,但现在我什至在单击 btn 时出现此错误,在此处输入图像描述

尝试分配 function 而不调用它。

window.dropDown = dropDown;

感谢 Steven,我终于做了一些改变来解决问题。 在 index.js 中是这样的:

import $ from "jquery";
import 'bootstrap';
import './styles/index.scss';
import {show} from './drop.js';





window.show= show;
window.onclick = function(e){
if(!e.target.matches('.dropbtn')){
    var myDropdown = document.getElementById('myDropdown');
    if(myDropdown.classList.contains('show')){
      myDropdown.classList.remove('show');
    }
  }
} 

在 index.pug 中:

 onclick = 'show()'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM