gpt4 book ai didi

node.js - "Cannot read property ' MDCSwitch ' of undefined"导入MDC Switch组件时出错

转载 作者:行者123 更新时间:2023-12-05 08:26:12 36 4
gpt4 key购买 nike

我正在使用 express 和 material-components-web 编写一个 Node 应用程序,我遇到了一个问题,TextField 工作但 Switch 不工作。

这是我用来将相关 JS 附加到元素的代码:

  [].slice.call(document.querySelectorAll('.mdc-text-field')).forEach(
function(ele) {
mdc.textField.MDCTextField.attachTo(ele);
});

[].slice.call(document.querySelectorAll('.mdc-switch')).forEach(
function(ele) {
mdc.switch.MDCSwitch.attachTo(ele);
});

当我注释掉文本字段的代码时,它停止工作,所以 mdc.textField.MDCTextField 部分肯定可以工作。

我在 Chrome 开发工具中得到的错误是:

(index):446 Uncaught TypeError: Cannot read property 'MDCSwitch' of undefined
at (index):446
at Array.forEach (<anonymous>)
at (index):444

我的 package.json 文件如下所示:

{
"name": "emergencyregister",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"debug": "~2.6.9",
"ejs": "^2.6.2",
"eslint": "^5.7.0",
"express": "^4.16.4",
"helmet": "^3.20.0",
"http-errors": "~1.6.2",
"material-components-web": "^3.1.0",
"mysql": "^2.16.0",
"node-sass": "^4.12.0",
"node-sass-middleware": "^0.11.0",
"serve-favicon": "^2.5.0"
},
"devDependencies": {}
}

有什么想法吗?

最佳答案

原来我应该使用的代码是:

  [].slice.call(document.querySelectorAll('.mdc-switch')).forEach(
function(ele) {
mdc.switchControl.MDCSwitch.attachTo(ele);
});

为了解决这个问题,我将 mdc 对象打印到控制台以查找所有不同的项目:

enter image description here

关于node.js - "Cannot read property ' MDCSwitch ' of undefined"导入MDC Switch组件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57447776/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com