提交 d96b8a24 编写于 作者: 雪洛's avatar 雪洛

fix(uni-app-x web): 修复unicloud-db组件ssr时无法在服务端取数据的Bug

上级 62e4e002
...@@ -5,6 +5,9 @@ export type AutoImportOptions = Options ...@@ -5,6 +5,9 @@ export type AutoImportOptions = Options
const uniPreset = { const uniPreset = {
from: '@dcloudio/uni-app', from: '@dcloudio/uni-app',
imports: [ imports: [
// ssr
'ssrRef',
'shallowSsrRef',
// uni-app lifecycle // uni-app lifecycle
// App and Page // App and Page
'onShow', 'onShow',
......
...@@ -5,8 +5,6 @@ ...@@ -5,8 +5,6 @@
</template> </template>
<script> <script>
export default { export default {
name: 'PageMeta', name: 'PageMeta'
setup(props, { emit }) {
}
} }
</script> </script>
...@@ -190,7 +190,7 @@ ...@@ -190,7 +190,7 @@
} as UniCloudDBComponentUpdateOptions) } as UniCloudDBComponentUpdateOptions)
} }
onLoadData! : (_ : UniCloudDBComponentLoadDataOptions) => void onLoadData! : (_ : UniCloudDBComponentLoadDataOptions) => Promise<void>
onLoadMore! : () => void onLoadMore! : () => void
onAdd! : (value : UTSJSONObject, options : UniCloudDBComponentAddOptions) => void onAdd! : (value : UTSJSONObject, options : UniCloudDBComponentAddOptions) => void
onUpdate!: (id : string, value : UTSJSONObject, options : UniCloudDBComponentUpdateOptions) => void onUpdate!: (id : string, value : UTSJSONObject, options : UniCloudDBComponentUpdateOptions) => void
...@@ -288,11 +288,23 @@ ...@@ -288,11 +288,23 @@
manual: { manual: {
type: Boolean, type: Boolean,
default: false default: false
},
ssrKey: {
type: String,
default: ""
} }
}, },
data() { data() {
return { return {
//#ifdef WEB
// TODO 修复类型错误
// @ts-ignore
dataList: ssrRef([] as Array<UTSJSONObject>) as Array<UTSJSONObject>,
//#endif
//#ifndef WEB
dataList: [] as Array<UTSJSONObject>, dataList: [] as Array<UTSJSONObject>,
//#endif
// dataList: [] as Array<UTSJSONObject>,
loading: false, loading: false,
hasMore: false, hasMore: false,
isEnded: false, isEnded: false,
...@@ -304,18 +316,27 @@ ...@@ -304,18 +316,27 @@
error: null as UniCloudError | null error: null as UniCloudError | null
} }
}, },
//#ifdef (WEB || APP-IOS) && !_NODE_JS_ //#ifdef WEB || APP-IOS
beforeCreate() { beforeCreate() {
if (!registerFlag) { if (!registerFlag) {
registerFlag = true registerFlag = true
// @ts-ignore // @ts-ignore
customElements.define( typeof customElements !== 'undefined' && customElements.define(
'uni-cloud-db-element', 'uni-cloud-db-element',
UniCloudDBElement, UniCloudDBElement,
) )
} }
}, },
//#endif //#endif
//#ifdef WEB
async serverPrefetch() : Promise<any> {
// @ts-ignore
if (!this.manual && this.loadtime === 'auto') {
// @ts-ignore
return this.loadData({})
}
},
//#endif
created() { created() {
this.pagination.current = this.pageCurrent this.pagination.current = this.pageCurrent
this.pagination.size = this.pageSize this.pagination.size = this.pageSize
...@@ -359,7 +380,7 @@ ...@@ -359,7 +380,7 @@
} }
) )
if (!this.manual && this.loadtime == LOAD_MODE_AUTO) { if (!this.manual && this.loadtime == LOAD_MODE_AUTO && this.dataList.length === 0) {
if (typeof this.collection == 'string') { if (typeof this.collection == 'string') {
const collectionString = this.collection as string const collectionString = this.collection as string
if (collectionString.length == 0) { if (collectionString.length == 0) {
...@@ -394,7 +415,7 @@ ...@@ -394,7 +415,7 @@
//#endif //#endif
}, },
methods: { methods: {
loadData(options : UniCloudDBComponentLoadDataOptions) { async loadData(options : UniCloudDBComponentLoadDataOptions) : Promise<void> {
let clear = (options.clear != null && options.clear == true) let clear = (options.clear != null && options.clear == true)
if (clear == true) { if (clear == true) {
if (this.pageData == PAGE_MODE_REPLACE) { if (this.pageData == PAGE_MODE_REPLACE) {
...@@ -403,7 +424,7 @@ ...@@ -403,7 +424,7 @@
this.reset() this.reset()
} }
this.get(options) await this.get(options)
}, },
loadMore() { loadMore() {
if (this.isEnded || this.loading) { if (this.isEnded || this.loading) {
...@@ -427,7 +448,7 @@ ...@@ -427,7 +448,7 @@
reset() { reset() {
this.pagination.current = 1 this.pagination.current = 1
}, },
get(options? : UniCloudDBComponentLoadDataOptions) { async get(options? : UniCloudDBComponentLoadDataOptions) : Promise<void> {
let loadAfterClear = false let loadAfterClear = false
if (options != null && options.clear != null && options.clear == true) { if (options != null && options.clear != null && options.clear == true) {
loadAfterClear = true loadAfterClear = true
...@@ -439,7 +460,7 @@ ...@@ -439,7 +460,7 @@
this.error = null this.error = null
this.loading = true this.loading = true
this.getExec().then((res : UniCloudDBGetResult) => { await this.getExec().then((res : UniCloudDBGetResult) => {
const data = res.data const data = res.data
const count = res.count const count = res.count
......
...@@ -60,7 +60,7 @@ export function initPlugins( ...@@ -60,7 +60,7 @@ export function initPlugins(
config, config,
extend({ exclude: [...COMMON_EXCLUDE, /\/@dcloudio\/uni-app/] }, options) extend({ exclude: [...COMMON_EXCLUDE, /\/@dcloudio\/uni-app/] }, options)
), ),
'vite:vue' process.env.UNI_APP_X === 'true' ? 'uts' : 'vite:vue'
) )
addPlugin(plugins, uniJsonPlugin(options), 'vite:json', 'pre') addPlugin(plugins, uniJsonPlugin(options), 'vite:json', 'pre')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册