[英]Uncaught TypeError: _models_Search__WEBPACK_IMPORTED_MODULE_0___default.a is not a constructor
I am following a tutorial on Javascript- currently building a app which involves uses classes I have encountered an error which is the title of this question 'Uncaught TypeError: _models_Search__WEBPACK_IMPORTED_MODULE_0___default.a is not a constructor' this project involves using the MVC design pattern also.我正在关注关于 Javascript 的教程 - 目前正在构建一个涉及使用类的应用程序我遇到了一个错误,它是这个问题的标题“未捕获的 TypeError:_models_Search__WEBPACK_IMPORTED_MODULE_0___default.a 不是构造函数”这个项目也涉及使用 MVC 设计模式。
I have tried to rename import Search from './models/Search';我试图从'./models/Search'重命名导入搜索; to import {Search} from './models/Search';从'./models/Search'导入{Search}; but still I am being greeted with this full error message:但我仍然收到这个完整的错误消息:
Uncaught TypeError: _models_Search__WEBPACK_IMPORTED_MODULE_0___default.a is not a constructor
at eval (index.js:5)
at Module../src/js/index.js (bundle.js:4198)
at __webpack_require__ (bundle.js:20)
at eval (webpack:///multi_(:8080/webpack)-dev-server/client?:3:18)
at Object.0 (bundle.js:4220)
at __webpack_require__ (bundle.js:20)
at bundle.js:84
at bundle.js:87
Search.js file搜索.js 文件
import axios from 'axios';
export default class Search {
constructor(query) {
this.query = query;
}
async getResults() {
const proxy = 'https://cors-anywhere.herokuapp.com/'; const
key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx';
try {
const res = await
axios(`${proxy}https://www.food2fork.com/api/search?
key=${key}&q=${this.query}`);
this.result = res.data.recipes;
console.log(recipes);
} catch (error) {
alert(error);
}
}
}
index.js file index.js 文件
import Search from './models/Search';
const search = new Search('pizza');
console.log(search);
the expected outcome is for the the search property to be returned in the client and for me to be able to access all of its properties within the browser.预期的结果是在客户端中返回搜索属性,并且我能够在浏览器中访问其所有属性。 and yes I have purposely x'd out the API key just in case you were wondering- would actually help too if the answer is given you can give me a brief breakdown as to where I went wrong.是的,我特意删除了 API 密钥,以防万一您想知道-如果给出答案,实际上也会有所帮助,您可以简要介绍一下我哪里出错了。 Thanks in advance.提前致谢。
To correctly import your Search class use the following lines of code.要正确导入您的搜索 class,请使用以下代码行。 In index.js:在 index.js 中:
import Search from './models/Search';
And in Search class declaration:并在搜索 class 声明:
export default class Search
Or second way that you tried (with braces in import): in index.js或者您尝试的第二种方式(在导入中使用大括号):在 index.js 中
import { Search } from './models/Search';
And export, without default :并导出,没有默认值:
export class Search
Braces needed only in named exports仅在命名导出中需要大括号
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.