【发布时间】:2020-06-17 21:54:20
【问题描述】:
我的custom-form 组件中有一个包含一些自定义组件的表单,例如custom-input、custom-button 等。我的自定义组件(custom-form 除外)包含一个 disabled 道具。当单击提交按钮时,我想在所有自定义组件中将 disabled 属性设置为 true。
我需要动态执行此操作,但我不知道表单中使用了哪些自定义组件。我还需要提一下,我可能在一个页面上有多个表单。
我该如何处理?
这是我尝试过的。
--- Main Component ---
<template>
<div>
<slot />
</div>
</template>
<script>
export default {
}
</script>
--- Component1 ---
<template>
<div>
<span v-if="!disabled">this is Comp1</span>
</div>
</template>
<script>
export default {
props: {
disabled: {
type: Boolean,
default: false
}
}
}
</script>
--- Component2 ---
<template>
<div>
<span v-if="!disabled">this is Comp2</span>
</div>
</template>
<script>
export default {
props: {
disabled: {
type: Boolean,
default: false
}
}
}
</script>
--- My directive ---
import Vue from 'vue'
Vue.directive('disable', {
bind: (el, binding, vnode) => {
methods.setChildrent(vnode.context.$children, binding.value)
}
})
const methods = {
setChildrent(children, value) {
children.forEach(element => {
this.setChildrent(element.$children, value)
if(element.$options.propsData)
if ('disabled' in element.$props)
element.$props.disabled = value
})
}
}
--- Page ---
<template>
<MainComp v-disable="true">
<Comp1></Comp1>
<Comp2></Comp2>
</MainComp>
</template>
<script>
import Comp1 from './Comp1.vue'
import Comp2 from './Comp2.vue'
import MainComp from './MainComp.vue'
import Directives from '../directives/index.js'
export default {
name: 'HelloWorld',
components: {
Comp1,
Comp2,
MainComp,
},
directives: {
Directives
},
props: {
msg: String
},
methods: {
},
mounted() {
}
}
</script>
【问题讨论】: