vue2 + router + vuex + vux + axios 开发的一点总计

建议一般单页面应用架构模式:vue2 + router + vuex + vux + axios

 

其中涉及很多npm的安装可以百度一下

 

路由配置

 

 

vuex ,小项目可以放弃模块store,模块store有命名空间的概念

 

 

main.js

 

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import store from './store/index'
import router from './router'
import  {AlertPlugin, LoadingPlugin, Confirm} from 'vux'

Vue.use(AlertPlugin)
Vue.use(LoadingPlugin)
Vue.use(Confirm)

Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
  el: '#app',
  store,
  router,
  created: function () {
    var docEl = document.documentElement
    var clientWidth = docEl.clientWidth
    if (!clientWidth) return
    docEl.style.fontSize = 20 * (clientWidth / 320) + 'px'
    // 判断是否在安卓和ios运行
    var u = navigator.userAgent
    if (!(u.indexOf('Android') > -1 || u.indexOf('Adr') > -1)) {
      if (!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
        docEl.style.fontSize = 40 + 'px'
      }
    }
  },
  // template: '<App/>',
  // components: { App },
  render: h => h(App)
})

 

 

 

config文件夹 注意是公共配置文件 可以注入vuex全局使用,模块的小项目直接data()使用就够

 

工具文件夹

 

 

notice.js封装vux 弹窗

 

import Vue from 'vue'

const notify = {
  alert: (title = '提示', content, showFn, hideFn) => {
    return Vue.$vux.alert.show({
      title: title,
      content: content,
      onShow () {
        if (typeof showFn === 'function') {
          showFn ()
        }
      },
      onHide () {
        if (typeof hideFn === 'function') {
          hideFn ()
        }
      }
    })
  },
  loading: (text = 'Loading') => {
    return Vue.$vux.loading.show({
      text: text
    })
  },
  cloLoading: () => {
    Vue.$vux.loading.hide()
  }
}

export default notify;

 

 

 

request.js封装axios的ajax请求

 

import axios from 'axios'
import qs from 'qs'
import config from '../config/config'
// import util from './util'

axios.interceptors.request.use(config => {
  return config
}, error => {
  return Promise.reject(error)
})

axios.interceptors.response.use(response => {
  return response
}, error => {
  return Promise.resolve(error.response)
})

export default {
  post (method, data = {}) {
    data.buId = '00000000-0000-0000-0000-000000000000'
    data.operatorId = '398c2e79-8816-4ef5-891c-b2ae2a8bd904'
    data.token = '00000000-0000-0000-0000-000000000000'
    data.method = method
    return axios({
      method: 'post',
      url: config.getHost() + config.getApi(),
      data: qs.stringify(data),
      timeout: 5000,
      headers: {
        // 'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
      }
    }).then((response) => {
      // util.consoleGroup(method, [response.data])
      return response.data
    }).then((res) => {
      return res
    }).catch((res) => {
      console.log(res)
    })
  },
  get (method, params = {}) {
    params.method = method
    return axios({
      method: 'get',
      url: config.getApi(),
      // get 请求时带的参数
      params,
      timeout: 5000,
      headers: {
        // 'X-Requested-With': 'XMLHttpRequest'
      }
    }).then((response) => {
      // util.consoleGroup(method, [response.data])
      return response.data
    }).then((res) => {
      return res
    }).catch((res) => {
      console.log(res)
    })
  },
  all (req) {
    return axios.all(req)
    .then((response) => {
      console.log(response)
    }).then((res) => {
      console.log(res)
    })
  }
}

 

 

 

util.js 公共处理函数封装

 

掉过的坑:

图片的引入问题,template子件文件直接因为文件会报路径错误,可以写在data () 用request引入,我写在less单独文件中不出现这个问题

打包发布问题,打包后index.html引入报错问题,在webpack.prod.conf.js 文件找到output

属性增加

publicPath: './'

 

好的,这是一个比较广泛的问题,我会尽可能详细地回答。 首先,我们需要创建一个基本的 Vue.js 应用程序,并安装所需的依赖项。我们可以使用 Vue CLI 来创建应用程序,它会自动配置我们需要的大部分内容,如路由和状态管理。 ``` vue create my-app ``` 接下来,我们需要安装 vue-routervuex,这两个库分别用于路由和状态管理。 ``` npm install vue-router vuex ``` 接着,我们可以创建一个名为“contacts”的组件,用于显示我们的通讯录。这个组件将包含一个表格,用于显示我们的联系人列表。我们还需要一个名为“contact”的组件,用于编辑单个联系人的详细信息。 接下来,我们可以在我们的应用程序中配置路由。我们需要定义两个路由,一个用于显示联系人列表,另一个用于编辑单个联系人的详细信息。 ```javascript import Vue from 'vue' import VueRouter from 'vue-router' import Contacts from './components/Contacts.vue' import Contact from './components/Contact.vue' Vue.use(VueRouter) const routes = [ { path: '/', component: Contacts }, { path: '/contact/:id', component: Contact } ] const router = new VueRouter({ routes }) export default router ``` 在这里,我们使用 VueRouter 创建了两个路由。一个路由用于显示联系人列表,另一个路由用于编辑单个联系人的详细信息。我们使用“:id”来指定要编辑的联系人的 ID。这个参数将在我们的组件中使用。 接下来,我们可以创建一个名为“contacts”的 Vuex 模块,用于管理我们的通讯录。这个模块将包含我们的联系人列表,并提供用于添加、编辑和删除联系人的方法。 ```javascript const state = { contacts: [] } const mutations = { addContact (state, contact) { state.contacts.push(contact) }, updateContact (state, contact) { const index = state.contacts.findIndex(c => c.id === contact.id) state.contacts.splice(index, 1, contact) }, deleteContact (state, contact) { const index = state.contacts.findIndex(c => c.id === contact.id) state.contacts.splice(index, 1) } } const actions = { addContact ({ commit }, contact) { commit('addContact', contact) }, updateContact ({ commit }, contact) { commit('updateContact', contact) }, deleteContact ({ commit }, contact) { commit('deleteContact', contact) } } export default { state, mutations, actions } ``` 在这里,我们定义了三个 mutation,用于添加、更新和删除联系人。我们还定义了三个 action,用于触发这些 mutation。 最后,我们可以使用 Axios 库从后端 API 获取我们的联系人数据。我们可以在组件的 created 钩子中发出 GET 请求,并将结果存储在我们的 Vuex store 中。 ```javascript import axios from 'axios' export default { created () { axios.get('/api/contacts') .then(response => { this.$store.dispatch('addContact', response.data) }) .catch(error => { console.log(error) }) } } ``` 在这里,我们使用 Axios 发出 GET 请求,并在响应中获取联系人数据。然后,我们调用我们的 Vuex action,将联系人添加到我们的 store 中。 最终,我们将所有这些部分组合在一起,使用我们的通讯录应用程序。我们可以像这样在我们的 App.vue 文件中导入我们的组件和路由: ```javascript <template> <div id="app"> <router-view></router-view> </div> </template> <script> import router from './router' export default { name: 'app', router } </script> ``` 在这里,我们将我们的路由器导入到我们的应用程序中,并在模板中使用<router-view>来显示当前路由。 这就是用 Vue 组件、Vue 路由、VuexAxios 实现通讯录的基本步骤。当然,这只是一个基本的示例,您可能需要根据您的具体需求进行更改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值