收藏js css html大前端开发

蓝桥第四课-新增商品页面开发以及接口调用

2022-10-15  本文已影响0人  风中凌乱的男子
children: [
      {
        path: '/shopModel/shopList',
        name: 'ShopList',
        component: () => import('@/views/shopModel/shopList/index'),//引入的是页面的路径,这是一种懒加载形式
        meta: { title: '商品列表', icon: 'table' }
      },
      {
        path: '/shopModel/addShop',
        name: 'AddShop',
        hidden: true,//在左侧隐藏,不会显示在左侧
        component: () => import('@/views/shopModel/addShop/index'),//引入的是页面的路径,这是一种懒加载形式
        meta: { title: '新增商品', icon: 'table' }
      },
    ]
handleAdd(){
      this.$router.push("/shopModel/addShop")
},
image.png
<template>
  <div class="addShop">
    <el-card>
      新增商品  
    </el-card>
  </div>
</template>

<script>
export default {};
</script>

<style lang="scss" scoped>
.addShop {
  margin: 12px;
}
</style>
<!-- 折叠面板 -->
<el-collapse v-model="activeNames">
     <el-collapse-item title="添加商品" name="1">
         <div class="tip">操作提示</div>
         <div>
            1. 该页面展示了商城所有的商品信息,可对商品进行编辑修改操作。
         </div>
         <div>2. 商品下架后将无法在前台展示,请慎重操作。</div>
     </el-collapse-item>
</el-collapse>
// 推荐::v-deep 叫样式穿透  /deep/ 这种也许会不生效
  ::v-deep .el-collapse-item__header {
    color: #55ca7d;
    font-size: 14px;
  }
  ::v-deep .el-collapse-item__wrap {
    border: 1px dashed #55ca7d;
    background-color: #f0fdf5;
  }
  ::v-deep .el-collapse-item__content {
    padding: 10px;
  }
<div class="from">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="商品类型" prop="region">
            <el-select
              v-model="ruleForm.type"
              placeholder="请选择商品类型"
            >
              <el-option
                v-for="(item, index) in typeArr"
                :key="index"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="商品名称">
            <el-input
              v-model="ruleForm.name"
              placeholder="请输入商品名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="价格">
            <el-input
              v-model="ruleForm.price"
              type="number"
              placeholder="请输入商品名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="库存">
            <el-input-number
              v-model="ruleForm.stock"
              placeholder="请输入商品名称"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="编号">
            <el-input-number
              v-model="ruleForm.number"
              placeholder="请输入商品名称"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="排序">
            <el-input-number
              v-model="ruleForm.sort"
              placeholder="请输入商品名称"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="是否上架">
            <el-switch
              v-model="ruleForm.status"
              :active-value="1"
              :inactive-value="2"
            ></el-switch>
          </el-form-item>
          <el-form-item label="商品描述">
            <el-input
              v-model="ruleForm.desc"
              type="textarea"
              placeholder="请输入商品名称"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">立即创建</el-button>
            <el-button>取消</el-button>
          </el-form-item>
        </el-form>
</div>
data() {
    return {
      ruleForm: {
        type: "",
        name: "",
        price: "",
        stock: "",
        number: "",
        sort: "",
        status: 1,
      },
      rules: {},
      typeArr: [
        {
          name: "儿童用品",
          id: 1,
        },
        {
          name: "厨房用品",
          id: 2,
        },
        {
          name: "生活用品",
          id: 3,
        },
      ],
    };
  },
methods: {
    onSubmit() {
      
    }
  },
 <el-form-item label="商品类型" prop="type">
            <el-select
              v-model="ruleForm.type"
              placeholder="请选择商品类型"
            >
              <el-option
                v-for="(item, index) in typeArr"
                :key="index"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="商品名称" prop="name">
            <el-input
              v-model="ruleForm.name"
              placeholder="请输入商品名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="价格" prop="price">
            <el-input
              v-model="ruleForm.price"
              type="number"
              placeholder="请输入商品名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="库存" prop="stock">
            <el-input-number
              v-model="ruleForm.stock"
              placeholder="请输入商品名称"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="编号" prop="number">
            <el-input-number
              v-model="ruleForm.number"
              placeholder="请输入商品名称"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <el-input-number
              v-model="ruleForm.sort"
              placeholder="请输入商品名称"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="是否上架" prop="status">
            <el-switch
              v-model="ruleForm.status"
              :active-value="1"
              :inactive-value="2"
            ></el-switch>
          </el-form-item>
          <el-form-item label="商品描述" prop="desc">
            <el-input
              v-model="ruleForm.desc"
              type="textarea"
              placeholder="请输入商品名称"
            ></el-input>
          </el-form-item>
 rules: {
        type: [
          { required: true, message: "请选择商品分类", trigger: "change" },
        ],
        name: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
        ],
        price: [
          { required: true, message: "请输入商品价格", trigger: "blur" },
        ],
        stock: [
          { required: true, message: "请输入商品库存", trigger: "blur" },
        ],
        sort: [
          { required: true, message: "请输入商品排序", trigger: "blur" },
        ],
        number: [
          { required: true, message: "请输入商品编号", trigger: "blur" },
        ],
        status: [
          { required: true, message: "请选择是否上下架", trigger: "change" },
        ],
        desc: [
          { required: true, message: "请输入商品简介", trigger: "blur" },
        ],
      },
onSubmit(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
/**
 * 新增商品 
 */
 export function addShop(data,id) {
  return request({
    url: '/api/add/goods',
    method: 'post',
    data
    // get 传参数   使用  params
    // put  post  delete  传参 使用 data
  })
}
import {addShop} from '@/api/user';
/**
 * 获取商品分类 /api/query/menu
 */
 export function getShopMenu(data) {
  return request({
    url: '/api/query/menu',
    method: 'get',
    data
    // get 传参数   使用  params
    // put  post  delete  传参 使用 data
  })
}
import {getShopMenu} from '@/api/user';
methods:{
 /**获取商品分类 */
    getShopMenuFun(){
      getShopMenu().then(res=>{
        this.typeArr = res.data
      })
    }
}
 mounted () {
    this.getShopMenuFun();
  },
<el-select v-model="ruleForm.type" placeholder="请选择商品类型">
              <el-option
                v-for="(item, index) in typeArr"
                :key="index"
                :label="item.name"
                :value="item._id"
              ></el-option>
</el-select>
<el-form-item label="商品类型" prop="category_id">
            <el-select v-model="ruleForm.category_id" placeholder="请选择商品类型">
              <el-option
                v-for="(item, index) in typeArr"
                :key="index"
                :label="item.name"
                :value="item._id"
              ></el-option>
            </el-select>
          </el-form-item>
ruleForm: {
        category_id: "",//改这个
        name: "",
        price: "",
        stock: "",
        number: "",
        sort: "",
        status: 1,
      },
rules: {
        category_id: [ //改这个
          { required: true, message: "请选择商品分类", trigger: "change" },
        ],
        name: [{ required: true, message: "请输入商品名称", trigger: "blur" }],
        price: [{ required: true, message: "请输入商品价格", trigger: "blur" }],
        stock: [{ required: true, message: "请输入商品库存", trigger: "blur" }],
        sort: [{ required: true, message: "请输入商品排序", trigger: "blur" }],
        number: [
          { required: true, message: "请输入商品编号", trigger: "blur" },
        ],
        status: [
          { required: true, message: "请选择是否上下架", trigger: "change" },
        ],
        desc: [{ required: true, message: "请输入商品简介", trigger: "blur" }],
      },
onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addShop(this.ruleForm).then((res) => {
            console.log(res);
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
image.png

接下章

上一篇下一篇

猜你喜欢

热点阅读