我正在创建一个自定义组件库,希望在多个vue ssr项目之间共享。
问题:
使用当前选项,我收到一个水化错误(客户端渲染的虚拟DOM树与服务器渲染的内容不匹配。)如果我将组件包装在我不想做的<no-ssr>
标记中,则该错误已修复。 。我之前在一个vue项目中直接拥有了这些组件文件,并且它们可以正常工作。
无效的示例文件:
<template>
<div class="collapse">
<div class="trigger" @click="handleClickEvent">
<slot name="trigger"></slot>
</div>
<div :id="uniqueKey" class="details">
<slot></slot>
</div>
</div>
</template>
<style lang="scss" scoped>
.collapse {
.trigger {
cursor: pointer;
}
.details {
max-height: 0;
overflow: hidden;
transition: max-height 0.3s ease-out;
}
}
</style>
<script>
import $ from 'bucks-js'
export default {
name: 'collapse',
data: () => ({
uniqueKey: null,
}),
props: {
// if collapse should be triggered from outside
manual: { type: Boolean, default: false },
},
created () {
this.uniqueKey = 'mapify-collapse-' + $.string.random()
},
methods: {
/**
* Handles click event.
*/
handleClickEvent () {
// if collapse should be manual triggered , fire event and open from outside
this.$emit('triggered')
if (!this.manual) this.openCloseDetails()
},
/**
* Open / close details collapse.
*/
openCloseDetails () {
if (!$.isClient()) return
let detailsContainer = document.getElementById(this.uniqueKey)
// check if open
if (detailsContainer.style.maxHeight) {
detailsContainer.style.maxHeight = null
} else {
detailsContainer.style.maxHeight = detailsContainer.scrollHeight + 'px'
}
},
/**
* Open details.
*/
openDetails () {
if (!$.isClient()) return
let detailsContainer = document.getElementById(this.uniqueKey)
detailsContainer.style.maxHeight = detailsContainer.scrollHeight + 'px'
},
/**
* Close details.
*/
closeDetails () {
if (!$.isClient()) return
let detailsContainer = document.getElementById(this.uniqueKey)
detailsContainer.style.maxHeight = null
},
},
}
</script>
这是我的package.json的一部分:
"scripts": {
"build": "rm -rf ./lib && vue-cli-service build --target lib src/index.js"
},
"dependencies": {
"bucks-js": "^1.5.3",
"vue": "^2.6.11"
},
"devDependencies": {
"@vue/cli-plugin-babel": "^4.2.3",
"@vue/cli-service": "^4.2.3",
"node-sass": "^4.13.1",
"sass-loader": "^8.0.2",
"vue-template-compiler": "^2.6.11"
}
我还尝试使用非常有用的存储库https://github.com/team-innovation/vue-sfc-rollup设置软件包,但是组件不再正常工作。