vue

vue-cli2 构建项目【入坑指南】

2018-11-02  本文已影响107人  oo高学吟

构建项目

npm install vue
npm install -g vue/cli
vue init webpack my-Project 
npm install
npm run dev

配置

"scripts": {
    "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js --host 0.0.0.0", 
    //--host 0.0.0.0 支持用ip打开项目
    "start": "npm run dev",
    "build-prod": "node build/build.js prod", //用于打包dist
    "build-uat": "node build/build.js uat",
    "build-test": "node build/build.js test"
                                        ||
                                    process.argv[2]
  },
let envConfig = undefined;
let buildEnv = 'env';
if(process.argv.length>2){
  buildEnv = process.argv[2]
}
if(buildEnv=='prod'){
  envConfig = require('../config/prod.env')
}else if(buildEnv=='uat'){
  envConfig = require('../config/uat.env')
}else if(buildEnv=='test'){
  envConfig = require('../config/test.env')
}else {
  envConfig = require('../config/dev.env')
}
const env = envConfig
module.exports = merge(prodEnv, {
  NODE_ENV: '"development"',
  BASE_URL: '"/api"'
})

(!)提示: prod.env.js 和 uat.env.js 直接配置 'BASE_URL'的路径

proxyTable: {
  '/api': {
      // target: 'http://localhost:8080',
      target: 'https://test-api-p.tui-ing.cn/',
      changeOrigin: true,
      pathRewrite: {
          '^/api': ''
      }
  }
},

安装依赖

axois(http请求)
babel-polyfill(兼容IE)
elementUI (http://element-cn.eleme.io/#/zh-CN)

npm i axios
npm i babel-polyfill
npm i element-ui
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'
import constant from '@/common/constant'
import { Cascader, Message,Input,Form,FormItem,Button } from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

Vue.use(Cascader)
Vue.use(Input)
Vue.use(Form)
Vue.use(FormItem)
Vue.use(Button)
Vue.prototype.$message = Message;

new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

页面路由、嵌套

<template>
  <div id="app">
    <router-view/>
  </div>
</template>

<script>
    export default {
      name: 'App',
      data(){
        return {
    
        }
      }
    }
</script>
import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

export default new Router({
  routes: [
    {
        path: '/',
        redirect: '/index'
    },
    {
      path: '/index',
      name: 'index',
      component: resolve => require(['@/view/common/content'], resolve), // 主体
      children: [{
          path: '',
          component: resolve => require(['@/view/homePage'], resolve), //子页面
      }
    ]},
    {
      path:'/agreement',
      component:resolve=>require(['@/view/agreement'],resolve), //协议页面
    }
  ]
})
<template>
    <div>
        <v-head></v-head>
        <div class="contentBox">
            <router-view></router-view>
        </div>
        <v-footer></v-footer>
    </div>
</template>

<script>
    import vHead from './header.vue'
    import vFooter from './footer.vue'
    export default {
        components:{vHead,vFooter},
        data(){
            return {
                
            }
        },
    }
</script>
上一篇下一篇

猜你喜欢

热点阅读