黑马vue商城-day05-商品列表

创建基本组件

<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/categories' }"
        >首页</el-breadcrumb-item
      >
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input laceholder="请输入内容">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary">添加商品</el-button>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {}
  },
  methods: {},
  created() {}
}
</script>

<style lang="less" scoped></style>

获取商品列表数据

这几个都比较简单

时间过滤器

Vue.filter("dateFormat", function(origunVal) {
  const dt = new Date(origunVal)

  const y = dt.getFullYear()
  const m = (dt.getMonth() + 1 + "").padStart(2, "0")
  const d = (dt.getDate() + "").padStart(2, "0")

  const hh = (dt.getHours() + "").padStart(2, "0")
  const mm = (dt.getMinutes() + "").padStart(2, "0")
  const ss = (dt.getSeconds() + "").padStart(2, "0")

  return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
})
<el-table-column label="创建时间" prop="add_time" width="140px">
	<template slot-scope="scope">
		{{scope.row.add_time | dateFormat}}
	</template>
</el-table-column>

商品列表分页

比较简单

搜索

比较简单

删除

简单

点击添加按钮跳转到添加页面

也比较简单

绘制添加页面的基本结构

忽略

切换标签页面的禁止

:before-leave=“beforeTabLeave” 绑定这个属性

beforeTabLeave(activeName, oldActiveName) {
      // console.log('即将离开的标签页名字是:' + oldActiveName)
      // console.log('即将进入的标签页名字是:' + activeName)
      // return false
      if (oldActiveName === '0' && this.addForm.goods_cat.length !== 3) {
        this.$message.error('请先选择商品分类!')
        return false
      }
    },

return false可以阻止跳转

商品参数

第二个面板获取动态参数

根据 分类信息,tab 被选中的时候触发

async tabClicked() {
      // console.log(this.activeIndex)
      // 证明访问的是动态参数面板
      if (this.activeIndex === '1') {
        const { data: res } = await this.$http.get(
          `categories/${this.cateId}/attributes`,
          {
            params: { sel: 'many' }
          }
        )

        if (res.meta.status !== 200) {
          return this.$message.error('获取动态参数列表失败!')
        }

        console.log(res.data)
        res.data.forEach(item => {
          item.attr_vals =
            item.attr_vals.length === 0 ? [] : item.attr_vals.split(' ')
        })
        this.manyTableData = res.data
      } else if (this.activeIndex === '2') {
        const { data: res } = await this.$http.get(
          `categories/${this.cateId}/attributes`,
          {
            params: { sel: 'only' }
          }
        )

        if (res.meta.status !== 200) {
          return this.$message.error('获取静态属性失败!')
        }

        console.log(res.data)
        this.onlyTableData = res.data
      }
    }

解决 checkbox 的问题

.el-checkbox {
  margin: 0 10px 0 0 !important;
}

商品属性

还算简单

<el-tab-pane label="商品属性" name="2">
  <el-form-item
    :label="item.attr_name"
    v-for="item in onlyTableData"
    :key="item.attr_id"
  >
    <el-input v-model="item.attr_vals"></el-input>
  </el-form-item>
</el-tab-pane>

图片上传

图片列表缩略图

<!-- action 图片要上传的地址
  list-type="picture" 当前文件的预览方式 -->
  <el-upload
    :action="uploadURL"
    :on-preview="handlePreview"
    :on-remove="handleRemove"
    list-type="picture"
    :headers="headerObj"
  >
    <el-button size="small" type="primary">点击上传</el-button>
  </el-upload></el-tab-pane
>

解决token 问题

:headers="headerObj"

headerObj: {
  Authorization: window.sessionStorage.getItem("token")
}

当上传成功以后

// 监听图片上传成功的事件
handleSuccess(response) {
  console.log(response)
  // 1.拼接得到一个图片信息对象
  const picInfo = { pic: response.data.tmp_path }
  // 2.将图片信息对象,push到pics数组中
  this.addForm.pics.push(picInfo)
  console.log(this.addForm);
}

remove

// 处理移除图片的操作
handleRemove(file) {
    // 1.获取将要删除的图片的临时路径
    const filePath = file.response.data.tmp_path
    // 2.从pics数组中,找到这个图片对应的索引值
    const i = this.addForm.pics.findIndex(x => x.pic === filePath)
    // 3.调用数组的 splice 方法,把图片信息对象,从pics 数组中移除
    this.addForm.pics.splice(i, 1)
    console.log(this.addForm);
},

预览

<!-- 图片预览 -->
<el-dialog
  title="图片预览"
  :visible.sync="previewVisible"
  width="50%"
>
  <img :src="previewPath" class="previewImg"/>
</el-dialog>
// 处理图片预览效果
handlePreview(file) {
  console.log(file)
  this.previewPath = file.response.data.url
  this.previewVisible = true
},

商品内容

https://github.com/surmon-china/vue-quill-editor

安装这个插件

<!-- 富文本编辑器组件 -->
<quill-editor v-model="addForm.goods_introduce"></quill-editor>

lodash 的使用

v-model 中的是数组 提交的是 字符串

good_vals

需要使用深拷贝

// 添加商品
add() {
  console.log(this.addForm)
  this.$refs.addFormRef.validate(valid => {
    if (!valid) {
      return this.$message.error("请填写必要的表单项")
    }
  })
  // 执行添加的业务逻辑
  // lodash  cloneDeep(obj)
  const form = _.cloneDeep(this.addForm)
  form.goods_cat = form.goods_cat.join(",")
  console.log(form)
}

处理属性

// 处理动态参数
this.manyTableData.forEach(item => {
  const newInfo = {
    attr_id: item.attr_id,
    attr_value: item.attr_vals.join(" ")
  }
  this.addForm.attrs.push(newInfo)
})
// 处理静态参数
this.onlyTableData.forEach(item => {
  const newInfo = {
    attr_id: item.attr_id,
    attr_value: item.attr_vals
  }
  this.addForm.attrs.push(newInfo)
})
form.attrs = this.addForm.attrs
console.log(form)

提交商品

// 发起请求添加商品
// 商品的名称,必须是唯一的
const {data: res} = await this.$http.post('goods', form)
if (res.meta.status !== 201) {
  return this.$message.error('添加商品失败')
}
this.$message.success('添加商品成功')
this.$router.push('/goods')
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值