我不确定在挂载组件时如何提供注入(inject) react 数据。我不需要确切的解决方案,只需提示即可。
这是我的父组件:
@Component
export default class Preferences extends Vue {
@ProvideReactive() serviceLevels: CarrierFilterType[] = [];
// more code here...
这是我的子组件:
@Component
export default class CarrierFilters {
@InjectReactive() readonly serviceLevels!: CarrierFilterType[];
// more code here...
这是我的测试文件:
// other imports are here...
import { supportedServiceLevels } from '../../constant';
// initial setup code here...
describe('Settings > Preferences > Content > CarrierFilters.vue', () => {
beforeEach(() => {
options = {
localVue,
i18n,
store,
router,
vuetify: new Vuetify(),
provide: { // I am not sure how to provide the inject-reactive data here...?
serviceLevels: [...supportedServiceLevels],
},
};
initWrapper = shallowMount(CarrierFilters, options);
});
// more code here...
目前,我在运行测试时收到以下控制台错误:
console.error node_modules/vue/dist/vue.runtime.common.dev.js:621
[Vue warn]: Injection "__reactiveInject__" not found
found in
---> <CarrierFilters>
<Root>
顺便说一句,上面的代码使用了@Inject
,但没有使用@InjectReactive
。我看过他们的源代码,看来我必须以某种方式提供这个 key __reactiveInject__
。
最佳答案
将所有响应式属性放入 __reactiveInject__
中。例如:
const wrapper = mount(Component, {
localVue,
provide: {
__reactiveInject__: {
foo: "bar"
},
},
});
关于vue.js - 如何在 vue-test-utils 上测试 Vue-Property-Decorator 的 @InjectReactive()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71871221/