项目的百度埋点需要做路由拦截,找了个路由插件 uni-simple-router
。下面是按照和基本使用步骤:
安装 uni-simple-router 插件
如果是用 hbuilderX 创建的项目,就在插件库中找。
如果是用 cli 创建的项目就如下操作:
1
| npm install uni-simple-router uni-read-pages
|
App 下 main.js 中引用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| import Router, { RouterMount } from 'uni-simple-router';
Vue.use(Router); const router = new Router({ routes: ROUTES, h5: { loading: false }, routerBeforeEach(e) {}, routerAfterEach(e) {} });
router.beforeEach((to, from, next) => { next(); });
router.afterEach((to, from) => { if (to.meta && to.meta.isBaiduCount) { Vue.prototype.$util.baiduPageView(to.path); } });
const app = new Vue({ store, ...App });
RouterMount(app, '#app');
app.$mount();
|
配置 vue.config.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| const TransformPages = require('uni-read-pages'); const tfPages = new TransformPages({ includes: ['path', 'style', 'meta'] }); module.exports = { productionSourceMap: false, configureWebpack: config => { return { plugins: [ new tfPages.webpack.DefinePlugin({ ROUTES: JSON.stringify(tfPages.routes) }) ] }; } };
|
对应 pages.json 添加 meta
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| { "pages": [{ "path": "pages/homePage/homePage", "style": { "navigationBarTitleText": "首页", "navigationStyle": "custom" }, "meta": { "isBaiduCount": true } }, ... ], }
|
参考