Commit ec49429e authored by Andrew Fontaine's avatar Andrew Fontaine

Update Vue Documentation to Use Plugin/Mixin

This updates our documentation to ensure people are aware of the now
existing `GlFeatureFlagsPlugin` and `glFeatureFlagsMixin` to easily
provide and inject the feature flags object into a child component.
parent 02b3fbc5
import Vue from 'vue';
import GlFeatureFlagsPlugin from '~/vue_shared/gl_feature_flags_plugin';
if (process.env.NODE_ENV !== 'production') {
Vue.config.productionTip = false;
}
Vue.use(GlFeatureFlagsPlugin);
export default Vue => {
Vue.mixin({
provide: {
glFeatures: { ...(window.gon.features || {}) },
glFeatures: { ...((window.gon && window.gon.features) || {}) },
},
});
};
......@@ -2,7 +2,7 @@ export default () => ({
inject: {
glFeatures: {
from: 'glFeatures',
default: {},
default: () => ({}),
},
},
});
......@@ -108,42 +108,44 @@ document.addEventListener('DOMContentLoaded', () => new Vue({
Use Vue's [provide/inject](https://vuejs.org/v2/api/#provide-inject) mechanism
to make feature flags available to any descendant components in a Vue
application:
application. The `glFeatures` object is already provided in `commons/vue.js`, so
only the mixin is required to utilize the flags:
```javascript
// application entry point
document.addEventListener('DOMContentLoaded', () => new Vue({
el: '.js-vue-app',
provide: {
aFeatureFlag: gon.features.aFeatureFlag,
},
render(createElement) {
return createElement('my-component');
},
}));
// An arbitrary descendant component
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
export default {
...
inject: {
aFeatureFlag: {
from: 'aFeatureFlag',
default: false,
},
// ...
mixins: [glFeatureFlagsMixin()],
// ...
created() {
if (this.glFeatures.myFlag) {
// ...
}
},
...
}
```
This approach has several benefits:
This approach has a few benefits:
- Arbitrarily deeply nested components can opt-in and access the flag without
intermediate components being aware of it (c.f. passing the flag down via
props).
- Components can use a different local name for the flag if necessary.
- A default value can be declared in case the flag is not provided.
- Good testability, since the flag can be provided to `mount`/`shallowMount`
from `vue-test-utils` as easily as a prop.
```javascript
import { shallowMount } from '@vue/test-utils';
shallowMount(component, {
provide: {
glFeatures: { myFlag: true },
},
});
```
- No need to access a global variable, except in the application's
[entry point](#accessing-the-gl-object).
......
......@@ -110,7 +110,7 @@ The name of the feature flag in JavaScript will always be camelCased, meaning
that checking for `gon.features.vim_bindings` would not work.
See the [Vue guide](../fe_guide/vue.md#accessing-feature-flags) for details about
how to access feature flags in a Vue application.
how to access feature flags in a Vue component.
### Specs
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment