3.前端项目搭建

3.1.开发环境检查

  1. 检查cnpm安装环境: 命令行下输入:cnpm -v
  2. 检查VueCli安装环境:命令行下输入:vue -V (注意:本工程使用VueCli4.0.0以上版本)

    附录:

    1. 安装npm:直接安装node.js (输入 “npm -v” 测试是否安装成功; 输入 node –v 查看node版本)
    2. 安装cnpm:npm install -g cnpm —registry=https://registry.npm.taobao.org
    3. 全局安装vuecli:cnpm install -g @vue/cli (或:npm install -g @vue/cli@4.x.x)
    4. 查看当前安装的vue-cli版本:vue —version 或 vue –V
    5. 卸载旧版本的vue-cli:cnpm uninstall vue-cli -g
    6. 查看远程仓库中的版本号:cnpm view @vue/cli versions —json

3.2.搭建VueCli工程总体架构

3.2.1.搭建VueCli模板工程

  1. 命令行下进入工作空间目录中,输入: vue create elmclient (工程名必须小写)
  2. 选择预设模板:这里选择“Manually select features”(手动选择特征)
  3. 模块选取:Babel、Router
  4. 选择是否使用history 形式的路由:选择:Y
  5. 将依赖文件放在package.json中:选择:“in package.json”
  6. 是否将当前选择保存以备下次使用:选择:N
  7. 进入创建好的工程目录:cd elmclient
  8. 启动工程:npm run serve
  9. 在浏览器中测试:http://localhost:8080

    3.2.2.添加其它依赖及配置文件

  10. 添加font-awesome与axios依赖:

    1. npm install font-awesome --save <br /> npm install axios --save <br /> npm install qs --save
  11. 添加图片到src的assets中。

  12. 在src目录下添加common.js文件

    1. //获取当前时间(XXXX-XX-XX)
    2. export function getCurDate() {
    3. var now = new Date();
    4. var year = now.getFullYear();
    5. var month = now.getMonth() + 1;
    6. var day = now.getDate();
    7. month = month < 10 ? "0" + month : month;
    8. day = day < 10 ? "0" + day : day;
    9. return year + "-" + month + "-" + day;
    10. }
    11. //向sessionStorage中存储一个JSON对象
    12. export function setSessionStorage(keyStr, value) {
    13. sessionStorage.setItem(keyStr, JSON.stringify(value));
    14. }
    15. //从sessionStorage中获取一个JSON对象(取不到时返回null)
    16. export function getSessionStorage(keyStr) {
    17. var str = sessionStorage.getItem(keyStr);
    18. if (str == '' || str == null || str == 'null' || str == undefined) {
    19. return null;
    20. } else {
    21. return JSON.parse(str);
    22. }
    23. }
    24. //从sessionStorage中移除一个JSON对象
    25. export function removeSessionStorage(keyStr) {
    26. sessionStorage.removeItem(keyStr);
    27. }
    28. //向localStorage中存储一个JSON对象
    29. export function setLocalStorage(keyStr, value) {
    30. localStorage.setItem(keyStr, JSON.stringify(value));
    31. }
    32. //从localStorage中获取一个JSON对象(取不到时返回null)
    33. export function getLocalStorage(keyStr) {
    34. var str = localStorage.getItem(keyStr);
    35. if (str == '' || str == null || str == 'null' || str == undefined) {
    36. return null;
    37. } else {
    38. return JSON.parse(str);
    39. }
    40. }
    41. //从localStorage中移除一个JSON对象
    42. export function removeLocalStorage(keyStr) {
    43. localStorage.removeItem(keyStr);
    44. }
  13. 在工程根目录下添加vue.config.js文件

    module.exports = {
     devServer: {
         port: 8081
     }
    }
    

    3.2.3.main.js文件

    import Vue from 'vue'
    import App from './App.vue'
    import router from './router'
    import store from './store'
    import 'font-awesome/css/font-awesome.min.css'
    import axios from 'axios'
    import qs from 'qs'
    //导入自定义模块
    import {
     getCurDate,
     setSessionStorage,
     getSessionStorage,
     removeSessionStorage,
     setLocalStorage,
     getLocalStorage,
     removeLocalStorage
    } from './common.js'
    Vue.config.productionTip = false
    //设置axios的基础url部分
    axios.defaults.baseURL = 'http://localhost:8080/elm/';
    //将axios挂载到vue上,使用是就可以 this.$axios 这样使用了
    Vue.prototype.$axios = axios;
    Vue.prototype.$qs = qs;
    Vue.prototype.$getCurDate = getCurDate;
    Vue.prototype.$setSessionStorage = setSessionStorage;
    Vue.prototype.$getSessionStorage = getSessionStorage;
    Vue.prototype.$removeSessionStorage = removeSessionStorage;
    Vue.prototype.$setLocalStorage = setLocalStorage;
    Vue.prototype.$getLocalStorage = getLocalStorage;
    Vue.prototype.$removeLocalStorage = removeLocalStorage;
    router.beforeEach(function(to, from, next) {
     let user = sessionStorage.getItem('user');
     //除了登陆、注册、首页、商家列表、商家信息之外,都需要判断是否登陆了
     if (!(to.path == '/' || to.path == '/index' || to.path == '/businessList' || to.path == '/businessInfo' || to.path ==
             '/login' || to.path == '/register')) {
         if (user == null) {
             router.push('/login');
             location.reload();
         }
     }
     next();
    });
    new Vue({
     router,
     store,
     render: h => h(App)
    }).$mount('#app')
    

    3.2.4.App.vue文件

    注意:在App.vue文件中,#app的高度也要设置为100%。

    <template>
     <div id="app">
         <router-view />
     </div>
    </template>
    <!-- 注意这里为共通样式,不能加scoped -->
    <style>
     /*这里的内容,就是静态工程中的 reset.css*/
     html,body,div,span,h1,h2,h3,h4,h5,h6,ul,ol,li,p {
         margin: 0;
         padding: 0;
         font-family: "微软雅黑";
     }
     html,body,#app {
         /*必须要设置,这样总容器高度才能100%*/
         width: 100%;
         height: 100%;
     }
     ul,ol {
         list-style: none;
     }
     a {
         text-decoration: none;
     }
    </style>
    

    3.2.5.路由index.js文件

    import Vue from 'vue'
    import VueRouter from 'vue-router'
    import Index from '../views/Index.vue'
    import BusinessList from '../views/BusinessList.vue'
    // ... ...
    Vue.use(VueRouter)
    const routes = [{
         path: '/',
         name: 'Home',
         component: Index
     },{
         path: '/index',
         name: 'Index',
         component: Index
     },{
         path: '/businessList',
         name: 'BusinessList',
         component: BusinessList
     },{
         // ... ...
     }
    ]
    //解决重复路由报异常问题
    const originalPush = VueRouter.prototype.push
    VueRouter.prototype.push = function push(location) {
     return originalPush.call(this, location).catch(err => err)
    }
    const router = new VueRouter({
     mode: 'history',
     base: process.env.BASE_URL,
     routes
    })
    export default router
    

    3.3.共通组件

    3.3.1.Footer组件:

    <template>
     <!-- 底部菜单部分 -->
     <ul class="footer">
         <li @click="toIndex">
             <i class="fa fa-home"></i>
             <p>首页</p>
         </li>
         <li>
             <i class="fa fa-compass"></i>
             <p>发现</p>
         </li>
         <li @click="toOrderList">
             <i class="fa fa-file-text-o"></i>
             <p>订单</p>
         </li>
         <li @click="toUserCenter">
             <i class="fa fa-user-o"></i>
             <p>我的</p>
         </li>
     </ul>
    </template>
    <script>
     export default{
         name:'Footer',
         data(){
             return {
                 user:{}
             }
         },
         methods:{
             toIndex(){
                 this.$router.push('/index');
             },
             toOrderList(){
                 this.user = this.$getSessionStorage('user');
                 if(this.user==null){
                     this.$router.push('/login');
                 }else{
                     this.$router.push('/orderList');
                 }
             },
             toUserCenter(){
                 this.user = this.$getSessionStorage('user');
                 if(this.user==null){
                     this.$router.push('/login');
                 }else{
                     this.$router.push('/userCenter');
                 }
             }
         }
     }
    </script>
    <style scoped>
     /****************** 底部菜单部分 ******************/
     .wrapper .footer{
         width: 100%;
         height: 14vw;
         border-top: solid 1px #DDD;
         background-color: #fff;
         position: fixed;
         left: 0;
         bottom: 0;
         display: flex;
         justify-content: space-around;
         align-items: center;
     }
     .wrapper .footer li{
         /*li本身的尺寸完全由内容撑起*/
         display: flex;
         flex-direction: column;
         justify-content: center;
         align-items: center;
         color: #999;
         user-select: none;
         cursor: pointer;
     }
     .wrapper .footer li p{
         font-size: 2.8vw;
     }
     .wrapper .footer li i{
         font-size: 5vw;
     }
    </style>
    

    [

](https://null_688_6639.gitee.io/javase/18%E5%89%8D%E5%90%8E%E7%AB%AF%E9%A1%B9%E7%9B%AE/02%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%AB%AF%E9%A1%B9%E7%9B%AE%E6%90%AD%E5%BB%BA.html)