gpt4 book ai didi

vue.js - 计算属性不更新 Prop 更改

转载 作者:行者123 更新时间:2023-12-05 04:02:53 25 4
gpt4 key购买 nike

当传递的 prop 对象中的嵌套属性发生更改时,我无法更新计算属性。

this.favourite 通过 props 传递,但是当 this.favourite.selectedChoices.second.idthis.favourite 时,计算属性不会更新。 selectedChoices.first.id 已更改。

关于如何使其具有响应性的任何想法?

这是计算属性:

isDisabled() {
const hasMultipleChoices = this.favourite.choices.length
? this.favourite.choices[0].value.some(value => value.choices.length) :
false;

if (hasMultipleChoices && !this.favourite.selectedChoices.second.id) {
return true;
} else if (this.favourite.choices.length && !this.favourite.selectedChoices.first.id) {
return true;
}

return false;
}

最佳答案

已测试

在我的 test.vue 中

props: {
variant: {
type: String,
default: ''
}
}

const myComputedName = computed(() => {
return {
'yellow--warning': props.variant === 'yellow',
'red--warning': props.variant === 'red',
}
})

测试.spec.js

    import { shallowMount } from '@vue/test-utils'
import test from '@/components/test.vue'
let wrapper

//default values
function createConfig(overrides) {
let variant = ''
const propsData = { variant }
return Object.assign({ propsData }, overrides)
}
//test
describe('test.vue Implementation Test', () => {
let wrapper

// TEARDOWN - run after to each unit test
afterEach(() => {
wrapper.destroy()
})
it('computed return red if prop variant is red', async (done) => {
const config = createConfig({ propsData: { variant: 'red' } })
wrapper = shallowMount(test, config)
wrapper.vm.$nextTick(() => {
//checking that my computed has changed, in my case I want to matchanObject
expect(wrapper.vm.myComputedName).toMatchObject({
'red--warning': true
})
//check what your computed value looks like
console.log(wrapper.vm.myComputedName)
done()
})
})

//TEST 2 Variant, this time instead red, lets say yellow

it('computed return yellow if prop variant is red', async (done) => {
const config = createConfig({ propsData: { variant: 'yellow' } })
wrapper = shallowMount(test, config)
wrapper.vm.$nextTick(() => {
//checking that my computed has changed, in my case I want to matchanObject
expect(wrapper.vm.myComputedName).toMatchObject({
'yellow--warning': true
})
//check what your computed value looks like
console.log(wrapper.vm.myComputedName)
done()
})
})
})

有关更多信息,此页面对我有帮助。

https://vuejsdevelopers.com/2019/08/26/vue-what-to-unit-test-components/

关于vue.js - 计算属性不更新 Prop 更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54090092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com