<template> <div class="container"> <el-row> <el-col class="menu-box" :span="6"> <p></p> <el-menu class="menu" :default-active="$route.path" router background-color="#fff" text-color="#267943" active-text-color="#000"> <el-menu-item v-for="(item, index) in productclass" :index="'/product/'+item.class" :key="'/product/'+item.class" v-text="item.name"> </el-menu-item> </el-menu> </el-col> <!-- 右邊主要內容 --> <el-col :span="18"> <!-- 搜索框 --> <el-input type="text" class="el-input" placeholder="請輸入商品名" v-model="searchName"> <i slot="prefix" class="el-input__icon el-icon-search"></i> </el-input> <el-button type="primary" :disabled="disabled" @click="search" :loading="loading"> 搜索 </el-button> <!-- 商品列表渲染的地方 --> <transition name="el-zoom-in-center"> <router-view :key="key"></router-view> </transition> </el-col> </el-row> </div> </template> <script> import {SearchProductList} from '../../api/api' export default { data () { return { searchName: '', isactive: false, loading: false, productclass: [ { class: 'all', name: '所有商品' }, { class: 'pomegranate', name: '石榴' }, { class: 'pine', name: '松子' }, { class: 'ham', name: '火腿' }, { class: 'other', name: '其它商品' } ] } }, computed: { key () { return this.$route.params.class + new Date() }, disabled () { if (this.searchName === '') { return true } else { return false } } }, methods: { search () { this.loading = true let searchparams = this.searchName SearchProductList(searchparams).then(res => { this.loading = false if (res.data.code === 200) { this.$router.push({ path: '/product/search', query: {name: this.searchName} }) } else { this.$notify({ title: '很抱歉', message: res.data.msg, type: 'warning', offset: 200 }) this.$router.push('/product/all') } }) } } } </script> <style scoped> .el-input { margin: 20px 0; width: 70%; } .el-button { width: 25%; margin-right: 5px; } .menu { position: fixed; width: 200px; top: 150px; left: 50px; box-shadow: 0 0 6px #ccc; } .is-active { background: #669966 !important; /* border-bottom: 3px solid #66CC00 !important; */ color: #fff !important; font-weight: bold !important; } @media screen and (max-width: 767px) { .menu { position: fixed; width: 25%; top: 100px; left: 5px; box-shadow: 0 0 6px #ccc; } .is-active { background: #669966 !important; /* border-bottom: 3px solid #66CC00 !important; */ color: #fff !important; font-weight: bold !important; } } </style>
<template> <div> <div class="title"> <h1 v-text="$route.params.class"></h1> </div> <!-- <p>共{{productlist.length}}個商品</p> --> <ul> <template v-for="(item, index) in productlist"> <el-row class="item" :key="item.productclass"> <router-link :to="'/product/'+item.productclass+'/'+item.productname" :key="item.productclass"> <el-col :span="8"> <img :src="item.productimage" > </el-col> </router-link> <el-col :span="16"> <h3>{{item.productname}}</h3> <p class="intro">{{item.productintro}}</p> <router-link :to="'/product/'+item.productclass+'/'+item.productname" :key="item.productclass"> <p class="link">瞭解詳情...</p> </router-link> <p class="sellnum">累計發貨<span>{{item.productsells}}</span>件</p> <p class="price">全國包郵價<span>{{item.productprice}}</span>元</p> <!-- <el-input-number size="mini" v-model="addnum"></el-input-number> --> </el-col> </el-row> </template> </ul> </div> </template> <script> import {GetProductList, SearchProductList} from '../../../api/api' export default { data () { return { productlist: [], addnum: 0 } }, // methods: { // formatClass () { // if (this.$route.params.class === 'all' || 'ham' || 'ham' || 'ham') { // } // } // }, mounted () { if (this.$route.query.name) { // 獲取查詢列表 // console.log(this.$route.query.name) let searchparams = this.$route.query.name SearchProductList(searchparams).then(res => { // console.log(res) if (res.data.code === 200) { this.productlist = res.data.searchRes } else { this.$notify({ title: '很抱歉', message: res.data.msg, type: 'warning', offset: 200 }) this.$router.push('/product/all') } }) } else { // 獲取分類列表 let params = null if (this.$route.params.class === 'all') { params = '' } else { params = this.$route.params.class } GetProductList(params).then(res => { // console.log(res) this.productlist = res.data.productlist }) } } } </script> <style scoped> a { text-decoration: none; color: #999; } a:hover { color: #669966; } .title { height: 60px; border-left: 4px solid #669966; margin: 0 15px; background-color: #f2f3f2; text-align: left; padding-left:20px; line-height: 60px; } ul { padding: 0; margin: 0 15px; } .item { margin: 25px 0; /* background: #f2f3f2; */ box-shadow: 0 0 30px #ccc; border-radius: 15px; } .item:hover { /* border: 1px solid silver; */ background: #eee; } img { width: 100%; max-width: 500px; padding: 10px; border-radius: 15px; } .intro,.price,.sellnum { margin: 0 15px 0 25px; text-align: left; color: #666; line-height: 2; letter-spacing: 1.2; } .link { text-align: right; font-size: 16px; margin-right: 40px; } .price,.sellnum { color: gray; } .price span { color: red; font-size: 25px; } </style>
<template> <div> <h1>productcontent.vue</h1> <p>{{$route.params.class}}</p> <p>{{$route.params.productname}}</p> <div> <h2 v-text="product.productname"></h2> <p>價格:{{product.productprice}}元</p> <p>銷量:{{product.productsells}}</p> <img :src="product.productimage"> <p>{{product.productintro}}</p> </div> </div> </template> <script> import {GetProduct} from '../../../api/api' export default { data () { return { product: {} } }, mounted () { let params = { productname: this.$route.params.productname, productclass: this.$route.params.class } GetProduct(params).then(res => { // console.log(res) this.product = res.data.curproduct }) } } </script>
樣式的寫法那就多種多樣了,這個就是隨便寫一下,若是是這要寫商品頁面的話能夠參考不少商城的樣式,也能夠加不少效果,我這個寫得更像文章頁面element-ui 修改樣式vue
好比修改element-ui的導航菜單active樣式,先查看元素,在菜單被選中的時候會多一個is-active的class,因此只要修改這個is-active就能夠了,但要注意寫在scope裏面須要在後面加一個!importantgit
雖然是菜鳥初學者,但仍是自戀的留一個github地址吧
github: https://github.com/lyttonlee/...