Skip to content

Commit 8f77a5a

Browse files
committed
docs: Simplify useStore and useRouter components
1 parent 9c286f2 commit 8f77a5a

File tree

1 file changed

+14
-104
lines changed

1 file changed

+14
-104
lines changed

README.md

Lines changed: 14 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,9 @@ Some Vue composition API functions have not been included in this library but
138138
can be created easily by following the steps below.
139139

140140
<details><summary>useStore</summary><p>
141+
141142
Creating a useStore function connected to Vuex store is pretty straightforward.
142-
First we have to export our Vuex store:
143+
For example, given the following store:
143144

144145
```typescript
145146
// @src/mystore.ts
@@ -163,18 +164,20 @@ const store = new Vuex.Store({
163164
export default store
164165
```
165166

166-
Then we import the store and expose it in useStore function:
167+
We can get the store from the `vm` and expose it in our useStore function:
167168

168169
```typescript
169170
// @src/useStore.ts
170-
import store from '@src/mystore'
171+
import { getCurrentInstance } from '@vue/composition-api'
171172

172173
export function useStore() {
173-
return store
174+
const vm = getCurrentInstance()
175+
if (!vm) throw new Error('Vue instance not found!')
176+
return vm.$store
174177
}
175178
```
176179

177-
Now we can use it inside the setup() method of our component:
180+
Now we can use useStore inside the setup() method of our component:
178181

179182
```html
180183
// MyComponent.vue
@@ -202,37 +205,24 @@ Now we can use it inside the setup() method of our component:
202205
</p></details>
203206

204207
<details><summary>useRouter</summary><p>
205-
Creating a useRouter function that exposes `this.$router` and `this.$route` from VueRouter is very easy.
206-
First we have to export the Vue's `vm` instance:
207-
208-
```typescript
209-
// @src/main.ts
210-
import VueCompositionAPI from '@src/api'
211-
import router from '@src/router'
212-
import App from '@src/App.vue'
213-
214-
Vue.use(VueCompositionAPI)
215208

216-
export const vm = new Vue({
217-
router,
218-
render: h => h(App)
219-
}).$mount('#app')
220-
```
221-
222-
Then we can expose `$router` and `$route` in our useRouter function:
209+
Creating a useRouter function connected to VueRouter is rather simple.
210+
We can get `$route` and `$router` from the `vm` and expose them in our useRouter function:
223211

224212
```typescript
225213
// @src/useRouter.ts
226-
import { vm } from '@src/main'
214+
import { getCurrentInstance } from '@vue/composition-api'
227215

228216
export function useRouter() {
217+
const vm = getCurrentInstance()
218+
if (!vm) throw new Error('Vue instance not found!')
229219
const route = vm.$route
230220
const router = vm.$router
231221
return { route, router }
232222
}
233223
```
234224

235-
Now we can use it inside the setup() method of our component:
225+
Now we can use useRouter inside the setup() method of our component:
236226

237227
```html
238228
// MyComponent.vue
@@ -256,86 +246,6 @@ Now we can use it inside the setup() method of our component:
256246
</script>
257247
```
258248

259-
Note that a drawback of this useRouter example is that it may be difficult to test since when we define `localVue`
260-
the `vm` instance is going to be different from the one we exported from `@src/main`.
261-
262-
To fix this we have to create a small runtime utility function:
263-
264-
```typescript
265-
// @src/runtimeHelper.ts
266-
const runtime = {};
267-
268-
export const runtimeHelper {
269-
set(vm) {
270-
runtime.vm = vm;
271-
},
272-
get() {
273-
if (runtime.vm) return runtime.vm;
274-
throw new ReferenceError("Vue instance not found.");
275-
}
276-
}
277-
```
278-
279-
Then we have to get the `vm` on runtime:
280-
281-
```typescript
282-
// @src/main.ts
283-
import Vue from 'vue'
284-
import VueCompositionAPI from '@src/api'
285-
import { runtimeHelper } from '@src/runtimeHelper'
286-
import App from '@src/App.vue'
287-
import router from '@src/router'
288-
289-
Vue.use(VueCompositionAPI)
290-
291-
new Vue({
292-
router,
293-
beforeCreate() {
294-
// Set the runtime so that we can access the vm from anywhere
295-
runtimeHelper.set(this)
296-
},
297-
render: h => h(App)
298-
}).$mount('#app')
299-
```
300-
301-
Now in the useRouter function we can get the `vm` on runtime:
302-
303-
```typescript
304-
// @src/useRouter.ts
305-
import { runtimeHelper } from '@src/runtimeHelper'
306-
307-
export function useRouter() {
308-
const vm = runtimeHelper.get()
309-
const route = vm.$route
310-
const router = vm.$router
311-
return { route, router }
312-
}
313-
```
314-
315-
Finally, we can test the router easily by passing the right `vm` on runtime:
316-
317-
```typescript
318-
// @src/mytest.ts
319-
import VueRouter from 'vue-router'
320-
import { createLocalVue, mount } from '@vue/test-utils'
321-
import VueCompositionAPI from '@src/api'
322-
import { runtimeHelper } from '@src/runtimeHelper'
323-
import router from '@src/router'
324-
325-
const localVue = createLocalVue()
326-
localVue.use(VueCompositionAPI)
327-
localVue.use(VueRouter)
328-
329-
mount(localVue.extend(ComponentHere), {
330-
localVue,
331-
router,
332-
beforeCreate() {
333-
// Set the runtime so that we can access the vm from anywhere
334-
runtimeHelper.set(this)
335-
}
336-
})
337-
```
338-
339249
</p></details>
340250

341251
## Inspiration

0 commit comments

Comments
 (0)