53
loading...
This website collects cookies to deliver better user experience
RexComponent (Anyone needs my wallet address?)
├── TomComponent
├── PeterComponent
├── DurryComponent (yes I need it)
RexComponent
has a wallet address to give out and DurryComponent
is the only one in need of the wallet address. We will have to pass the wallet address from RexComponent
to TomComponent
to PeterComponent
, and finally to DurryComponent
. This results in the redundant piece of code in both TomComponent
and PeterComponent
.DurryComponent
would receive the wallet from RexComponent
without passing through TomComponent
and PeterComponent
.//Parent component
<script lang="ts">
import {Component, Vue} from 'vue-property-decorator';
import Child from '@/components/Child.vue';
@Component({
components: {
Child
},
provide: {
'name': 'Paul',
},
})
export default class Parent extends Vue {
}
</script>
<template>
<h1> My name is {{name}}</h1>
</template>
<script lang="ts">
import {Component, Inject, Vue} from 'vue-property-decorator';
@Component({})
export default class Child extends Vue {
@Inject('name')
name!: string; // non-null assertion operator
}
</script>
vue-property-decorator
also exposes @Provide
decorator for declaring providers.@Provide
decorator, we can make dependencies available in the parent component://Parent component
export default class ParentComponent extends Vue {
@Provide("user-details") userDetails: { name: string } = { name: "Paul" };
}
//Child component
<script lang="ts">
import {Component, Inject, Vue} from 'vue-property-decorator';
@Component({})
export default class ChildComponent extends Vue {
@Inject('user-details')
user!: { name: string };
}
</script>
FatherComponent
├── SonComponent
├── GrandsonComponent
//Father component
<script lang="ts">
import {Component, Vue} from 'vue-property-decorator';
import SonComponent from '@/components/Son.vue';
@Component({
components: {
SonComponent
},
provide: {
'family-name': 'De Ekongs',
},
})
export default class FatherComponent extends Vue {
}
</script>
family-name
dependency is provided by the FatherComponent
://Son component
<script lang="ts">
import {Component, Vue} from 'vue-property-decorator';
import GrandsonComponent from '@/components/Grandson.vue';
@Component({
components: {
GrandsonComponent
},
provide: {
'family-name': 'De Royals',
},
})
export default class SonComponent extends Vue {
}
</script>
SonComponent
overrides the family-name
dependency previously provided by the FatherComponent
://Grand son Component
<template>
<h1> Our family name is {{familyName}}</h1>
</template>
<script lang="ts">
import {Component, Inject, Vue} from 'vue-property-decorator';
@Component({})
export default class Child extends Vue {
@Inject('family-name')
familyName!: string; // non-null assertion operator
}
</script>
De Royals
will be rendered in the template of the GrandsonComponent
.Symbol('foo') === Symbol('foo') // false
Symbol
. This will ensure that no dependency gets overridden by another:export const FAMILY = {
FAMILY_NAME: Symbol('FAMILYNAME'),
};