0
# Version Detection
1
2
Runtime flags and utilities for detecting the current Vue version and accessing version-specific APIs. These utilities enable conditional logic based on the Vue version in use.
3
4
## Capabilities
5
6
### Vue Version Flags
7
8
Boolean flags that indicate which Vue version is currently active.
9
10
```typescript { .api }
11
/**
12
* Boolean flag indicating if running under Vue 2 environment
13
* true for Vue 2.x (including 2.7), false for Vue 3.x
14
*/
15
declare const isVue2: boolean;
16
17
/**
18
* Boolean flag indicating if running under Vue 3 environment
19
* true for Vue 3.x, false for Vue 2.x
20
*/
21
declare const isVue3: boolean;
22
```
23
24
**Usage Examples:**
25
26
```typescript
27
import { isVue2, isVue3 } from "vue-demi";
28
29
// Conditional logic based on Vue version
30
if (isVue2) {
31
// Vue 2 specific implementation
32
console.log("Using Vue 2 features");
33
// Access Vue 2 specific APIs
34
} else if (isVue3) {
35
// Vue 3 specific implementation
36
console.log("Using Vue 3 features");
37
// Access Vue 3 specific APIs
38
}
39
40
// Version-specific component setup
41
const setupComponent = () => {
42
if (isVue2) {
43
// Vue 2 component setup
44
return {
45
beforeDestroy() {
46
// Vue 2 lifecycle
47
}
48
};
49
} else {
50
// Vue 3 component setup
51
return {
52
beforeUnmount() {
53
// Vue 3 lifecycle
54
}
55
};
56
}
57
};
58
```
59
60
### Vue 2 Constructor Access
61
62
Reference to the Vue 2 constructor for accessing global Vue 2 APIs. This is undefined when running on Vue 3.
63
64
```typescript { .api }
65
/**
66
* Reference to Vue 2 constructor, undefined in Vue 3
67
* Provides access to Vue 2's global API when available
68
*/
69
declare const Vue2: typeof Vue | undefined;
70
```
71
72
**Usage Examples:**
73
74
```typescript
75
import { Vue2 } from "vue-demi";
76
77
// Safely access Vue 2 global configuration
78
if (Vue2) {
79
// Configure Vue 2 instance
80
Vue2.config.productionTip = false;
81
Vue2.config.ignoredElements.push('custom-element');
82
83
// Register global components
84
Vue2.component('GlobalComponent', {
85
template: '<div>Global Component</div>'
86
});
87
88
// Add global mixins
89
Vue2.mixin({
90
created() {
91
console.log('Global mixin');
92
}
93
});
94
95
// Access Vue 2 utilities
96
Vue2.set(obj, key, value);
97
Vue2.delete(obj, key);
98
}
99
```
100
101
### Version String
102
103
The Vue version string is available when running on Vue 2 or Vue 2.7. This export is not available in Vue 3.
104
105
```typescript { .api }
106
/**
107
* Vue version string (available in Vue 2/2.7 implementations only)
108
* Contains the semver version like "2.6.14" or "2.7.16"
109
* Not exported in Vue 3 implementations
110
*/
111
declare const version: string;
112
```
113
114
**Usage Examples:**
115
116
```typescript
117
import { version, isVue2 } from "vue-demi";
118
119
// version is only available in Vue 2/2.7
120
if (isVue2) {
121
console.log(`Vue version: ${version}`);
122
123
// Check for specific Vue 2 versions
124
if (version.startsWith('2.7')) {
125
// Vue 2.7 specific features
126
console.log('Using Vue 2.7 with built-in Composition API');
127
} else {
128
// Vue 2.6 and below
129
console.log('Using Vue 2.6 with @vue/composition-api plugin');
130
}
131
} else {
132
// Vue 3 - version export not available
133
console.log('Using Vue 3');
134
}
135
```
136
137
### Legacy Vue Export
138
139
Deprecated export that provides access to the Vue constructor. This is provided for backward compatibility but should be avoided in new code.
140
141
```typescript { .api }
142
/**
143
* @deprecated To avoid bringing in all the tree-shakable modules,
144
* this API has been deprecated. Use `Vue2` or named exports instead.
145
* Refer to https://github.com/vueuse/vue-demi/issues/41
146
*/
147
declare const V: typeof Vue;
148
```
149
150
**Migration Example:**
151
152
```typescript
153
// Old (deprecated)
154
import { V as Vue } from "vue-demi";
155
Vue.config.productionTip = false;
156
157
// New (recommended)
158
import { Vue2 } from "vue-demi";
159
if (Vue2) {
160
Vue2.config.productionTip = false;
161
}
162
```
163
164
### Injection Context Check
165
166
Utility function to check if injection context is available, useful for safely using provide/inject.
167
168
```typescript { .api }
169
/**
170
* Check if injection context exists
171
* Falls back to getCurrentInstance() check
172
* @returns boolean indicating if injection context is available
173
*/
174
declare function hasInjectionContext(): boolean;
175
```
176
177
**Usage Examples:**
178
179
```typescript
180
import { hasInjectionContext, inject, provide } from "vue-demi";
181
182
// Safe injection usage
183
function useTheme() {
184
if (hasInjectionContext()) {
185
return inject('theme', 'light');
186
}
187
return 'light'; // fallback
188
}
189
190
// Conditional provide/inject
191
export default {
192
setup() {
193
if (hasInjectionContext()) {
194
provide('theme', 'dark');
195
}
196
197
const theme = useTheme();
198
return { theme };
199
}
200
};
201
```