VUE组件中的JavaScript导出功能失败
我想简化我的代码,因此我将一些函数放在这样的JS文件中:
[...]
function changeToEditView(reportId)
{
let pathEdit="/edit/"+reportId;
this.$router.push({ path: pathEdit, replace: true });
}
[...]
export {convertDate, deleteReport, changeToEditView, getPdfByReportId}
当我在vue组件中导入它们时,
import axios from 'axios'
import convertDate from '@/js/methods'
import deleteReport from '@/js/methods'
import changeToEditView from '@/js/methods'
import getPdfByReportId from '@/js/methods'
export default
{
[...]
methods:
{
convertDate,
deleteReport,
changeToEditView,
getPdfByReportId,
我有此消息:
warning in ./src/views/DashboardView.vue?vue&type=script&lang=js
export 'default' (imported as 'deleteReport') was not found in '@/js/methods' (possible exports: changeToEditView, convertDate, deleteReport, getPdfByReportId)
warning in ./src/views/DashboardView.vue?vue&type=script&lang=js
export 'default' (imported as 'changeToEditView') was not found in '@/js/methods' (possible exports: changeToEditView, convertDate, deleteReport, getPdfByReportId)
warning in ./src/views/DashboardView.vue?vue&type=script&lang=js
export 'default' (imported as 'getPdfByReportId') was not found in '@/js/methods' (possible exports: changeToEditView, convertDate, deleteReport, getPdfByReportId)
我尝试在TE JS文件中导出后将'默认值'列为但是这些功能都没有用
export default {convertDate, deleteReport, changeToEditView, getPdfByReportId}
I wanted to simplify my code, so i put some functions in a js file like that :
[...]
function changeToEditView(reportId)
{
let pathEdit="/edit/"+reportId;
this.$router.push({ path: pathEdit, replace: true });
}
[...]
export {convertDate, deleteReport, changeToEditView, getPdfByReportId}
and when I import them in my vue component like that
import axios from 'axios'
import convertDate from '@/js/methods'
import deleteReport from '@/js/methods'
import changeToEditView from '@/js/methods'
import getPdfByReportId from '@/js/methods'
export default
{
[...]
methods:
{
convertDate,
deleteReport,
changeToEditView,
getPdfByReportId,
I have this message :
warning in ./src/views/DashboardView.vue?vue&type=script&lang=js
export 'default' (imported as 'deleteReport') was not found in '@/js/methods' (possible exports: changeToEditView, convertDate, deleteReport, getPdfByReportId)
warning in ./src/views/DashboardView.vue?vue&type=script&lang=js
export 'default' (imported as 'changeToEditView') was not found in '@/js/methods' (possible exports: changeToEditView, convertDate, deleteReport, getPdfByReportId)
warning in ./src/views/DashboardView.vue?vue&type=script&lang=js
export 'default' (imported as 'getPdfByReportId') was not found in '@/js/methods' (possible exports: changeToEditView, convertDate, deleteReport, getPdfByReportId)
I tried to put 'default' after export in te js file like that but none of these functions work
export default {convertDate, deleteReport, changeToEditView, getPdfByReportId}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在这里有几个问题。
第一个是简单的。例如,将您的脚本更新到命名函数的导出:
然后导入:
然后您可以在VUE组件中使用这些方法。您无需先注册它们,也不需要使用
this。
。例如:
this 答案 在更详细地解释了命名出口和默认导出之间的差异。
下一个问题是
this。$ router
无法像这样访问。您将需要导入Vue路由器的实例才能在其上调用.push。You've got several issues here.
The first is a simple one. Update your script to export named functions, for example:
And then import with:
You'll then be able to use these methods within your Vue component. You don't need to register them first, nor do you need to use
this.
.For example:
This answer explains the differences between named exports and default exports in a bit more detail.
The next issue, is that
this.$router
isn't going to be accessible like this. You will need to import your instance of Vue router to call .push on it.