diff --git a/demo/js/main-for-vue3.js b/demo/js/main-for-vue3.js index a1d32ba..f50e92c 100644 --- a/demo/js/main-for-vue3.js +++ b/demo/js/main-for-vue3.js @@ -1,33 +1,14 @@ +/** + * 导入需要用到的组件 + */ const { createRouter, createWebHashHistory } = VueRouter; const { createApp, defineComponent, getCurrentInstance, ref } = Vue; -// 定义路由信息 -const routes = [ - { - path: '/', - redirect: '/page1' - }, - { - path: '/page1', - component: { - template: '
当前是 Page1 的路由
' - } - }, - { - path: '/page2', - component: { - template: '
当前是 Page2 的路由
' - } - }, - { - path: '/page3', - component: { - template: '
当前是 Page3 的路由
' - } - } -]; -// 初始化路由 +/** + * 初始化路由 + * routes是来自 js/routes.js 里面的配置 + */ const router = createRouter({ history: createWebHashHistory(), routes, @@ -35,39 +16,38 @@ const router = createRouter({ linkExactActiveClass: 'cur' }); -// 初始化Vue -createApp({}) - .use(router) - .use(baiduAnalytics, { - router: router, - siteIdList: [ - '8dca8e2532df48ea7f1b15c714588691' - ], - isDebug: true - }) - .mount('#app'); -const app = defineComponent({ +/** + * 创建实例 + */ +const app = { setup () { - // 数据 + // 插件要用到的一个代理组件 + const { proxy } = getCurrentInstance(); + + // 初始化要用到的数据 const pageUrl = ref(''); const category = ref(''); const action = ref(''); const label = ref(''); const value = ref(''); - const { proxy } = getCurrentInstance(); - - console.log('setup'); - console.log(pageUrl); + // 提交pv的操作 const pv = () => { - pushBAIDU.pv('/') + proxy.$pushBAIDU.pv(pageUrl.value); } - const pv2 = () => { - proxy.$pushBAIDU.pv('/2'); + // 提交事件的操作 + const event = () => { + proxy.$pushBAIDU.event( + category.value, + action.value, + label.value, + value.value + ); } + // Vue 3.0 需要把模板要用到的东西 return 出去 return { // 数据 pageUrl, @@ -78,33 +58,29 @@ const app = defineComponent({ // 方法 pv, - pv2 + event } } -}) +}; -// // 初始化Vue -// const app = new Vue({ -// el: '#app', -// router, -// data () { -// return { -// pageUrl: '', -// category: '', -// action: '', -// label: '', -// value: '' -// } -// }, -// mounted () { -// }, -// methods: { -// pv () { -// // this.$pushBAIDU.pv(this.pageUrl); -// console.log(baiduAnalytics.pushBAIDU); -// }, -// event () { -// this.$pushBAIDU.event(this.category, this.action, this.label, this.value); -// } -// } -// }); \ No newline at end of file + +/** + * 初始化Vue + */ +createApp(app) + // 启动路由 + .use(router) + + // 启动插件 + .use(baiduAnalytics, { + router: router, + siteIdList: [ + 'aaaaaaaaaaaaaaaaaaa', + 'bbbbbbbbbbbbbbbbbbb', + 'ccccccccccccccccccc' + ], + isDebug: true + }) + + // 挂载到节点上 + .mount('#app'); \ No newline at end of file diff --git a/demo/js/routes.js b/demo/js/routes.js new file mode 100644 index 0000000..f39c2b5 --- /dev/null +++ b/demo/js/routes.js @@ -0,0 +1,27 @@ +/** + * 定义路由信息 + */ +const routes = [ + { + path: '/', + redirect: '/page1' + }, + { + path: '/page1', + component: { + template: '
当前是 Page1 的路由
' + } + }, + { + path: '/page2', + component: { + template: '
当前是 Page2 的路由
' + } + }, + { + path: '/page3', + component: { + template: '
当前是 Page3 的路由
' + } + } +]; \ No newline at end of file diff --git a/demo/vue3.html b/demo/vue3.html index 5007a91..5ef161d 100644 --- a/demo/vue3.html +++ b/demo/vue3.html @@ -4,8 +4,8 @@ [Vue3] vue baidu analytics demo - - + + @@ -55,6 +55,7 @@ +