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

Commit d315000

Browse files
authored
Merge pull request #6 from chengpeiquan/develop
Develop
2 parents 377337d + 4aff1f5 commit d315000

9 files changed

Lines changed: 64 additions & 13 deletions

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ npm install vue-baidu-analytics --save-dev
4646
参数|是否必填|参数类型|参数说明
4747
:-:|:-:|:-:|-
4848
router|是|object|Vue Router,本插件基于路由使用
49-
siteIdList|是|object Array|百度统计的站点id列表,item为站点id<br>只有一个站点需要上报就保留一个item即可
49+
siteIdList|是|string[]|百度统计的站点id列表,item为站点id<br>只有一个站点需要上报就保留一个item即可
5050
isDebug|否|boolean|是否开启debug模式,默认 `false`<br>开启后会在控制台打印上报信息,**上线前记得关闭**
5151

5252
## 使用
@@ -97,7 +97,7 @@ createApp(app)
9797
'bbbbbbbbbbbbbbbbbbb',
9898
'ccccccccccccccccccc'
9999
],
100-
isDebug: true
100+
isDebug: false
101101
})
102102

103103
// 挂载到节点上

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-baidu-analytics.js

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

dist/vue-baidu-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-baidu-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-baidu-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-baidu-analytics",
3-
"version": "2.0.1",
3+
"version": "2.0.2",
44
"description": "A data collection tool that supports reporting of single-page application data built by Vue 3.0 & 2.0 & VuePress, based on baidu statistics.",
55
"main": "dist/vue-baidu-analytics.min.js",
66
"types": "vue-baidu-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 PushBAIDU from '@m/pushBAIDU'
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('/');
5660

61+
// 根据路由模式生成要上报的链接
62+
const PAGE_URL: string = ROUTER_MODE === 'hash' ? `${PAGE_PATH}/#${to.fullPath}` : `${PAGE_PATH}${to.fullPath}`;
63+
64+
// 上报数据
5765
pushBAIDU.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)