挂载全局app 让api与小程序一致
Showing
6 changed files
with
41 additions
and
30 deletions
| 1 | import Vue from 'vue' | 1 | import Vue from 'vue' | 
| 2 | import App from './App.vue' | 2 | import VueApp from './App.vue' | 
| 3 | import router from './router' | 3 | import router from './router' | 
| 4 | import store from './store' | 4 | import store from './store' | 
| 5 | 5 | ||
| 6 | import api from '@/api/api' | ||
| 7 | import { | ||
| 8 | httpGet, | ||
| 9 | httpPost | ||
| 10 | } from '@/api/fetch-api.js' | ||
| 11 | |||
| 6 | // import Mock from './mock' | 12 | // import Mock from './mock' | 
| 7 | // Mock.bootstrap(); | 13 | // Mock.bootstrap(); | 
| 8 | 14 | ||
| ... | @@ -44,11 +50,21 @@ Vue.use(Swipe).use(SwipeItem) | ... | @@ -44,11 +50,21 @@ Vue.use(Swipe).use(SwipeItem) | 
| 44 | .use(Button) | 50 | .use(Button) | 
| 45 | .use(Toast); | 51 | .use(Toast); | 
| 46 | 52 | ||
| 47 | new Vue({ | 53 | let app = new Vue({ | 
| 48 | router, | 54 | router, | 
| 49 | store, | 55 | store, | 
| 50 | data: { | 56 | data: { | 
| 51 | isInit: false, | 57 | isInit: false, | 
| 52 | }, | 58 | }, | 
| 53 | render: h => h(App) | 59 | render: h => h(VueApp) | 
| 54 | }).$mount('#app') | 60 | }).$mount('#app') | 
| 61 | |||
| 62 | // 挂载全局app | ||
| 63 | app.api = api; | ||
| 64 | app.get = httpGet; | ||
| 65 | app.post = httpPost; | ||
| 66 | app.router = router; | ||
| 67 | window.app = app; | ||
| 68 | |||
| 69 | // 原app节点请通过 document.getElementById('app') 获取 | ||
| 70 | // routes里的component组件需要以import方式引入,否则页面创建的时候,app还没挂载。 | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... | 
| 1 | import api from '../../api/api' | ||
| 2 | import { | ||
| 3 | httpGet, | ||
| 4 | httpPost | ||
| 5 | } from '../../api/fetch-api.js' | ||
| 6 | |||
| 7 | export default { | 1 | export default { | 
| 8 | data() { | 2 | data() { | 
| 9 | return { | 3 | return { | 
| ... | @@ -12,12 +6,12 @@ export default { | ... | @@ -12,12 +6,12 @@ export default { | 
| 12 | }, | 6 | }, | 
| 13 | components: {}, | 7 | components: {}, | 
| 14 | methods: { | 8 | methods: { | 
| 15 | initData() {}, | ||
| 16 | toLottery() { | 9 | toLottery() { | 
| 17 | this.$router.push({ | 10 | app.router.push({ | 
| 18 | path: "/lottery" | 11 | name: "lottery" | 
| 19 | }) | 12 | }); | 
| 20 | } | 13 | }, | 
| 14 | initData() {}, | ||
| 21 | }, | 15 | }, | 
| 22 | mounted() {}, | 16 | mounted() {}, | 
| 23 | created() { | 17 | created() { | ... | ... | 
| 1 | import api from '../../api/api' | ||
| 2 | import { | ||
| 3 | httpGet, | ||
| 4 | httpPost | ||
| 5 | } from '../../api/fetch-api.js' | ||
| 6 | |||
| 7 | export default { | 1 | export default { | 
| 8 | data() { | 2 | data() { | 
| 9 | return { | 3 | return { | 
| ... | @@ -18,7 +12,18 @@ export default { | ... | @@ -18,7 +12,18 @@ export default { | 
| 18 | path: "/" | 12 | path: "/" | 
| 19 | }) | 13 | }) | 
| 20 | }, | 14 | }, | 
| 15 | async queryArea() { | ||
| 16 | let res = await app.post({ | ||
| 17 | url: app.api.areaQuery, | ||
| 18 | data: {}, | ||
| 19 | sid: false, | ||
| 20 | mode: "custom" | ||
| 21 | }); | ||
| 22 | console.log("地区查询:", res); | ||
| 23 | } | ||
| 24 | }, | ||
| 25 | mounted() { | ||
| 26 | this.queryArea(); | ||
| 21 | }, | 27 | }, | 
| 22 | mounted() {}, | ||
| 23 | created() {} | 28 | created() {} | 
| 24 | } | 29 | } | 
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... | 
| 1 | import Vue from 'vue' | 1 | import Vue from 'vue' | 
| 2 | import Router from 'vue-router' | 2 | import Router from 'vue-router' | 
| 3 | import Index from './pages/index/index.vue' | ||
| 4 | 3 | ||
| 5 | Vue.use(Router) | 4 | Vue.use(Router) | 
| 6 | 5 | ||
| 7 | const routes = [{ | 6 | const routes = [{ | 
| 8 | path: '/', | 7 | path: '/', | 
| 9 | name: 'index', | 8 | name: 'index', | 
| 10 | component: Index, | 9 | component: () => import('./pages/index/index.vue'), | 
| 11 | meta: { | 10 | meta: { | 
| 12 | title: '' | 11 | title: '' | 
| 13 | } | 12 | } | ... | ... | 
- 
Please register or sign in to post a comment