【发布时间】:2020-05-16 12:57:08
【问题描述】:
我正在尝试运行一个基本的 Nuxt 应用程序,其中包含一个使用 lerna monorepo 中的 vue-cli 构建的外部 Vue 组件。
页面短暂显示组件内容(服务器渲染)然后消失并抛出以下错误。
"export 'default' (imported as 'Header') was not found in 'a2b-header'
紧随其后
Mismatching childNodes vs. VNodes: NodeList(7) [svg, text, div#app, text, h2.subtitle, text, div.links] (7) [VNode, VNode, VNode, VNode, VNode, VNode, VNode]
最后是红色的 Vue 警告
[Vue warn]: The client-side rendered virtual DOM tree is not matching server-rendered content. This is likely caused by incorrect HTML markup, for example nesting block-level elements inside <p>, or missing <tbody>. Bailing hydration and performing full client-side render.
我用于外部组件的设置是 package.json:
{
"name": "a2b-header",
"version": "0.1.0",
"private": true,
"main": "./dist/a2b-header.umd.js",
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build --target lib --name a2b-header src/main.js",
"lint": "vue-cli-service lint"
},
"dependencies": {
"core-js": "^3.4.3",
"vue": "^2.6.10"
},
...
}
我的 main.js 如下所示:
import Header from './Header.vue'
export default Header
而组件文件本身Header.vue是:
<template>
<div id="app">
<h1>Welcome to Your Vue.js App</h1>
</div>
</template>
<script>
export default {
name: 'Header'
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
所有都被导入 Nuxt 项目 index.vue 使用简单:
import Header from 'a2b-header'
并且....它不起作用。我认为 SSR 与客户端的不匹配与不正确的导出有关,可能可以通过一些 webpack 配置解决,但在尝试了许多不同的事情之后,我在这里非常挣扎。
我想让它发挥作用的原因是,我们计划在 monorepo 中拥有各种 Vue 应用程序(SPA 和 Nuxt),并且将通用代码封装在可跨不同项目重用的组件中的能力至关重要。
【问题讨论】:
标签: vue.js webpack nuxt.js vue-cli lerna