[英]Webpack compile error: TypeError: __WEBPACK_IMPORTED_MODULE_1__ … is not a function
So, this my Api Service Component, I'm using Axios:所以,这是我的 Api 服务组件,我使用的是 Axios:
import api from './Api.vue';
export default {
name: 'app-feed-service',
methods: {
getPosts() {
return api.get('posts/');
}
}
}
and some feed component和一些饲料成分
import AppSinglePost from './../Feed/Post.vue';
import AppFeedService from './../../api/Feed.vue';
export default {
name: 'app-posts',
components: {
AppSinglePost
},
data() {
return {
posts: []
}
},
created() {
AppFeedService.getPosts().then((res) => {
console.log(res);
});
}
}
and now the error:现在的错误:
TypeError: __WEBPACK_IMPORTED_MODULE_1__api_Feed_vue___default.a.getPosts is not a function
can anybody help?有人可以帮忙吗?
It looks like AppFeedService as defined in Feed.vue is not really a component, it's just a collection of services you want to call.看起来 Feed.vue 中定义的 AppFeedService 并不是真正的组件,它只是您要调用的服务的集合。 Since you have defined it as a component, the component would have to be instantiated somewhere, which in most cases would mean you used it in another component's template
.由于您已将其定义为组件,因此必须在某处实例化该组件,这在大多数情况下意味着您在另一个组件的template
使用了它。
You can just define it as an object instead.您可以将其定义为一个对象。
import api from './Api.js';
export default {
getPosts() {
return api.get('posts/');
}
}
Same thing for your Api.vue file likely.您的 Api.vue 文件可能也是如此。 You only need to use a .vue
file when you are defining an actual component.你只需要在定义实际组件时使用.vue
文件。
Then in your feed component just然后在您的提要组件中
import AppFeedService from './../../api/Feed.js';
To summarize: the .vue
file format is meant for defining single file components .总结一下: .vue
文件格式用于定义单个文件组件。 You only need a .vue
file when you are actually defining a single file component (something that would probably be used in the template
of a different component).当您实际定义单个文件组件(可能会在不同组件的template
中使用的内容)时,您只需要一个.vue
文件。 If you just want an object that contains a collection of methods or some state, just define it with plain javascript.如果您只想要一个包含一组方法或某种状态的对象,只需使用普通的 javascript 定义它。
I was getting the exact same error as Webpack compile error: TypeError: WEBPACK_IMPORTED_MODULE_1 … is not a function in ReactJS.我得到了与Webpack 编译错误完全相同的错误:TypeError: WEBPACK_IMPORTED_MODULE_1 ... is not a function in ReactJS。 This type of error you get usually when you are importing any defined variable/ component from any other file as Component.当您从任何其他文件导入任何定义的变量/组件时,通常会遇到这种类型的错误。 In react we import component as在反应中,我们将组件导入为
import ComponentName from '/componentPath';
so if it isn't a component we are importing then try using importing as an Object所以如果它不是我们要导入的组件,那么尝试使用导入作为对象
import { componentName } from '/componentPath';
The { } are the nameplates for an Object. { } 是对象的铭牌。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.