Skip to content

Commit ad6618f

Browse files
committed
全局设置router和route
1 parent 368c83d commit ad6618f

File tree

9 files changed

+230
-0
lines changed

9 files changed

+230
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"vite-plugin-dts": "^4.3.0",
5959
"vitest": "^2.1.5",
6060
"vue": "^3.5.13",
61+
"vue-router": "^4.4.5",
6162
"vue-tsc": "^2.1.10"
6263
},
6364
"peerDependencies": {

pnpm-lock.yaml

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/test11/AboutView.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<div>AboutView</div>
3+
</template>

tests/test11/DemoComp.vue

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<script setup lang="ts">
2+
import {
3+
RouterLink,
4+
RouterView,
5+
useRoute,
6+
useRouter,
7+
Router,
8+
RouteLocationNormalizedLoaded,
9+
} from 'vue-router';
10+
import { DemoService } from './DemoService';
11+
import { declareProviders, useService } from '../../src/index';
12+
import { TYPES } from './router';
13+
14+
defineProps({
15+
msg: String,
16+
});
17+
18+
declareProviders([DemoService]);
19+
const service = useService(DemoService);
20+
const route = useRoute();
21+
const router = useRouter();
22+
const rootRoute = useService<RouteLocationNormalizedLoaded>(TYPES.route);
23+
const rootRouter = useService<Router>(TYPES.router);
24+
25+
defineExpose({
26+
service,
27+
route,
28+
router,
29+
rootRoute,
30+
rootRouter,
31+
});
32+
</script>
33+
34+
<template>
35+
<div>
36+
<div>
37+
<div class="msg">{{ msg }}</div>
38+
<div class="count">{{ service.count }}</div>
39+
40+
<button type="button" class="btn-count" @click="service.increaseCount()">
41+
Add count
42+
</button>
43+
</div>
44+
45+
<div class="fullpath1">{{ route.fullPath }}</div>
46+
<div class="fullpath2">{{ rootRoute.fullPath }}</div>
47+
<div class="fullpath3">{{ service.route.fullPath }}</div>
48+
<div class="fullpath4">{{ service.router.currentRoute.value.fullPath }}</div>
49+
50+
<nav>
51+
<RouterLink class="route-home" to="/">Go to Home</RouterLink>
52+
<RouterLink class="route-about" to="/about">Go to About</RouterLink>
53+
</nav>
54+
55+
<main class="main-content">
56+
<RouterView />
57+
</main>
58+
</div>
59+
</template>

tests/test11/DemoService.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { inject } from 'inversify';
2+
import { TYPES } from './router';
3+
4+
export class DemoService {
5+
public count = 1;
6+
7+
public increaseCount() {
8+
this.count++;
9+
}
10+
11+
@inject(TYPES.route)
12+
public route: any;
13+
14+
@inject(TYPES.router)
15+
public router: any;
16+
}

tests/test11/HomeView.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<div>HomeView</div>
3+
</template>

tests/test11/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 测试场景-当前组件访问当前组件的服务
2+
3+
主要测试了 declareRootProviders 配置全局vue router 和 vue route

tests/test11/demo.test.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import 'reflect-metadata';
2+
import { flushPromises, mount } from '@vue/test-utils';
3+
import DemoComp from './DemoComp.vue';
4+
import { DemoService } from './DemoService';
5+
import { router, TYPES } from './router';
6+
import { declareRootProviders, useRootService } from '../../src';
7+
import { markRaw, App } from 'vue';
8+
import { useRoute } from 'vue-router';
9+
10+
describe('test11', () => {
11+
it('get DemoService instance', async () => {
12+
declareRootProviders(container => {
13+
container
14+
.bind(TYPES.router)
15+
.toConstantValue(router)
16+
.onActivation((_: any, instance: any) => {
17+
return markRaw(instance);
18+
});
19+
});
20+
21+
router.push('/');
22+
await router.isReady();
23+
24+
const msg = 'Hello world';
25+
const wrapper = mount(DemoComp, {
26+
props: {
27+
msg,
28+
},
29+
global: {
30+
plugins: [
31+
router,
32+
(app: App) => {
33+
app.runWithContext(() => {
34+
const route = useRoute();
35+
declareRootProviders(container => {
36+
container
37+
.bind(TYPES.route)
38+
.toConstantValue(route)
39+
.onActivation((_: any, instance: any) => {
40+
return markRaw(instance);
41+
});
42+
});
43+
});
44+
},
45+
],
46+
},
47+
});
48+
49+
const rootRoute = useRootService(TYPES.route);
50+
const rootRouter = useRootService(TYPES.router);
51+
52+
expect(wrapper.vm.service).toBeInstanceOf(DemoService);
53+
expect(router).toBe(rootRouter);
54+
expect(router).toBe(wrapper.vm.router);
55+
expect(router).toBe(wrapper.vm.rootRouter);
56+
expect(router).toBe(wrapper.vm.service.router);
57+
expect(rootRoute).toBe(wrapper.vm.rootRoute);
58+
expect(rootRouter).toBe(wrapper.vm.rootRouter);
59+
expect(rootRoute).toBe(wrapper.vm.service.route);
60+
expect(rootRouter).toBe(wrapper.vm.service.router);
61+
62+
expect(wrapper.get('.msg').text()).toBe(msg);
63+
expect(wrapper.get('.count').text()).toBe('1');
64+
expect(wrapper.get('.fullpath1').text()).toBe('/');
65+
expect(wrapper.get('.fullpath2').text()).toBe('/');
66+
expect(wrapper.get('.fullpath3').text()).toBe('/');
67+
expect(wrapper.get('.fullpath4').text()).toBe('/');
68+
expect(wrapper.get('.main-content').text()).toBe('HomeView');
69+
70+
await wrapper.get('.btn-count').trigger('click');
71+
expect(wrapper.get('.msg').text()).toBe(msg);
72+
expect(wrapper.get('.count').text()).toBe('2');
73+
expect(wrapper.get('.fullpath1').text()).toBe('/');
74+
expect(wrapper.get('.fullpath2').text()).toBe('/');
75+
expect(wrapper.get('.fullpath3').text()).toBe('/');
76+
expect(wrapper.get('.fullpath4').text()).toBe('/');
77+
expect(wrapper.get('.main-content').text()).toBe('HomeView');
78+
79+
await wrapper.get('.btn-count').trigger('click');
80+
expect(wrapper.get('.msg').text()).toBe(msg);
81+
expect(wrapper.get('.count').text()).toBe('3');
82+
expect(wrapper.get('.fullpath1').text()).toBe('/');
83+
expect(wrapper.get('.fullpath2').text()).toBe('/');
84+
expect(wrapper.get('.fullpath3').text()).toBe('/');
85+
expect(wrapper.get('.fullpath4').text()).toBe('/');
86+
expect(wrapper.get('.main-content').text()).toBe('HomeView');
87+
88+
await wrapper.get('.route-about').trigger('click');
89+
await flushPromises();
90+
expect(wrapper.get('.msg').text()).toBe(msg);
91+
expect(wrapper.get('.count').text()).toBe('3');
92+
expect(wrapper.get('.fullpath1').text()).toBe('/about');
93+
expect(wrapper.get('.fullpath2').text()).toBe('/about');
94+
expect(wrapper.get('.fullpath3').text()).toBe('/about');
95+
expect(wrapper.get('.fullpath4').text()).toBe('/about');
96+
expect(wrapper.get('.main-content').text()).toBe('AboutView');
97+
98+
await wrapper.get('.route-home').trigger('click');
99+
await flushPromises();
100+
expect(wrapper.get('.msg').text()).toBe(msg);
101+
expect(wrapper.get('.count').text()).toBe('3');
102+
expect(wrapper.get('.fullpath1').text()).toBe('/');
103+
expect(wrapper.get('.fullpath2').text()).toBe('/');
104+
expect(wrapper.get('.fullpath3').text()).toBe('/');
105+
expect(wrapper.get('.fullpath4').text()).toBe('/');
106+
expect(wrapper.get('.main-content').text()).toBe('HomeView');
107+
});
108+
});

tests/test11/router.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { createWebHistory, createRouter } from 'vue-router';
2+
3+
import HomeView from './HomeView.vue';
4+
import AboutView from './AboutView.vue';
5+
6+
const routes = [
7+
{ path: '/', component: HomeView },
8+
{ path: '/about', component: AboutView },
9+
];
10+
11+
export const router = createRouter({
12+
history: createWebHistory(),
13+
routes,
14+
});
15+
16+
export const TYPES = {
17+
route: Symbol('route001'),
18+
router: Symbol('router001'),
19+
};

0 commit comments

Comments
 (0)