Skip to content

Commit 73a8d2f

Browse files
committed
Add provide/inject working example
1 parent 280eb11 commit 73a8d2f

File tree

3 files changed

+103
-16
lines changed

3 files changed

+103
-16
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<template>
2+
<p class="demo">
3+
<button class="btn" :style="{ color: '#fff', backgroundColor: primary && theme.primaryColor }">
4+
<slot></slot>
5+
</button>
6+
</p>
7+
</template>
8+
9+
<script>
10+
export default {
11+
inject: {
12+
theme: {
13+
default: {
14+
primaryColor: 'darkseagreen',
15+
},
16+
},
17+
},
18+
props: {
19+
primary: {
20+
type: Boolean,
21+
default: true,
22+
},
23+
},
24+
};
25+
</script>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script>
2+
export default {
3+
provide: {
4+
theme: {
5+
primaryColor: '#3eaf7c',
6+
},
7+
},
8+
render(h) {
9+
return this.$slots.default[0];
10+
},
11+
};
12+
</script>

docs/patterns/README.md

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ export default Mouse;
766766
- [Leveraging Render Props in Vue](https://medium.com/@dillonchanis/leveraging-render-props-in-vue-7eb9a19c262d)
767767
- [Use a Vue.js Render Prop!](https://medium.com/js-dojo/use-a-vue-js-render-prop-98880bc44e05)
768768

769-
## Passing Props
769+
## Passing Props & Listeners
770770

771771
Sometimes, you may want to pass props and listeners to child component without having to declare all child component's props.
772772
You can bind `$attrs` and `$listeners` in child component and set [`inheritAttrs` to `false`](https://vuejs.org/v2/api/#inheritAttrs) (otherwise both, `div` and `child-component` will receive the attributes)
@@ -857,29 +857,37 @@ With above example component hierarchy, in order to derive data from `parent-com
857857
### Provide / Inject
858858

859859
```js
860-
// ParentComponent.vue
860+
// ThemeProvider
861861

862862
export default {
863863
provide: {
864864
theme: {
865-
primaryColor: 'blue',
865+
primaryColor: '#3eaf7c',
866866
},
867867
},
868868
};
869869
```
870870

871-
```html
872-
// GrandChildComponent.vue
871+
```vue
872+
// ThemeButton.vue
873873
874874
<template>
875-
<button :style="{ backgroundColor: primary && theme.primaryColor }">
876-
<slot></slot>
877-
</button>
875+
<p class="demo">
876+
<button class="btn" :style="{ color: '#fff', backgroundColor: primary && theme.primaryColor }">
877+
<slot></slot>
878+
</button>
879+
</p>
878880
</template>
879881
880882
<script>
881883
export default {
882-
inject: ['theme'],
884+
inject: {
885+
theme: {
886+
default: {
887+
primaryColor: 'darkseagreen',
888+
},
889+
},
890+
},
883891
props: {
884892
primary: {
885893
type: Boolean,
@@ -890,6 +898,18 @@ export default {
890898
</script>
891899
```
892900

901+
```vue
902+
<theme-provider>
903+
<theme-button>Themed Button</theme-button>
904+
</theme-provider>
905+
```
906+
907+
#### Working Example:
908+
909+
<ThemeProvider>
910+
<ThemeButton>Themed Button</ThemeButton>
911+
</ThemeProvider>
912+
893913
### [@Provide / @Inject Decorator](https://github.com/kaorun343/vue-property-decorator)
894914

895915
```js
@@ -932,6 +952,7 @@ export class GrandChildComponent extends Vue {
932952
### `errorCaptured` Hook
933953

934954
```js
955+
// ErrorBoundary.vue
935956
export default {
936957
name: 'ErrorBoundary',
937958
data() {
@@ -942,37 +963,66 @@ export default {
942963
},
943964
errorCaptured(err, vm, info) {
944965
this.error = true;
945-
this.errorMessage = `${err.stack}\n\nfound in ${info} of component`;
966+
this.errorMessage = `Sorry, error occured in ${info}`;
946967

947968
return false;
948969
},
949970
render(h) {
950971
if (this.error) {
951-
return h('pre', { style: { color: 'red' } }, this.errorMessage);
972+
return h('p', { class: 'demo bg-danger' }, this.errorMessage);
952973
}
953974

954975
return this.$slots.default[0];
955976
},
956977
};
957978
```
958979

980+
```vue
981+
// ThrowError.vue
982+
983+
<template>
984+
<p class="demo">
985+
<button class="btn btn-danger" @click.prevent="throwError()">Error Thrown Button ({{count}})</button>
986+
</p>
987+
</template>
988+
989+
<script>
990+
export default {
991+
data() {
992+
return {
993+
count: 0,
994+
};
995+
},
996+
watch: {
997+
count() {
998+
throw new Error('error');
999+
},
1000+
},
1001+
methods: {
1002+
throwError() {
1003+
this.count++;
1004+
},
1005+
},
1006+
};
1007+
</script>
9591008
```
1009+
1010+
```vue
9601011
<error-boundary>
961-
<another-component/>
1012+
<throw-error></throw-error>
9621013
</error-boundary>
9631014
```
9641015

1016+
#### Working Example:
1017+
9651018
<ErrorBoundary>
9661019
<ThrowError></ThrowError>
9671020
</ErrorBoundary>
9681021

969-
#### Examples
970-
971-
- [Example 1](https://jsfiddle.net/Linusborg/z84wspcg/)
972-
9731022
#### References
9741023

9751024
- [Handling Errors in Vue with Error Boundaries](https://medium.com/@dillonchanis/handling-errors-in-vue-with-error-boundaries-91f6ead0093b)
1025+
- [Example 1](https://jsfiddle.net/Linusborg/z84wspcg/)
9761026

9771027
## Productivity Tips
9781028

0 commit comments

Comments
 (0)