1. 程式人生 > >vue項目總結,所用到的技術點

vue項目總結,所用到的技術點

-- ews -1 數量 頁面 splice 匹配規則 {} 路由

1.用到的技術點

vue     是一個漸進式JavaScript框架    npm install vue
vue-route    是一個路由匹配功能    npm install vue-router              
vue-resource    發送ajax請求的    npm install vue-resource
vue-preview     圖片預覽插件        npm i vue-preview -S
vuex    組件傳值    npm install vuex --save
mint-ui    基於vue的移動端組件    npm i mint-ui -S
mui    最接近原生的前端框架    

2. template.html 是整個主頁面,最終所有的js和css和html都會匹配到這裏的

  1.只需要定義一個ID容器 <div id="app"></div>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Vue項目</title>
    <!-- 我是一個好人 -->
    <style>
        @media only screen and (width:320px){
            html{
                font-size:16px;
            }
        }

        @media only screen and (width:360px){
            html{
                font-size:18px;
            }
        }

        @media only screen and (width:414px){
            html{
                font-size:20px;
            }
        }

        body{
            margin: 0px;
            padding: 0px;
        }
    </style>
</head>
<body>
    <div id="app"></div>
</body>
</html>

2. main.js 是一個主模塊集成了所有的第三方包和css文件和js文件和路由匹配  

  1.引入第三方包,使用import關鍵字,基於vue的必須集成到vue中以 Vue.use() 方法

  2.導入項目中所需要的css

  3.讓app.vue給用戶顯示的第一個組件

    1.首先app.vue引入到mian.js中  import App from ‘./App.vue‘

    2.在Vue實例中定義render函數,變量名需要一致

      render:function(createElement){ //項目一啟動之後,呈現給用戶的第一個組件

        return createElement(App)

      }   4.設置全局過濾器 Vue.filter()   5.路由整個步驟     1.導入需要設置路由的組件 
import home from ‘./components/home/home.vue‘
import category from ‘./components/category/category.vue‘
    2.設置路由規則 new VueRouter實例      
const router = new VueRouter({
        routes:[
          {path:‘/‘,redirect:‘/home‘},
          {path:‘/home‘,component:home},
          {path:‘/category‘,component:category},
          {name:"pictureAndTextIntruduction",path:"/goods/pictureAndTextIntruduction",component:pictureAndTextIntruduction},
          {path:"/goods/goodscomment",component:goodscomment}
        ]
      })
    3.把路由匹配規則放到跟實例 new Vue({})的router中,就實現整個路由匹配功能了

new Vue({
   router:router
})   
6.vuex的使用步驟
  1.新建一個空倉庫 var store = new Vuex.Store()
    倉庫裏有四個對象
    state  存儲數據的地方
    getters 獲取數據的地方
    mutations 同步更新數據
    actions 異步更新數據
  2.把倉庫的東西放到跟實例中,變量名需一致
new Vue({
   store:store
})   
     
//導入我們的第三方包
import Vue from ‘vue‘ //es5 ===> var vue = require(‘vue‘)
import Mint from ‘mint-ui‘
import VueRouter from ‘vue-router‘
import VueResource from ‘vue-resource‘
import moment from ‘moment‘
import VuePreview from ‘vue-preview‘
import axios from ‘axios‘
import Vuex from ‘vuex‘

//集成到Vue中
Vue.use(Mint)
Vue.use(VueRouter) // vue.$route vue.$router
Vue.use(VueResource) //vue.$http...
Vue.use(VuePreview)
Vue.use(Vuex) //$store

Vue.prototype.$axios = axios

//導入項目中需要用到的css
import ‘mint-ui/lib/style.min.css‘
import ‘./statics/mui/css/mui.min.css‘
import ‘./statics/css/common.css‘ //自己寫的樣式,放在最好


//導入我們要渲染的根組件這個模塊
import App from ‘./App.vue‘

//全局過濾器
Vue.filter(‘dateFmt‘,(input,formatString)=>{
    const lastFormatString = formatString || ‘YYYY-MM-DD HH:mm:ss‘

    /**
     * 參數1:格式化的原始時間
     * 參數2:格式化的字符串
     */
    return moment(input).format(lastFormatString)
})


//導入需要設置路由規則的組件
import home from ‘./components/home/home.vue‘
import category from ‘./components/category/category.vue‘
import shopcart from ‘./components/shopcart/shopcart.vue‘
import mine from ‘./components/mine/mine.vue‘
import newslist from ‘./components/news/newslist.vue‘
import newsinfo from ‘./components/news/newsinfo.vue‘
import photolist from ‘./components/photo/photolist.vue‘
import photoinfo from ‘./components/photo/photoinfo.vue‘
import goodslist from ‘./components/goods/goodslist.vue‘
import goodsinfo from ‘./components/goods/goodsinfo.vue‘
import pictureAndTextIntruduction from ‘./components/goods/pictureAndTextIntruduction.vue‘
import goodscomment from ‘./components/goods/goodscomment.vue‘

//創建路由對象,設置路由規則
const router = new VueRouter({
    routes:[
        {path:‘/‘,redirect:‘/home‘},
        {path:‘/home‘,component:home},
        {path:‘/category‘,component:category},
        {path:‘/shopcart‘,component:shopcart},
        {path:‘/mine‘,component:mine},
        {path:‘/news/newslist‘,component:newslist},
        {path:‘/news/newsinfo/:newsId‘,component:newsinfo},
        {path:‘/photo/photolist‘,component:photolist},
        {path:‘/photo/photoinfo/:photoId‘,component:photoinfo},
        {path:‘/goods/goodslist‘,component:goodslist},
        {path:"/goods/goodsinfo/:goodsId",component:goodsinfo},
        {name:"pictureAndTextIntruduction",path:"/goods/pictureAndTextIntruduction",component:pictureAndTextIntruduction},
        {path:"/goods/goodscomment",component:goodscomment}
    ]
})

//創建一個倉庫
//window
const store = new Vuex.Store({
  state: {//存儲數據的地方
    goodsList:[]
  },
  getters: {//獲取數據
    //獲取加入購物車的商品的數組
    getGoodsList(state){
        return state.goodsList
    },
    //獲取加入購物車中的總數量
    getGoodsCount(state){
        let totalCount = 0
        for(var i=0;i<state.goodsList.length;i++){
            totalCount+=state.goodsList[i].count
        }

        return totalCount
    }
  },
  mutations: {//`同步更改數據`
    /**
     * 添加商品到購物車的方法
     * 其中參數1固定的,就是代表我們的state
     * 參數2:商品信息的對象
     */
    addGoods(state,goodsObj){
        //console.log("添加商品到購物車")
        //console.log(goodsObj)
        //goodsObj ==> {goodsId:87,count:3}
        state.goodsList.push(goodsObj)
        //console.log(state.goodsList)
    },
    deleteGoodsById(state,goodsId){
        for(var i = state.goodsList.length-1 ;i>=0;i--){
            if(goodsId==state.goodsList[i].goodsId){
                state.goodsList.splice(i,1)
            }
        }
    }
  },
  actions: {//`異步更改數據`
    //可以認為是$store對象
    addGoodsAction(context,goodsObj){
        //調用mutations
        context.commit(‘addGoods‘,goodsObj)
    }
  }
})

//創建根實例,到時候,Vue去解析id=app的div
new Vue({
    el:"#app",
    router,
    store,
    render:function(createElement){ //項目一啟動之後,呈現給用戶的第一個組件
        return createElement(App)
    }
})

  

vue項目總結,所用到的技術點