bootstrap-vue
Version:
With more than 85 components, over 45 available plugins, several directives, and 1000+ icons, BootstrapVue provides one of the most comprehensive implementations of the Bootstrap v4 component and grid system available for Vue.js v2.6, complete with extens
47 lines (42 loc) • 1.3 kB
JavaScript
// SSR safe client-side ID attribute generation
// ID's can only be generated client-side, after mount
// `this._uid` is not synched between server and client
// @vue/component
export default {
props: {
id: {
type: String // default: null
}
},
data: function data() {
return {
localId_: null
};
},
computed: {
safeId: function safeId() {
// Computed property that returns a dynamic function for creating the ID
// Reacts to changes in both `.id` and `.localId_` and regenerates a new function
var id = this.id || this.localId_; // We return a function that accepts an optional suffix string
// So this computed prop looks and works like a method
// but benefits from Vue's computed prop caching
var fn = function fn(suffix) {
if (!id) {
return null;
}
suffix = String(suffix || '').replace(/\s+/g, '_');
return suffix ? id + '_' + suffix : id;
};
return fn;
}
},
mounted: function mounted() {
var _this = this;
// `mounted()` only occurs client-side
this.$nextTick(function () {
// Update DOM with auto-generated ID after mount
// to prevent SSR hydration errors
_this.localId_ = "__BVID__".concat(_this._uid);
});
}
};