【vue3.0】19.0 某東到家(十九)——訂單頁面創建及頂部佈局

修改 src\views\home\Docker.vue

<template>
  <!-- 底部主菜單容器 -->
  <div class="docker">
    <span
      :class="{ docker__item: true, 'docker__item--active': item.active }"
      v-for="(item, index) in dockerList"
      :key="index"
    >
      <router-link :to="item.to">
        <div class="docker__item_icon">
          <i :class="item.icon"></i>
        </div>
        <div class="docker__item__title">{{ item.title }}</div>
      </router-link>
    </span>
  </div>
</template>

<script>
export default {
  name: 'Docker',
  setup() {
    const dockerList = [
      {
        active: true,
        icon: 'custom-icon custom-icon-home',
        title: '首頁',
        to: '/'
      },
      {
        icon: 'custom-icon custom-icon-shopping',
        title: '購物車',
        to: '/cartList'
      },
      {
        icon: 'custom-icon custom-icon-order',
        title: '訂單',
        to: '/'
      },
      {
        icon: 'custom-icon custom-icon-my',
        title: '我的',
        to: '/'
      }
    ]

    return { dockerList }
  }
}
</script>

<style lang="scss" scoped>
@import '@/style/viriables';
@import '@/style/mixins';
.docker {
  color: $content-font-color;
  display: flex; //自適應均贈,彈性盒子
  position: absolute; //絕對定位
  box-sizing: border-box; //這個會以body的最外層作爲容器的最外層
  padding: 0 0.18rem;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 0.49rem;
  border-top: 1px solid $content-bg-color;
  &__item {
    flex: 1;
    text-align: center;
    a {
      color: $content-font-color;
      text-decoration: none;
    }
    &--active {
      a {
        color: #1fa4fc;
      }
    }
    &_icon {
      margin: 0.05rem 0 0.02rem 0; //圖標距離上邊距7px左右;
      font-size: 0.18rem;
    }
    &__title {
      //瀏覽器最小像素是12px,如果想表達12px以下大小得如下編寫
      font-size: 0.12rem;
      transform: scale(0.5 0.5); //橫向縮小50% 縱向搜小50%
      transform-origin: center top; //縮放的中心點
    }
  }
}
</style>

主要是構建路由跳轉。
增加代碼如下:

      <router-link :to="item.to">
......
      </router-link>

......
    const dockerList = [
      {
        active: true,
        icon: 'custom-icon custom-icon-home',
        title: '首頁',
        to: '/'
      },
      {
        icon: 'custom-icon custom-icon-shopping',
        title: '購物車',
        to: '/cartList'
      },
      {
        icon: 'custom-icon custom-icon-order',
        title: '訂單',
        to: '/'
      },
      {
        icon: 'custom-icon custom-icon-my',
        title: '我的',
        to: '/'
      }
    ]
......

新增頁面src\views\cartList\CartList.vue

<template> cartList</template>

<script>
export default {
  name: 'CartList'
}
</script>

新增路由:
src\router\index.js

  {
    path: '/cartList',
    name: 'CartList',
    component: () =>
      import(
        /* webpackChunkName: "cartList" */ '../views/cartList/CartList.vue'
      )
  },
新增訂單頁面

調整路由:

import { createRouter, createWebHistory } from 'vue-router'

const routes = [
 ......
  {
    path: '/orderConfirmation',
    name: 'OrderConfirmation',
    component: () =>
      import(
        /* webpackChunkName: "orderConfirmation" */ '../views/OrderConfirmation/OrderConfirmation.vue'
      )
  }
]
......
export default router

創建src\views\orderConfirmation\OrderConfirmation.vue:

<template> </template>
<script>
export default {
  name: 'OrderConfirmation',
  setup() {}
}
</script>

路由文件更新如下:
src\router\index.js

import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () =>
      import(/* webpackChunkName: "home" */ '../views/home/Home.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () =>
      import(/* webpackChunkName: "login" */ '../views/login/Login.vue'),
    beforeEnter: (to, from, next) => {
      // 只有訪問Login頁面之前纔會執行次函數
      const { isLogin } = localStorage // 從本地存儲中取isLogin
      // 如果登錄,就跳到首頁頁面;否則跳轉到登錄頁面
      isLogin ? next({ name: 'Home' }) : next()
    }
  },
  {
    path: '/register',
    name: 'Register',
    component: () =>
      import(
        /* webpackChunkName: "register" */ '../views/register/Register.vue'
      ),
    beforeEnter: (to, from, next) => {
      const { isLogin } = localStorage
      isLogin ? next({ name: 'Home' }) : next()
    }
  },
  {
    path: '/shop/:id',
    name: 'Shop',
    component: () =>
      import(/* webpackChunkName: "shop" */ '../views/shop/Shop.vue')
  },
  {
    path: '/cartList',
    name: 'CartList',
    component: () =>
      import(
        /* webpackChunkName: "cartList" */ '../views/cartList/CartList.vue'
      )
  },
  {
    path: '/orderConfirmation',
    name: 'OrderConfirmation',
    component: () =>
      import(
        /* webpackChunkName: "orderConfirmation" */ '../views/orderConfirmation/OrderConfirmation.vue'
      )
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
// beforeEach:全局,每次做路由跳轉之前都會執行這個操作。
router.beforeEach((to, from, next) => {
  // to and from are Route Object,
  // to:跳轉的時候想要跳轉的頁面的信息
  // from :指從哪裏跳過來的信息
  // next() must be called to resolve the hook}
  // 中間件繼續執行的方法

  // 從本地存儲中取isLogin
  const { isLogin } = localStorage

  console.log(to, from)
  /** 判斷是否登錄 */
  // 必須雙循環,才能防止死循環
  // 如果沒有登錄,就跳到登錄頁面
  const { name } = to
  const isLoginOrRegister = name === 'Login' || name === 'Register'
  isLogin || isLoginOrRegister ? next() : next({ name: 'Login' })
})
export default router

新建文件
src\views\orderConfirmation\OrderConfirmation.vue

<template>
  <div>orderConfirmation</div>
</template>

<script>
// import { ref } from 'vue'
export default {
  name: 'OrderConfirmation'
}
</script>

修改src\views\shop\Cart.vue

......
     <div class="check__btn">
        <router-link :to="{ name: 'OrderConfirmation' }">
          去結算
        </router-link>
      </div>
......

跳轉後需要知道是哪個商鋪下的購物車訂單。這時候需要帶一個參數過去,可以修改如下:

src\views\orderConfirmation\OrderConfirmation.vue                 
......
      <div class="check__btn">
        <router-link :to="{ path: `/orderConfirmation/${shopId}` }">
          去結算
        </router-link>
      </div>
......

修改路由:
src\router\index.js

  {
    path: '/orderConfirmation/:shopId',
    name: 'OrderConfirmation',
    component: () =>
      import(
        /* webpackChunkName: "orderConfirmation" */ '../views/orderConfirmation/OrderConfirmation.vue'
      )
  }
訂單頁面樣式佈局

首先完善一下骨架:
src\views\orderConfirmation\OrderConfirmation.vue

<template>
  <div class="top">
    <div class="top__bgcolor" />
    <div class="top__header">
      <span><i class="custom-icon custom-icon-back"></i> 確認訂單</span>
    </div>
    <div class="top__receiver">
      <div class="top__receiver__title">收貨地址</div>
      <div class="top__receiver__address">西安一二三大學四五六科技園2號樓</div>
      <div class="top__receiver__info">
        <span class="top__receiver__info__name">張三(先生)</span>
        <span class="top__receiver__info__phone">18012341234</span>
      </div>
      <span><i class="custom-icon custom-icon-back"></i></span>
    </div>
  </div>
</template>

<script>
// import { ref } from 'vue'
export default {
  name: 'OrderConfirmation'
}
</script>

接下來增加樣式:

<style lang="scss" scoped>
.top {
  height: 1.96rem;
  background-size: 100% 1.59rem;
  /* 漸變軸爲0度,相當於從下到上,
   高度4%位置從rgba(0, 145, 255, 0) 開始漸變
   到高度50%位置的藍色(#0091ff)結束 */
  background-image: linear-gradient(0deg, rgba(0, 145, 255, 0) 4%, #0091ff 50%);
}
</style>

這裏背景超出長度會顯示原來的藍色。
優化如下:

<style lang="scss" scoped>
.top {
  height: 1.96rem;
  background-size: 100% 1.59rem;
  /* 漸變軸爲0度,相當於從下到上,
   高度4%位置從rgba(0, 145, 255, 0) 開始漸變
   到高度50%位置的藍色(#0091ff)結束 */
  background-image: linear-gradient(0deg, rgba(0, 145, 255, 0) 4%, #0091ff 50%);
  background-repeat: no-repeat;
}
</style>


增加樣式:

<style lang="scss" scoped>
.top {
......
  &__header {
    padding-top: 0.4rem;
    line-height: 0.24rem;
    color: #fff;
    text-align: center;
  }
}
</style>

進一步微調

<template>
  <div class="top">
    <div class="top__bgcolor" />
    <div class="top__header">
      <div class="top__header__back">
        <i class="custom-icon custom-icon-back"></i>
      </div>
      <span>確認訂單</span>
    </div>
......
  </div>
</template>
<style lang="scss" scoped>
.top {
......
  &__header {
    position: relative;
    padding-top: 0.4rem;
    line-height: 0.24rem;
    color: #fff;
    text-align: center;
    font-size: 0.16rem;
    &__back {
      position: absolute;
      font-size: 0.22rem;
      left: 0.18rem;
    }
  }
}
</style>

以上是模擬器的效果,考慮到真機瀏覽器的大小本來就會給上部留白,這裏微調一下距上的距離:

  &__header {
......
    padding-top: 0.26rem;
......
    &__back {
......
    }
  }

進一步,優化收貨信息:

<template>
  <div class="wrapper">
    <div class="top">
      <div class="top__bgcolor" />
      <div class="top__header">
        <div class="top__header__back">
          <i class="custom-icon custom-icon-back"></i>
        </div>
        <span>確認訂單</span>
      </div>
      <div class="top__receiver">
        <div class="top__receiver__title">收貨地址</div>
        <div class="top__receiver__address">
          西安一二三大學四五六科技園2號樓
        </div>
        <div class="top__receiver__info">
          <span class="top__receiver__info__name">張三(先生)</span>
          <span class="top__receiver__info__phone">18012341234</span>
        </div>
        <span><i class="custom-icon custom-icon-back"></i></span>
      </div>
    </div>
  </div>
</template>

<script>
// import { ref } from 'vue'
export default {
  name: 'OrderConfirmation'
}
</script>
<style lang="scss" scoped>
.wrapper {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background-color: #eee;
}
.top {
  position: relative;
  height: 1.96rem;
  background-size: 100% 1.59rem;
  /* 漸變軸爲0度,相當於從下到上,
   高度4%位置從rgba(0, 145, 255, 0) 開始漸變
   到高度50%位置的藍色(#0091ff)結束 */
  background-image: linear-gradient(0deg, rgba(0, 145, 255, 0) 4%, #0091ff 50%);
  background-repeat: no-repeat;

  &__header {
    position: relative;
    padding-top: 0.26rem;
    line-height: 0.24rem;
    color: #fff;
    text-align: center;
    font-size: 0.16rem;
    &__back {
      position: absolute;
      font-size: 0.22rem;
      left: 0.18rem;
    }
  }
  &__receiver {
    position: absolute;
    left: 0.18rem;
    right: 0.18rem;
    bottom: 0rem;
    height: 1.11rem;
    background: #fff;
    border-radius: 0.04rem;
  }
}
</style>

繼續優化其他信息:

<template>
  <div class="wrapper">
    <div class="top">
      <div class="top__bgcolor" />
      <div class="top__header">
        <div class="top__header__back">
          <i class="custom-icon custom-icon-back"></i>
        </div>
        <span>確認訂單</span>
      </div>
      <div class="top__receiver">
        <div class="top__receiver__title">收貨地址</div>
        <div class="top__receiver__address">
          西安一二三大學四五六科技園2號樓
        </div>
        <div class="top__receiver__info">
          <span class="top__receiver__info__name">張三(先生)</span>
          <span class="top__receiver__info__phone">18012341234</span>
        </div>
        <div class="top__receiver__icon">
          <i class="custom-icon custom-icon-back"></i>
        </div>
      </div>
    </div>
  </div>
</template>
......
<style lang="scss" scoped>
.wrapper {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background-color: #eee;
}
.top {
......
  &__receiver {
    position: absolute;
    left: 0.18rem;
    right: 0.18rem;
    bottom: 0rem;
    height: 1.11rem;
    background: #fff;
    border-radius: 0.04rem;
    &__title {
      line-height: 0.22rem;
      padding: 0.16rem 0 0.14rem 0.16rem;
      font-size: 0.16rem;
      color: #333;
    }
    &__address {
      line-height: 0.2rem;
      padding: 0 0.4rem 0 0.16rem;
      font-size: 0.16rem;
      color: #333;
    }
    &__info {
      padding: 0.06rem 0 0 0.16rem;
      &__name &__phone {
        margin-right: 0.1rem;
        line-height: 0.18rem;
        font-size: 0.12rem;
        color: #666;
      }
    }
    &__icon {
      //旋轉180度
      transform: rotate(180deg);
      position: absolute;
      right: 0.16rem;
      top: 0.5rem;
      font-size: 0.16rem;
      color: #666;
    }
  }
}
</style>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章