【问题标题】:Vue-cli 3 component library to support SSRVue-cli 3组件库支持SSR
【发布时间】:2020-04-06 10:51:31
【问题描述】:

我正在创建一个我想跨多个域共享的自定义组件库。

  • 每个域都有自己的 nuxt 实例
  • 每个域都有my-component-lib注册在package.json
  • 每个域都将 lib 注册为插件

    //my-component-lib.js
    import components from 'my-component-lib'
    import Vue from 'vue'
    
    export default ({ store }) => {
     Vue.use(components, { store: store })
    }
    
    //nuxt.config.js
    plugins: [
      /*Desired option 1*/ '@/plugins/my-component-lib',
      /*Currently using*/ { src: '@/plugins/my-component-lib', ssr: false }
    ]
    

我的组件库:

  • 使用 vue-cli 3 进行设置

  • 该库由基本的 html 标签和 CSS ex <input ></input> 组成。样式很重要,我想将它与组件 (extract:false) 放在一起,这样我就可以拉出单个组件而不必担心导入 css 文件。

    //vue.config.js
    module.exports = {
     outputDir: 'dist',
     lintOnSave: false,
     css: {
      extract: false
     }
    }
    

使用"production": "vue-cli-service build --target lib --name sc components/index.js"进行生产设置

问题:

  1. 使用所需选项,当我运行 nuxt npm run dev 时,我在 sc.common.js 内的函数 addStyle (obj /* StyleObjectPart */) {..} 中得到一个 document is not defined
  2. 使用当前选项,我得到一个水合错误(The client-side rendered virtual DOM tree is not matching server-rendered content.),如果我将组件包装在我不想这样做的<no-ssr> 标签中,则该错误已修复。

我想编译我的组件库以使用 SSR 而不必导入大型 css 文件

【问题讨论】:

  • 你如何解决第二个问题?我有同样的问题。
  • 更改 CSS 属性为我解决了问题
  • 仅供参考,如果您在

    标签内嵌套 html 标签,则会引发该错误

  • 还要确保在安装组件之前不要使用文档或 Windows
  • 我将 css 属性切换为 true,但我仍然遇到同样的问题。您在项目中使用的是 scss 还是 sass?

标签: vue.js webpack vuejs2 vue-component


【解决方案1】:

改变

 ...
 css: {
  extract: false
 }
 ...

true

【讨论】:

  • 但这不是导出了 CSS,而你又得到了一个需要导入的 CSS 文件吗?
  • @HerrSiering - 确实如此 - 我最终做的是使用 rollup 来处理这一切 - 我正在使用 vue 并且汇总使它变得容易。唯一的问题是 rollup 不处理延迟加载 components (ex:{ lazyLoadedComp:()=>import('file location') } )但在我的情况下还可以
猜你喜欢
  • 1970-01-01
  • 2018-08-10
  • 2018-10-11
  • 2021-08-20
  • 2019-02-13
  • 2021-05-25
  • 2021-10-23
  • 2021-09-09
  • 1970-01-01
相关资源
最近更新 更多