科技知識動態:Vue+Vux項目(詳細教程)

導讀跟大家講解下有關Vue+Vux項目(詳細教程),相信小伙伴們對這個話題應該也很關注吧,現在就為小伙伴們說說Vue+Vux項目(詳細教程),小編也

跟大家講解下有關Vue+Vux項目(詳細教程),相信小伙伴們對這個話題應該也很關注吧,現在就為小伙伴們說說Vue+Vux項目(詳細教程),小編也收集到了有關Vue+Vux項目(詳細教程)的相關資料,希望大家看到了會喜歡。

本文給大家分享一段詳細的代碼給大家介紹Vue+Vux項目實踐思路,需要的朋友可以參考下

提供完整的路由,services""""`

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

index.html

<!DOCTYPE html><html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0"> <title>insurance-weixin</title> </head> <body> <p id="app-box"></p> <!-- built files will be auto injected --> </body></html>

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

main.js

import Vue from 'vue'import Vuex from 'vuex'import VueRouter from 'vue-router'import FastClick from 'fastclick'import {WechatPlugin, AjaxPlugin, LoadingPlugin, ToastPlugin, AlertPlugin} from 'vux'import App from './app.vue'/** * 加載插件 */Vue.use(Vuex)Vue.use(VueRouter)Vue.use(WechatPlugin)Vue.use(AjaxPlugin)Vue.use(LoadingPlugin)Vue.use(ToastPlugin)Vue.use(AlertPlugin)/** * 定義常量 */const domainName = 'localhost:8010'const serverName = 'localhost:3000'const apiPrefix = serverName + '/api/outer'const loginTimeOutErrorCode = 'login_timeout_error'/** * 設置vuex */const store = new Vuex.Store({})store.registerModule('vux', { state: { loading: false, showBack: true, title: '' }, mutations: { updateLoading (state, loading) { state.loading = loading }, updateShowBack (state, showBack) { state.showBack = showBack }, updateTitle (state, title) { state.title = title } }})/** * 設置路由 */const routes = [ // 初始頁 { path: '/', component: function (resolve) { require(['./components/init.vue'], resolve) } }, // 主頁 { path: '/index', component: function (resolve) { require(['./components/index.vue'], resolve) }, children: [ // 測試頁 { path: 'test', component: function (resolve) { require(['./components/tests/page.vue'], resolve) } } ] }, // 綁定頁 { path: '/bind', component: function (resolve) { require(['./components/bind.vue'], resolve) } }]const router = new VueRouter({ routes})router.beforeEach(function (to, from, next) { store.commit('updateLoading', true) store.commit('updateShowBack', true) next()})router.afterEach(function (to) { store.commit('updateLoading', false)})/** * 點擊延遲 */FastClick.attach(document.body)/** * 日志輸出開關 */Vue.config.productionTip = true/** * 定義全局公用常量 */Vue.prototype.domainName = domainNameVue.prototype.serverName = serverNameVue.prototype.apiPrefix = apiPrefix/** * 定義全局公用方法 */Vue.prototype.http = function (opts) { let vue = this vue.$vux.loading.show({ text: 'Loading' }) vue.$http({ method: opts.method, url: apiPrefix + opts.url, headers: opts.headers || {}, params: opts.params || {}, data: opts.data || {} }).then(function (response) { vue.$vux.loading.hide() opts.success(response.data.data) }).catch(function (error) { vue.$vux.loading.hide() if (!opts.error) { let response = error.response let errorMessage = '請求失敗' if (response && response.data) { if (response.data.code === loginTimeOutErrorCode) { window.location.href = '/' } errorMessage = response.data.message } vue.$vux.alert.show({ title: '提示', content: errorMessage }) } else { opts.error(error.response.data.data) } })}Vue.prototype.get = function (opts) { opts.method = 'get' this.http(opts)}Vue.prototype.post = function (opts) { opts.method = 'post' this.http(opts)}Vue.prototype.put = function (opts) { opts.method = 'put' this.http(opts)}Vue.prototype.delete = function (opts) { opts.method = 'delete' this.http(opts)}Vue.prototype.valid = function (opts) { let vue = this let valid = true if (opts.ref && !opts.ref.valid) { valid = false } if (opts.ignoreRefs) { let newRefs = [] for (let i in opts.refs) { let ref = opts.refs[i] for (let j in opts.ignoreRefs) { let ignoreRef = opts.ignoreRefs[j] if (ref !== ignoreRef) { newRefs.push(ref) } } } opts.refs = newRefs } for (let i in opts.refs) { if (!opts.refs[i].valid) { valid = false break } } if (valid) { opts.success() } else if (opts.error) { opts.error() } else { vue.$vux.toast.show({ text: '請檢查輸入' }) }}Vue.prototype.closeShowBack = function () { this.$store.commit('updateShowBack', false)}Vue.prototype.updateTitle = function (value) { this.$store.commit('updateTitle', value)}/** * 創建實例 */new Vue({ store, router, render: h => h(App)}).$mount('#app-box')app.vue<template> <p id="app"> <loading v-model="isLoading"></loading> <transition> <router-view></router-view> </transition> </p></template><script> import {Loading} from 'vux' import {mapState} from 'vuex' export default { name: 'app', components: { Loading }, computed: { ...mapState({ isLoading: state => state.vux.isLoading }) } }</script><style lang="less"> @import '~vux/src/styles/reset.less'; body { background-color: #fbf9fe; }</style>components/index.vue<template> <p style="height:100%;"> <top style="margin-bottom:46px"></top> <transition> <router-view></router-view> </transition> <bottom></bottom> </p></template><script> import Top from './layouts/top.vue' import Bottom from './layouts/bottom.vue' export default { components: { Top, Bottom } }</script><style> html, body { height: 100%; width: 100%; overflow-x: hidden; }</style>components/tests/page.vue<template> <p> <page @loadMore="loadMore" @refresh="refresh"> <p> <p v-for="i in n">placeholder {{i}}</p> </p> </page> </p></template><script> import Page from '../kits/page.vue' import {cookie} from 'vux' export default { components: { Page }, created () { let vue = this vue.closeShowBack() vue.updateTitle('測試頁面'), //獲取常量 console.log(0) vue.get({ url: '/test/constants', headers: { 'token': cookie.get('token') }, success: function (data) { cookie.set('constants',JSON.stringify(data),{ expires: 1 }) } }) }, data () { return { n: 10, } }, methods: { loadMore () { this.n += 10 }, refresh () { this.n = 10 }, } }</script>

components/tests/page.vue代碼中的 import Page from '../kits/page.vue'是我自己寫的下拉刷新上啦加在的組件,運行的話刪掉這些引用就可以了。

本次記錄摘要是從剛剛完成的項目中抽離的部分代碼(注:本項目實踐代碼,可運行,可運行,可運行,可運行)

上面是我整理給大家的,希望今后會對大家有幫助。

相關文章:

在微信小程序中有關功能函數總結(詳細教程)

在javaScript中如何使用手機號碼校驗工具類PhoneUtils

在微信小程序中如何實現下載進度條

在微信小程序中如何使用video組件播放視頻

以上就是Vue+Vux項目(詳細教程)的詳細內容,更多請關注php中文網其它相關文章!

來源:php中文網

免責聲明:本文由用戶上傳,如有侵權請聯系刪除!