Vue技术栈开发实战(一)——自定义项目目录结构和跨域配置

使用Vue UI创建、管理项目

vue ui   // 启动Vue UI

项目结构目录

mock中的index.js

import Mock from 'mockjs'

export default Mock

安装mockjs开发依赖

npm i mockjs -D

router中的index.js

import Vue from 'vue'
import Router from 'vue-router'

import routes from './router'

Vue.use(Router)

export default new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router中的router.js

import Home from '../views/Home.vue'

export default [
  {
    path: '/',
    name: 'home',
    component: Home
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  }
]

store中的index.js

import Vue from 'vue'
import Vuex from 'vuex'
import state from './state'
import mutations from './mutations'
import actions from './actions'
import user from './module/user'

Vue.use(Vuex)

export default new Vuex.Store({
  state,
  mutations,
  actions,
  modules: {
    user
  }
})

state.js,mutations.js,actions.js内容

export default {}

store中的module中的user.js

const state = {}

const mutations = {}

const actions = {}

export default {
  state,
  mutations,
  actions
}

然后要改一下main.js中的引入,默认会引入文件夹下的index.js文件,所以可以省略

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')

最后新建vue.config.js文件,进行基本配置和跨域配置

const path = require('path')
const resolve = dir => path.join(__dirname, dir)

// 如果是生产环境,路径就要指定到域名的/iview-admin/下,如果是开发环境就只要/
const BASE_URL = process.env.NODE_ENV === 'prodution' ? '/iview-admin/' : '/'

module.exports = {
  publicPath: BASE_URL,
  chainWebpack: config => {
    config.resolve.alias
      .set('@', resolve('src'))
      .set('_c', resolve('src/components'))
  },
  // 打包时不生成.map文件,减少打包后的体积
  productionSourceMap: false,
  // 会告诉服务器将没有匹配到静态文件的请求代理到http://localhost:8080来满足跨域的需求
  devServer: {
    proxy: 'http://localhost:8080'
  }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风里有诗句哈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值