mall2

image.png

plugins

import Vue from 'vue'
import Vant, { Lazyload } from 'vant'
import 'vant/lib/index.css'


// / 相當(dāng)于絕對(duì)路徑public
Vue.use(Lazyload, {
  // loading: '/loading.gif'
  // loading: require('../assets/loading.gif')
  // loading: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Fb779f7241e59aebabb25b7e68bde3669be1e8e91128e8d-5echD1_fw658&refer=http%3A%2F%2Fhbimg.b0.upaiyun.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1637891018&t=7dd7a3882f61c8d26c4ccbc1f588723f'
  loading: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F0dbd39dd274025f21e0a4016cfcb32653b24536a364f-9jIt3B_fw658&refer=http%3A%2F%2Fhbimg.b0.upaiyun.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1637974552&t=8efe9a716f40a9f229d733de405b1966'
})

Vue.use(Vant)

router

import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home'
import Friends from '../views/Friends'
import Search from '../views/Search'
import Cart from '../views/Cart'
import NewsList from '../views/Home/news/NewsList'
import NewsInfo from '../views/Home/news/NewsInfo'
import PhotoList from '../views/Home/photos/PhotoList'
import PhotoInfo from '../views/Home/photos/PhotoInfo'
import GoodsList from '../views/Home/goods/GoodsList'
import GoodsInfo from '../views/Home/goods/GoodsInfo'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    component: Home,
    meta: {
      title: '首頁(yè)',
      isShow: true,
      keepAlive: true
    }
  },
  {
    path: '/home/newslist',
    component: NewsList,
    meta: {
      title: '新聞列表',
      isShow: false,
      keepAlive: true
    }
  },
  {
    path: '/home/newsinfo/:id',
    component: NewsInfo,
    meta: {
      title: '新聞詳情',
      isShow: false,
      keepAlive: false
    },
    props: true
  },
  {
    path: '/home/photolist',
    component: PhotoList,
    meta: {
      title: '圖片列表',
      isShow: false,
      keepAlive: true
    }
  },
  {
    path: '/home/photoinfo/:id',
    component: PhotoInfo,
    meta: {
      title: '圖片詳情',
      isShow: false,
      keepAlive: false
    },
    props: true
  },
  {
    path: '/home/goodslist',
    component: GoodsList,
    meta: {
      title: '商品列表',
      isShow: false,
      keepAlive: true
    }
  },
  {
    path: '/home/goodsinfo/:id',
    component: GoodsInfo,
    meta: {
      title: '商品詳情',
      isShow: true,
      keepAlive: false
    },
    props: true
  },
  {
    path: '/friends',
    component: Friends,
    meta: {
      title: '會(huì)員',
      isShow: true,
      keepAlive: true
    }
  },
  {
    path: '/search',
    component: Search,
    meta: {
      title: '搜索',
      isShow: true,
      keepAlive: true
    }
  },
  {
    path: '/cart',
    component: Cart,
    meta: {
      title: '購(gòu)物車',
      isShow: true,
      keepAlive: true
    }
  }
]

// eslint-disable-next-line no-new
const router = new VueRouter({
  // mode: 'history',
  // base: process.env.BASE_URL,
  routes
})

export default router

store
modules
cart

const state = {
  count: 6,
  cart: JSON.parse(localStorage.cart || '[]')
}

const mutations = {
  ADD_CART(state, payload) {
    let flag = false
    // 如果商品已經(jīng)存在只做數(shù)量上的修改
    state.cart.forEach(item => {
      if (item.id === payload.id) {
        item.count += payload.count
        flag = true
      }
    })
    // 如果商品不存在,添加商品
    if (!flag) {
      state.cart.push(payload)
    }
    // 把商品添加到本地存儲(chǔ)
    localStorage.cart = JSON.stringify(state.cart)
  }
}

const actions = {
  addCart({ commit }, obj) {
    commit('ADD_CART', obj)
  }
}

const getters = {
  getAllCount(state) {
    let c = 0
    state.cart.forEach(item => {
      c += item.count
    })
    return c
  },
  getAllPrice(state) {
    let o = 0
    state.cart.forEach(item => {
      if (item.isShow) {
        o += item.count * item.price
      }
    })

    return o
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}

index

import Vue from 'vue'
import Vuex from 'vuex'
import cart from './modules/cart'

Vue.use(Vuex)

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

utils

import axios from 'axios'
import { Toast } from 'vant'

// eslint-disable-next-line no-unused-vars
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API
  // baseURL: '/api'
  // timeout: 1000,
})

// 請(qǐng)求攔截
service.interceptors.request.use(config => {
  // 開啟進(jìn)度條
  NProgress.start()
  return config
}, err => {
  Promise.reject(err)
})

// 響應(yīng)攔截
service.interceptors.response.use(response => {
  // 關(guān)閉進(jìn)度條
  NProgress.done()
  // console.log(response)
  return response
}, err => {
  Toast('請(qǐng)求失敗')
  Promise.reject(err)
})

export default service

app

<template>
  <div class="app">
    <!-- header -->
    <van-nav-bar
      :title="title"
      left-text="返回"
      left-arrow
      @click-left="clickLeft"
      fixed
    />
    <!-- main -->
    <!-- keep-alive  可以緩存組件的數(shù)據(jù)、狀態(tài)  -->
    <!-- <keep-alive include="NewsList">
      <router-view></router-view>
    </keep-alive> -->

    <!-- keep-alive 可以緩存組件的狀態(tài),數(shù)據(jù) -->
    <keep-alive v-if="$route.meta.keepAlive">
      <router-view></router-view>
    </keep-alive>
    <router-view v-else></router-view>
    <!-- footer -->
    <van-tabbar
      v-model="active"
      v-if="$route.meta.isShow"
    >
      <van-tabbar-item
        icon="home-o"
        to="/home"
      ></van-tabbar-item>
      <van-tabbar-item
        icon="friends-o"
        to="/friends"
      ></van-tabbar-item>
      <van-tabbar-item
        icon="cart-o"
        :badge="$store.getters['cart/getAllCount']"
        to="/cart"
      ></van-tabbar-item>
      <van-tabbar-item
        icon="search"
        to="/search"
      ></van-tabbar-item>
    </van-tabbar>
  </div>
</template>
<script>
export default {
  data() {
    return {
      title: this.$route.meta.title,
      active: 0
    }
  },
  created() {
    console.log(this.$route.meta.keepAlive)
  },
  watch: {
    '$route.meta.title'(newVal) {
      this.title = newVal
    }
  },
  methods: {
    clickLeft() {
      this.$router.go(-1)
    }
  }
}
</script>
<style lang="scss">
.app {
  padding-top: 46px;
  padding-bottom: 50px;
}
</style>

main

import Vue from 'vue'
import App from './App.vue'
import './plugins/vant.js'
import router from './router'
import './directives/price'
import './mixins'
import './filters/datefmt'
import store from './store'
// import directivePrice from './directives/price'
// directivePrice(Vue)
import 'lib-flexible'

Vue.config.productionTip = false

new Vue({
  render: h => h(App),
  store,
  router
}).$mount('#app')
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 第一步:創(chuàng)建集群 image.png 如果已經(jīng)有了集群的界面如下 image.png 第二步:創(chuàng)建用戶(注意記住帳...
    羅雙海閱讀 272評(píng)論 0 0
  • 第一步:創(chuàng)建集群 image.png 如果已經(jīng)有了集群的界面如下 image.png 第二步:創(chuàng)建用戶(注意記住帳...
    張鈺張鈺張鈺閱讀 374評(píng)論 0 0
  • Nuxt爬坑 第一節(jié):nuxt.js相關(guān)概述 nuxt.js簡(jiǎn)單的說是Vue.js的通用框架,最常用的就是用來作S...
    阿_軍閱讀 1,158評(píng)論 0 0
  • vue-cli腳手架中webpack配置基礎(chǔ)文件詳解 一、前言 vue-cli是構(gòu)建vue單頁(yè)應(yīng)用的腳手架,輸入一...
    晟明閱讀 1,479評(píng)論 0 2
  • 原文地址在底部 這可能是vue-cli最全的解析了…… 題言: 相信很多vue新手,都像我一樣,只是知道可以用vu...
    web小哥MrYang閱讀 1,231評(píng)論 0 0

友情鏈接更多精彩內(nèi)容