gpt4 book ai didi

javascript - 属性未定义

转载 作者:行者123 更新时间:2023-12-02 23:25:42 24 4
gpt4 key购买 nike

我从控制台返回了此错误:

index.js:70 Uncaught TypeError: Cannot read property 'searchForm' of undefined
at eval (index.js:70)
at Module../src/js/index.js (bundle.js:4245)
at __webpack_require__ (bundle.js:20)
at eval (webpack:///multi_(:8080/webpack)-dev-server/client?:3:18)
at Object.0 (bundle.js:4292)
at __webpack_require__ (bundle.js:20)
at bundle.js:84
at bundle.js:87
(anonymous) @ index.js:70
./src/js/index.js @ bundle.js:4245
__webpack_require__ @ bundle.js:20
(anonymous) @ client:3
0 @ bundle.js:4292
__webpack_require__ @ bundle.js:20
(anonymous) @ bundle.js:84
(anonymous) @ bundle.js:87
mixpanel-2-latest.min.js:88 document not ready yet, trying again in 500 milliseconds...
client:52 [WDS] Live Reloading enabled.
client:126 [WDS] Warnings while compiling.
warnings @ client:126
(anonymous) @ socket.js:47
sock.onmessage @ SockJSClient.js:58
EventTarget.dispatchEvent @ sockjs.js:170
(anonymous) @ sockjs.js:887
SockJS._transportMessage @ sockjs.js:885
EventEmitter.emit @ sockjs.js:86
WebSocketTransport.ws.onmessage @ sockjs.js:2961
client:135 ./src/js/views/searchView.js 6:9-17
"export 'elements' was not found in './base'
warnings @ client:135
(anonymous) @ socket.js:47
sock.onmessage @ SockJSClient.js:58
EventTarget.dispatchEvent @ sockjs.js:170
(anonymous) @ sockjs.js:887
SockJS._transportMessage @ sockjs.js:885
EventEmitter.emit @ sockjs.js:86
WebSocketTransport.ws.onmessage @ sockjs.js:2961
client:135 ./src/js/index.js 66:0-8
"export 'elements' was not found in './views/base'

我通过 Babel 追踪到 ES6 --> ES5 转换中的错误:

_views_base__WEBPACK_IMPORTED_MODULE_2__["elements"].searchForm.addEventListener('submit', function (e) {

索引.js:

// always make sure you have the right directory 

// import field
import Search from './models/Search';
// import all the function from the view
import * as searchView from './views/searchView'
import {elements} from './views/base';

/* Global state of the app
- Search obj
- current recipe obj
- shopping list object
- liked recipes
*/

// everytime we reload the app, it will be empty
const state = {}
const controlSearch = async () =>{
// 1) Get the query from the view
const query = searchView.getInput;
console.log(query);

if(query){
// 2) new search object and add it to state
state.search = new Search(query); // new instance of the search class

// 3) prepare UI for results

// 4) Search for recipes
await state.search.getResults();

// 5) render result in the UI, reminder u got hit the search button
console.log(state.search.result);

}
}
elements.searchForm.addEventListener('submit', e =>{
e.preventDefault();
controlSearch();
})

const search = new Search('pizza');
console.log(search);
search.getResults();

searchView.js:

// if we are in the current folder then it is simply base 
import {elements} from './base';
// return the input value from the field
// implicit search
export const getInput =() => elements.searchInput.value;

base.js:

// all the DOM element will be in this class 
export const element = {
searchForm: document.querySelector('.search'),
searchInput: document.querySelector('.search__field')
}

追踪错误,我认为一切都已定义,但我似乎不明白这个错误是如何发生的。我是网络开发新手,我希望这不是一个坏问题。我真的需要一个更有经验的头脑来看待这个错误。多谢。

最佳答案

在你的base.js中......你正在导出元素

export const element

在你的index.js中......你正在导入元素

import {elements} from './views/base'; 

将“元素”更改为“元素”,反之亦然

关于javascript - 属性未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56744363/

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