I am trying to reset data passed as props to children components. How should i write this ?
Context: i'm converting a ThreeJS implementation into Vue/Typescript. It includes a controls panel composed with slider inputs controlling parameters of the Three canvas. I separated this massive monolithic original code into 3 components : - child1: the controlsPanel, contains sliders and the reset button - child2: the Vue-GL canvas, emitting mouse events - parent: the component hosting initial data, and reseting.
parent :
<template>
<div>
<child1 :prop1="prop1" :prop2="prop2" :child1Prop="child1Prop" :reset="reset" />
<child2 :prop1="prop1" :prop2="prop2" :child2Prop="child2Prop" />
</div>
</template>
<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
import Child1 from './components/Child1.vue';
import Child2 from './components/Child2.vue';
const initialState = {
prop1: 1,
prop2: 2,
child1Prop: 'some text',
child2Prop: 'another text',
}
export type InitialState = typeof initialState;
@Component({
components: {
Child1,
Child2,
},
})
export default class App extends Vue {
public prop1!: InitialState['prop1'];
public prop2!: InitialState['prop2'];
public child1Prop!: InitialState['child1Prop'];
public child2Prop!: InitialState['child2Prop'];
public beforeMount() {
Object.assign(this, initialState);
}
public reset() {
Object.assign(this, initialState);
}
}
</script>
Child code :
<template>
...
<!-- this button is only in Child1 -->
<button type="button" @click="resetCamera">Reset</button>
</template>
<script lang="ts">
// import VueAsyncComputed, { IAsyncComputedProperty } from 'vue-async-computed';
import { Component, Prop, Vue } from 'vue-property-decorator';
import { InitialState } from '../App.vue';
@Component
export default class ChildX extends Vue {
@Prop() public prop1!: InitialState['prop1'];
@Prop() public prop2!: InitialState['prop2'];
@Prop() public childXProp!: InitialState['childXProp']; // Specific prop
// computed getters and methods using this.prop1 etc...
// this method is only in Child1
public resetCamera() {
this.reset();
}
}
</script>
Properties prop1 and prop2 are controlled by Child1 component and consumed by Child2. Child2 can also update these props (via mouse events), which should update sliders in Child1 appropriately.
I managed to make Typescript happy, but at the cost of typings everywhere...
Question1: is there a way to simplify while keeping the 2way-bindings between childs and parent App ? (2way-bindings don't work with above code)
Question2: How to reset all props ? my child1.resetCamera seems to call parent reset() but props are not reseting ...