Vue2.x案例之商品增删改查的实现

简介: 准备环境; vue-cli方式开发; 校验效果; 传统开发

0x01 准备环境

1. 安装nvm(node与npm)

a. 按流程下载相对于版本的node即可,也会默认安装好对应版本的npm

2. 安装并初始化vue-cli

a. 请参考教程:Vue2.x最简单的两个入门例子


0x02 vue-cli方式开发

1. 启动项目

a. 进入到vue_sny目录下(可以自行创建新项目),执行:

npm run dev

b. 打开端口,可看到相关信息:

localhost:8880

2019013120572525.png

a. 修改vue_sny/src/App.vue文件的内容为:

<template>
  <div id="app">
    <table id="productListTable">
      <thead>
        <tr>
          <th>商品名称</th>
          <th>数量</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="product in products ">
          <td>{{product.productName}}</td>
          <td>{{product.nums}}</td>
          <td>
            <a href="#nowhere" @click="edit(product)">编辑</a>
            <a href="#nowhere" @click="deleteProduct(product.id)">删除</a>
          </td>
        </tr>
        <tr>
          <td colspan="3">      
            商品名称:
            <input type="text" v-model="product4Add.productName" />
            <br> 
            数量:
            <input type="number" v-model="product4Add.nums" />
            <br>
            <button type="button" v-on:click="add">增加</button>
          </td>
        </tr>
      </tbody>
    </table>
    <div id="div4Update" v-show="false">
      商品名称:
      <input type="text" v-model="product4Update.productName" />
      <br> 
      数量:
      <input type="number" v-model="product4Update.nums" />
      <input type="hidden" v-model="product4Update.id" />
      <br>
      <button type="button" v-on:click="update">修改</button>
      <button type="button" v-on:click="cancel">取消</button>
    </div>
  </div>
</template>
<script>
    //Model
    var data = {
        products: [
          { id: 1, productName: '袜子', nums: 15},
          { id: 2, productName: '羊毛衫', nums: 20},
          { id: 3, productName: '雪纺衫', nums: 24},
          { id: 4, productName: '高跟鞋', nums: 30}
        ],
        product4Add: { id: 0, productName: '', nums: '0'},
        product4Update: { id: 0, productName: '', nums: '0'}
    };
    var maxId = 5;
    for (var i=0;i<data.products.length;i++){
        if (data.products[i].id > maxId)
            maxId=  this.products[i].id;
    }
  export default {
    name: 'App',
    data: function() {
      return data;
    },
    methods: {
      add: function (event) {
                maxId++;
                this.product4Add.id = maxId;
                if(this.product4Add.productName.length==0)
                    this.product4Add.productName = "product#"+this.product4Add.id;
                //将对象加入到数组
                this.products.push(this.product4Add);
                this.product4Add = { id: 0, productName: '', nums: '0'}
            },
            deleteProduct: function (id) {
                console.log("id"+id);
                for (var i=0;i<this.products.length;i++){
                    if (this.products[i].id == id) {
                        this.products.splice(i, 1);
                        break;
                    }
                }
            },
            edit: function (product) {
                $("#productListTable").hide();
                $("#div4Update").show();
                this.product4Update = product;
            },
            update:function(){
                $("#productListTable").show();
                $("#div4Update").hide();          
            },
            cancel:function(){
                //恢复显示
                $("#productListTable").show();
                $("#div4Update").hide();
            },
    },
  }
</script>
<style type="text/css">
  td {
    border: 1px solid #000;
  }
</style>

3. 代码讲解

a. App.vue主要分为三部分template、script、style,分别对应着html、js、css:

<template>
</template>
<script>
</script>
<style>
</style>

b. 一般情况下用了vue,不再使用jQuery,此处为了对比学习,特地加入了jQuery,此外:#div4Update初还加入了v-show=“false”,来进行初始化,隐藏编辑框。

c. methods记得加上s。


0x03 校验效果

1. 详细步骤

a. 刷新localhost:8880界面,可看到内容:

20190211161331934.png

b. 然后自行进行增删改查操作进行校验,发现添加与删除都成功,但编辑无法成功。

2. 引入jQuery

a. 在浏览器按F12,点console:

Uncaught ReferenceError: $ is not defined

原因:$无法使用

解决:引入jQuery

b. 安装jQuery

ctrl+c停止之前的命令行窗口,执行

npm install --save jquery

出现警告,不管就行:

$ npm install --save jquery
npm WARN ajv-keywords@2.1.1 requires a peer of ajv@^5.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.7 (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.7: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
+ jquery@3.3.1
added 1 package from 1 contributor and audited 30845 packages in 38.583s
found 8 vulnerabilities (1 low, 1 moderate, 5 high, 1 critical)
  run `npm audit fix` to fix them, or `npm audit` for details

c. 修改webpack配置文件(项目/build/webpack.base.conf.js)

在开头引入webpack,因为该文件默认没有引用:

const webpack = require('webpack')


20190211163010936.png

引入插件:

// 添加代码
plugins: [
  new webpack.ProvidePlugin({
    $: "jquery",
    jQuery: "jquery",
    jquery: "jquery",
    "window.jQuery": "jquery"
  })
],

20190211163350463.png

d. 在main.js引入jQuery:

import $ from 'jquery'

e. 然后启动项目:

npm run dev

f. 重新校验,发现成功了!

0x04 传统开发

1. 非vue-cli方式代码

<html>
<head>
  <meta charset="utf-8"/>
    <script src="https://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/vue/2.5.22/vue.min.js"></script>
    <style type="text/css">
        td{
            border:1px solid #000;
        }     
    </style>
</head>
<body>
  <div id="app">
    <table id="productListTable">
      <thead>
        <tr>
          <th>商品名称</th>
          <th>数量</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="product in products">
          <td>{{product.productName}}</td>
          <td>{{product.nums}}</td>
          <td>
            <a href="#nowhere" @click="edit(product)">编辑</a>
            <a href="#nowhere" @click="deleteProduct(product.id)">删除</a>
          </td>
        </tr>
        <tr>
          <td colspan="3">      
            商品名称:
            <input type="text" v-model="product4Add.productName" />
            <br> 
            数量:
            <input type="number" v-model="product4Add.nums" />
            <br>
            <button type="button" v-on:click="add">增加</button>
          </td>
        </tr>
      </tbody>
    </table>
    <div id="div4Update">
      商品名称:
      <input type="text" v-model="product4Update.productName" />
      <br> 
      数量:
      <input type="number" v-model="product4Update.nums" />
      <input type="hidden" v-model="product4Update.id" />
      <br>
      <button type="button" v-on:click="update">修改</button>
      <button type="button" v-on:click="cancel">取消</button>
    </div>
  </div>
    <script type="text/javascript">
        $("#div4Update").hide();
        //Model
        var data = {
            products: [
            { id: 1, productName: '袜子', nums: 15},
            { id: 2, productName: '羊毛衫', nums: 20},
            { id: 3, productName: '雪纺衫', nums: 24},
            { id: 4, productName: '高跟鞋', nums: 30}
            ],
            product4Add: { id: 0, productName: '', nums: '0'},
            product4Update: { id: 0, productName: '', nums: '0'}
        };
        var maxId = 5;
        for (var i=0;i<data.products.length;i++){
            if (data.products[i].id > maxId)
                maxId=  this.products[i].id;
        }      
    //ViewModel
    var vue = new Vue({
        el: '#app',
        data: data,
        methods: {
            add: function (event) {
                maxId++;
                this.product4Add.id = maxId;
                if(this.product4Add.productName.length==0)
                    this.product4Add.productName = "product-"+this.product4Add.id;
                //将对象加入到数组
                this.products.push(this.product4Add);
                this.product4Add = { id: 0, productName: '', nums: '0'}
            },
            deleteProduct: function (id) {
                console.log("id"+id);
                for (var i=0;i<this.products.length;i++){
                    if (this.products[i].id == id) {
                        this.products.splice(i, 1);
                        break;
                    }
                }
            },
            edit: function (product) {
                $("#productListTable").hide();
                $("#div4Update").show();
                this.product4Update = product;
            },
            update:function(){
                $("#productListTable").show();
                $("#div4Update").hide();          
            },
            cancel:function(){
                //恢复显示
                $("#productListTable").show();
                $("#div4Update").hide();
            }
        }
    });
    </script>
</body>
</html>

2. 两种方式的比较

a. $("#div4Update").hide();对应着<div id="div4Update"v-show="false"

b. 传统方式使用cdn方式引入了vue与jquery,方便很多


0xFF 总结


  1. 本教程实现了一个简单的增删改查小案例,实现前后端分离开发可视化(进阶篇),请读者思考如何通过vue直接获取后台的数据。
  2. 本教程仍然使用了两种方式来实现一个案例,读者们请对比学习,关于webpack的相关知识请自行搜寻资料学习,此处没计划出教程。


相关文章
|
22天前
|
SQL 数据库
数据库开发之子查询案例的详细解析
数据库开发之子查询案例的详细解析
16 0
|
22天前
|
SQL 存储 数据库
数据库开发表操作案例的详细解析
数据库开发表操作案例的详细解析
13 0
|
22天前
|
存储 SQL 数据库
数据库设计案例:电商系统数据库设计实践
数据库设计案例:电商系统数据库设计实践
126 1
|
22天前
|
关系型数据库 MySQL 数据库
|
6天前
|
SQL 关系型数据库 MySQL
mysql 数据库 增删改查 基本操作
mysql 数据库 增删改查 基本操作
|
11天前
|
小程序 数据库
一分钟教你学会小程序云开发的数据库的增删改查
本文简要介绍了小程序云开发中数据库的增删改查操作。首先展示了查询代码,通过`wx.cloud.database().collection().get()`获取数据;接着演示添加数据,强调数据字段需与数据库匹配;然后说明删除数据需基于_id,通过`doc(id).remove()`执行;最后,更新数据同样依赖_id,使用`doc(id).update()`进行修改。
19 1
|
15天前
|
SQL 数据库
数据库数据恢复—sqlserver数据库分区空间不足导致故障的数据恢复案例
数据库数据恢复环境: 某品牌r520服务器,服务器中有7块SAS硬盘,这7块硬盘组建了一组2盘raid1阵列和一组5盘raid5阵列,raid1阵列存储空间安装操作系统,raid5阵列存储空间存放数据。服务器上部署sql server数据库,数据库存放在C盘。 数据库故障: 工作人员发现服务器的C盘容量即将耗尽,于是将sql server数据库路径指向D盘,在D盘生成了一个.ndf文件。一个多星期后,sql server数据库出现故障,连接失效,无法正常附加查询。
数据库数据恢复—sqlserver数据库分区空间不足导致故障的数据恢复案例
|
22天前
|
SQL 存储 小程序
数据库数据恢复—Sql Server数据库文件丢失的数据恢复案例
数据库数据恢复环境: 5块硬盘组建一组RAID5阵列,划分LUN供windows系统服务器使用。windows系统服务器内运行了Sql Server数据库,存储空间在操作系统层面划分了三个逻辑分区。 数据库故障: 数据库文件丢失,主要涉及3个数据库,数千张表。数据库文件丢失原因未知,不能确定丢失的数据库文件的存放位置。数据库文件丢失后,服务器仍处于开机状态,所幸未写入大量数据。
数据库数据恢复—Sql Server数据库文件丢失的数据恢复案例
|
22天前
|
SQL 存储 数据挖掘
数据库数据恢复—数据库ndf文件大小变为0KB的数据恢复案例
存储设备损坏导致存储中SQL Server数据库崩溃。对数据库文件进行恢复后,用户发现有4个ndf文件的大小变为0KB。该SQL Server数据库每10天生成一个大小相同的NDF文件,该SQL Server数据库包含两个LDF文件。
|
22天前
|
关系型数据库 MySQL Linux
【MySQL-10】数据库函数-案例演示【字符串/数值/日期/流程控制函数】(代码演示&可cv代码)
【MySQL-10】数据库函数-案例演示【字符串/数值/日期/流程控制函数】(代码演示&可cv代码)
【MySQL-10】数据库函数-案例演示【字符串/数值/日期/流程控制函数】(代码演示&可cv代码)