c-page的内容
<template>
<view>c-page</view>
<global-config>
<slot></slot>
</global-config>
</template>
<script>
import {
defineComponent,
provide
} from 'vue';
export default defineComponent({
setup() {
provide('count1', 9999)
return {};
},
});
</script>
<style scoped></style>
global-config的内容
<template>
<view>global-config</view>
<slot></slot>
</template>
<script>
import { defineComponent, provide, reactive, ref } from 'vue';
export default defineComponent({
setup() {
const state = reactive({
count: 666,
});
const count = ref(777);
provide('state', state);
provide('count', count);
return {};
},
});
</script>
<style scoped></style>
使用的页面的内容
<template>
<c-page>
<view>index page</view>
<button>state count is {{ state.count || 0 }}</button>
<button>count is {{ count || 0 }}</button>
<button>count1 is {{ count1 || 0 }}</button>
</c-page>
</template>
<script>
import { defineComponent, inject, watchEffect } from 'vue';
export default defineComponent({
setup() {
const state = inject('state', {});
const count = inject('count', 0);
const count1 = inject('count1', 0);
watchEffect(()=>{
console.log('state.count', state.count);
console.log('count', count.value);
})
return {
state,
count,
count1
};
},
});
</script>
<style scoped></style>
cxycode (作者)
不好意思,熬夜太多码魔怔了
2022-10-26 13:59
Mrchenyong
回复 cxycode: 我现在在头条小程序平台没有效果怎么回事,子组件接收到的是undfine
2023-07-15 16:33
hhyang
能否看看这个问题? 一个月了 https://github.com/dcloudio/uni-app/issues/4386
2023-07-15 17:02
cxycode (作者)
回复 Mrchenyong: 可以弄个在线demo或者zip方便分析下代码有没有问题,如果代码没问题那就是平台兼容性
2023-11-03 18:08