Home  >  Article  >  Web Front-end  >  Vue+Vux project practice complete code tutorial

Vue+Vux project practice complete code tutorial

小云云
小云云Original
2018-05-19 10:26:186599browse

This article will share with you a detailed code to introduce the practical ideas of the Vue+Vux project. Friends in need can refer to it. I hope it can help everyone.

Provide complete routing, services""""`

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

index.html



 
  
  
  insurance-weixin
 
 
  

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

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 = domainName
Vue.prototype.serverName = serverName
Vue.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



components/index.vue



components/tests/page.vue

The import Page from '../kits/page.vue' in the components/tests/page.vue code is a component I wrote myself to add to the pull-down refresh. , just delete these references when running.

The summary of this record is part of the code extracted from the just completed project (Note: The practical code of this project is runnable, runnable, runnable, runnable)

Related recommendations:

How to use calculated properties in vue

The specific implementation of Vue loading on demand

Detailed explanation of the code method of adding vux to vue

The above is the detailed content of Vue+Vux project practice complete code tutorial. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn