vuejs2 - Vue.component 构造引发错误,直到添加“.default”键
问题描述
在@vue/cli 4 和 vuejs 2.6.10 应用程序中,尝试使用带有命令的组件:
Vue.component('listing-header', require('../../../src/components/ListingHeader.vue'))
我得到控制台错误:
[Vue warn]: Failed to mount component: template or render function not defined.
在网上搜索后,我发现它可以通过以下方式修复:
Vue.component('listing-header', require('../../../src/components/ListingHeader.vue').default)
我想知道这里的“.default”是什么?
解决方案
查看 Webpack 的动态导入文档
我们需要的原因
default
是,从 webpack 4 开始,在导入 CommonJS 模块时,导入将不再解析为 module.exports 的值,而是会为 CommonJS 模块创建一个人工命名空间对象。
import()
promise resolve 是一个包含模块导出作为键的对象。通过使用.default
,您正在访问模块的默认导出....
推荐阅读
- java - 如何在 Spring JPA/Hibernate 中使用 JoinTable 设置仅通过 ID 引用父级的子实体
- sql - 如果有 3 条重复记录,则选择两条重复记录
- node.js - 没有沙箱的 Actions SDK 对话框 nodejs 事务 API 示例
- html - 消除视频加载后的页面跳转
- python - Python Selenium 点击按钮
- python - segyio > 如何解决 ImportError: DLL load failed: The specified module could not be found
- python - 从 Google Cloud Function 发出 POST 请求时 Python 超时
- tableau-api - 使用 Tableau API 检索嵌入式数据源的自定义 SQL
- c++ - VSCode 调试旧文件而不是当前文件
- c# - 如何使 WinForms 控件透明?