Skip to content
This repository was archived by the owner on Jun 23, 2023. It is now read-only.

Commit 536a613

Browse files
committed
Release v2.0.1
1 parent ca3c8f6 commit 536a613

File tree

8 files changed

+61
-10
lines changed

8 files changed

+61
-10
lines changed

dist/modules/getRouterMode.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
declare const getRouterMode: (vueVersion: number, router: any) => string;
2+
export default getRouterMode;

dist/vue-cnzz-analytics.js

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

dist/vue-cnzz-analytics.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/vue-cnzz-analytics.min.js

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

dist/vue-cnzz-analytics.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-cnzz-analytics",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "A data collection tool that supports reporting of single-page application data built by Vue 3.0 & 2.0 & VuePress, based on cnzz statistics.",
55
"main": "dist/vue-cnzz-analytics.min.js",
66
"types": "vue-cnzz-analytics.d.ts",

src/main.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import PushCNZZ from '@m/pushCNZZ'
22
import getVueVersion from '@m/getVueVersion'
3+
import getRouterMode from '@m/getRouterMode'
34

45
/**
56
* 定义插件
@@ -50,10 +51,17 @@ export default function install (Vue: Vue, { router, siteIdList, isDebug = false
5051
* 路由切换时执行PV上报
5152
*/
5253
router.afterEach( (to: To) => {
53-
const PAGE_PATH_DIR_COUNT = window.location.pathname.split('/').length;
54-
const PAGE_PATH = window.location.pathname.split('/').slice(0, PAGE_PATH_DIR_COUNT - 1).join('/');
55-
const PAGE_URL = router.mode === 'hash' ? `${PAGE_PATH}/#${to.fullPath}` : `${PAGE_PATH}${to.fullPath}`;
54+
// 根据Vue版本获取路由模式
55+
const ROUTER_MODE: string = getRouterMode(VUE_VERSION, router);
56+
57+
// 获取页面的url信息
58+
const PAGE_PATH_DIR_COUNT: number = window.location.pathname.split('/').length;
59+
const PAGE_PATH: string = window.location.pathname.split('/').slice(0, PAGE_PATH_DIR_COUNT - 1).join('/');
60+
61+
// 根据路由模式生成要上报的链接
62+
const PAGE_URL: string = ROUTER_MODE === 'hash' ? `${PAGE_PATH}/#${to.fullPath}` : `${PAGE_PATH}${to.fullPath}`;
5663

64+
// 上报数据
5765
pushCNZZ.pv(PAGE_URL);
5866
});
5967
}

src/modules/getRouterMode.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 获取Vue的版本
3+
* @param {number} vueVersion - vue版本号,2=Vue2.x, 3=Vue3.x
4+
* @param {object} router - vue路由
5+
* @return hash=hash模式、history=history模式
6+
*/
7+
const getRouterMode = (vueVersion: number, router: any): string => {
8+
let mode: string = 'history';
9+
10+
// 2.x直接读取mode即可
11+
if ( vueVersion === 2 ) {
12+
mode = router.mode;
13+
}
14+
15+
// 3.x需要判断一下
16+
if ( vueVersion === 3 ) {
17+
const BASE: string = router.options.history.base || '';
18+
if ( BASE.includes('#') ) {
19+
mode = 'hash';
20+
}
21+
}
22+
23+
return mode;
24+
}
25+
26+
export default getRouterMode;

0 commit comments

Comments
 (0)