JavaWeb开发-03-Ajax-Vue-Element

目录

一.Ajax

1.Axios

2.案例

二.前后端分离开发

1.介绍

2.YAPI

三.前端工程化

1.环境准备

2.Vue项目简介

3.Vue项目开发流程

四.Vue组件库Element

1. 快速入门

2.常见组件

3.案例

五.Vue路由

六.打包部署


一.Ajax

 

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>原生Ajax</title>
</head>
<body>
    
    <input type="button" value="获取数据" onclick="getData()">

    <div id="div1"></div>
    
</body>
<script>
    function getData(){
        //1. 创建XMLHttpRequest 
        var xmlHttpRequest  = new XMLHttpRequest();
        
        //2. 发送异步请求
        xmlHttpRequest.open('GET','http://yapi.smart-xwork.cn/mock/169327/emp/list');
        xmlHttpRequest.send();//发送请求
        
        //3. 获取服务响应数据
        xmlHttpRequest.onreadystatechange = function(){
            if(xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200){
                document.getElementById('div1').innerHTML = xmlHttpRequest.responseText;
            }
        }
    }
</script>
</html>

1.Axios

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset='UTF-8'>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Ajax-Axios</title>
    <script src="Axios/axios-0.18.0.js">
    </script>
</head>
<body>
    <input type="button" value="获取数据GET" onclick="get()">

    <input type="button" value="删除数据POST" onclick="post()">
    
</body>
<script>
    function get() {
        //通过Axios发送异步请求-get
        axios({
            method: "get",
            url: "http://yapi.smart-xwork.cn/mock/169327/emp/list"
        }).then(result => {
            console.log(result.data);
        })
        
    }

    function post() {
        //通过Axios发送异步请求-pst
        axios({
            method: "post",
            url: "http://yapi.smart-xwork.cn/mock/169327/emp/deleteById",
            data: "id=1"
        }).then(result => {
            console.log(result.data);
        })

    }
    
</script>
</html>

function get() {
    //通过axios发送异步请求 - get
    // axios({
    //     method:"get",
    //     url:"http://localhost:10010/emp/list"
    // }).then(result => {
    //     console.log(result.data);
    // })

    //简写
    axios.get("http://localhost:10010/emp/list").then(result => {
        console.log(result.data);
    })

}

function post() {
    //通过axios发送异步请求 - post
    // axios({
    //     method:"post",
    //     url:"https://mock.apifox.cn/m1/3128855-0-default/emp/deleteById",
    //     data:"id = 1"
    // }).then(result => {
    //     console.log(result.data);
    // })

    //简写
    axios.post("https://mock.apifox.cn/m1/3128855-0-default/emp/deleteById","id=1").then(result => {
        console.log(result.data);
    })

    
}

 

 

<script>
    function get() {
        //通过Axios发送异步请求-get
        // axios({
        //     method: "get",
        //     url: "http://yapi.smart-xwork.cn/mock/169327/emp/list"
        // }).then(result => {
        //     console.log(result.data);
        // })



        axios.get("http://yapi.smart-xwork.cn/mock/169327/emp/list").then(result => {
            console.log(result.data);
        })
        
    }

    function post() {
        //通过Axios发送异步请求-pst
        // axios({
        //     method: "post",
        //     url: "http://yapi.smart-xwork.cn/mock/169327/emp/deleteById",
        //     data: "id=1"
        // }).then(result => {
        //     console.log(result.data);
        // })

        axios.post("http://yapi.smart-xwork.cn/mock/169327/emp/deleteById", "id=1").then(result => {
            console.log(result.data);
        })

    }
    
</script>

2.案例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset='UTF-8'>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Ajax-Axios</title>
    <script src="Axios/axios-0.18.0.js"></script>
    <script src="Vue/vue.js"></script>
</head>
<body>
    <div id="app">
        <table border="1" cellspacing="0" width="60%" align="center">
            <tr>
                <th>编号</th>
                <th>姓名</th>
                <th>图像</th>
                <th>性别</th>
                <th>职位</th>
                <th>入职日期</th>
                <th>最后操作时间</th>
            </tr>
            <tr align="center" v-for="(dat, index) in emp">
                <td>{{dat.id}}</td>
                <td>{{dat.name}}</td>
                <td>
                    <img v-bind:src="dat.image" width="50px">   <!-- v-bind : 为html标签绑定属性值 如: src / href /CSS样式  -->
                </td>
                <td>
                    <span v-if="dat.gender == 1">男</span>
                    <span v-else="dat.gender == 2">女</span>
                </td>
                <td>{{dat.job}}</td>
                <td>{{dat.entrydate}}</td>
                <td>{{dat.updatetime}}</td>
            </tr>

        </table>
    </div>
    
</body>
<script>
    new Vue({
        el: "#app",
        data: {
            emp: [] //定义一个空数组
        },
        method () {
            
        },
        mounted () {
            //发送异步请求,加载数据
            axios.get("http://yapi.smart-xwork.cn/mock/169327/emp/list").then(result => {
               this.emp = result.data.data;
               //this.emp(这个emp)  result.data(固定格式)   .data(链接中的数组/集合名称);
            })
        }
    });
    
</script>
</html>

二.前后端分离开发

1.介绍

 

 

2.YAPI

 YAPI官网icon-default.png?t=N7T8http://yapi.smart-xwork.cn/

 

三.前端工程化

 

 

1.环境准备

 

2.Vue项目简介

 

 

 

devServer: {
    port: 7000
  }

3.Vue项目开发流程

 

 

<template>
  <div>
    <h1>{{message}}</h1>
  </div>
</template>

<script>
export default{
  data: function (){
    return {
      message: "Hello Vue"
    }
  },
  methods: {

  }
}
</script>

<style>
  
</style>

四.Vue组件库Element

Element组件库icon-default.png?t=N7T8https://element.eleme.cn/#/zh-CN/component/table

1. 快速入门

 

 

 

2.常见组件

 

 

 

 

 

 

<template>
    <div>
        <!-- button按钮 -->
        <el-row>
            <el-button>默认按钮</el-button>
            <el-button type="primary">主要按钮</el-button>
            <el-button type="success">成功按钮</el-button>
            <el-button type="info">信息按钮</el-button>
            <el-button type="warning">警告按钮</el-button>
            <el-button type="danger">危险按钮</el-button>
        </el-row>

        <br>

        <!-- Tab表格 -->
        <el-table :data="tableData" border style="width: 100%">
            <el-table-column prop="date" label="日期" width="180"></el-table-column>
            <el-table-column prop="name" label="姓名" width="180"></el-table-column>
            <el-table-column prop="address" label="地址"></el-table-column>
        </el-table>

        <!-- 分页 (格式化快捷键:shift + alt + F)-->
        <el-pagination background layout=" sizes, prev, pager, next, jumper" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" :total="1000"></el-pagination>

        <br><br>

        <!--Dialog 对话框组件 -->
        <el-button type="primary" @click="dialogTableVisible = true">打开嵌套表格的 Dialog</el-button>

        <el-dialog title="收货地址" :visible.sync="dialogTableVisible">
            <el-table :data="gridData">
                <el-table-column property="date" label="日期" width="150"></el-table-column>
                <el-table-column property="name" label="姓名" width="200"></el-table-column>
                <el-table-column property="address" label="地址"></el-table-column>
            </el-table>
        </el-dialog>

        <br><br>
        <!-- Dialog 对话框 -Form -->
        <el-button type="primary" @click="dialogFormVisible = true">form表单</el-button>

        <el-dialog title="form表单" :visible.sync="dialogFormVisible">
            <el-form ref="form" :model="form" label-width="80px">
                <el-form-item label="活动名称">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="活动区域">
                    <el-select v-model="form.region" placeholder="请选择活动区域">
                        <el-option label="区域一" value="shanghai"></el-option>
                        <el-option label="区域二" value="beijing"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="活动时间">
                    <el-col :span="11">
                        <el-date-picker type="date" placeholder="选择日期" v-model="form.date1"
                            style="width: 100%;"></el-date-picker>
                    </el-col>
                    <el-col class="line" :span="2">-</el-col>
                    <el-col :span="11">
                        <el-time-picker placeholder="选择时间" v-model="form.date2" style="width: 100%;"></el-time-picker>
                    </el-col>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="onSubmit">提交</el-button>
                    <el-button>取消</el-button>
                </el-form-item>
            </el-form>


        </el-dialog>


    </div>
</template>

<script>
export default {
    data() {
        return {
            form: {
                name: '',
                region: '',
                date1: '',
                date2: '',
            },
            gridData: [{
                date: '2016-05-02',
                name: '王小虎',
                address: '上海市普陀区金沙江路 1518 弄'
            }, {
                date: '2016-05-04',
                name: '王小虎',
                address: '上海市普陀区金沙江路 1518 弄'
            }, {
                date: '2016-05-01',
                name: '王小虎',
                address: '上海市普陀区金沙江路 1518 弄'
            }, {
                date: '2016-05-03',
                name: '王小虎',
                address: '上海市普陀区金沙江路 1518 弄'
            }],
            dialogTableVisible: false,
            dialogFormVisible: false,
            tableData: [{
                date: '2016-05-02',
                name: '王小虎',
                address: '上海市普陀区金沙江路 1518 弄'
            }, {
                date: '2016-05-04',
                name: '王小虎',
                address: '上海市普陀区金沙江路 1517 弄'
            }, {
                date: '2016-05-01',
                name: '王小虎',
                address: '上海市普陀区金沙江路 1519 弄'
            }, {
                date: '2016-05-03',
                name: '王小虎',
                address: '上海市普陀区金沙江路 1516 弄'
            }]
        }
    },
    methods: {
        onSubmit: function(){
            alert(JSON.stringify(this.form));   //将对象转换成字符串
        },
        handleSizeChange: function (val) {
            alert("每页记录数变化" + val)

        },
        handleCurrentChange: function (val) {
            alert("页码发生变化" + val)

        }
    }

}
</script>

<style></style>

 

3.案例

 

<!--  -->
<template>
    <div>
        <el-container style="height: 700px; border: 1px solid #eee">
            <el-header style="font-size: 40px; background-color: rgb(238, 241, 246)">tLias 智能学习辅助系统</el-header>
            <el-container>
                <el-aside width="230px" style="border: 1px solid #eee">
                    <!-- Aside -->
                    <el-menu :default-openeds="['1', '3']">
                        <el-submenu index="1">
                            <template slot="title"><i class="el-icon-message"></i>系统信息管理</template>
                            <el-menu-item index="1-1">部门管理</el-menu-item>
                            <el-menu-item index="1-2">员工管理</el-menu-item>
                        </el-submenu>

                    </el-menu>
                    <!-- Aside -->
                </el-aside>
                <el-container>

                    <el-main>
                        <!-- Main -->
                        <!-- 表单 -->
                        <el-form :inline="true" :model="searchForm" class="demo-form-inline">
                            <el-form-item label="姓名">
                                <el-input v-model="searchForm.name" placeholder="姓名"></el-input>
                            </el-form-item>
                            <el-form-item label="性别">
                                <el-select v-model="searchForm.gender" placeholder="性别">
                                    <el-option label="男" value="1"></el-option>
                                    <el-option label="女" value="2"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="入职日期">
                                <!-- 日期选择器 -->
                                <el-date-picker v-model="searchForm.entrydate" type="daterange" range-separator="至"
                                    start-placeholder="开始日期" end-placeholder="结束日期">
                                </el-date-picker>
                            </el-form-item>

                            <el-form-item>
                                <el-button type="primary" @click="onSubmit">查询</el-button>
                            </el-form-item>
                        </el-form>


                        <!-- 表格 -->
                        <el-table :data="tableData" :cell-style="rowStyle" border>
                            <el-table-column prop="name" label="姓名" width="180" align="center">
                            </el-table-column>
                            <el-table-column  label="图像" width="180" align="center">
                                <template slot-scope="scope">
                                    <img :src="scope.row.image" width="70px">
                                </template>
                            </el-table-column>
                            <el-table-column label="性别" width="140" align="center">
                                <template slot-scope="scope">
                                    {{scope.row.gender == 1 ? '男' : '女'}}
                                </template>
                            </el-table-column>
                            <el-table-column prop="job" label="职位" width="140" align="center">
                            </el-table-column>
                            <el-table-column prop="entrydate" label="入职日期" width="180" align="center">
                            </el-table-column>
                            <el-table-column prop="updatetime" label="最后操作时间" width="230" align="center">
                            </el-table-column>
                            <el-table-column label="操作" align="center">
                                <el-button type="primary" size="mini">编辑</el-button>
                                <el-button type="danger" size="mini">删除</el-button>
                            </el-table-column>
                        </el-table>
                        <br>
                        <!-- 分页条 -->
                        <!-- <hr style="border-color: #eee;"> -->
                        <el-pagination background layout="total, sizes,prev, pager, next, jumper"
                            @size-change="handleSizeChange" @current-change="handleCurrentChange" :total="1000">
                        </el-pagination>

                        <!-- el-main -->
                    
                </el-main>
                </el-container>
            </el-container>
        </el-container>

    </div>
</template>


<!--  -->
<script>

import axios from 'axios';

export default {
    data() {
        return {
            tableData: [],
            searchForm: {
                name: '',
                gander: '',
                entrydate: []
            }
        }
    },

    methods: {
        handleSizeChange: function (val) {
            alert("每页记录数变化" + val)
        },
        handleCurrentChange: function (val) {
            alert("页码发生变化" + val)
        },
        onSubmit: function() {
            alert(JSON.stringify(this.searchForm))
        },
        rowStyle() { 
            return "text-align:center"
        }
    },
    mounted () {
        //发送异步请求
        axios.get("https://mock.apifox.cn/m1/3128855-0-default/emp/list").then((result) => { 
            this.tableData = result.data.data;
        });

    }
}
</script>


<!--  -->
<style>
.el-table{
    text-align: right;
}

</style>

五.Vue路由

<!--  -->
<template>
    <div>
        <el-container style="height: 700px; border: 1px solid #eee">
            <el-header style="font-size: 40px; background-color: rgb(238, 241, 246)">tLias 智能学习辅助系统</el-header>
            <el-container>
                <el-aside width="230px" style="border: 1px solid #eee">
                    <!-- Aside -->
                    <el-menu :default-openeds="['1', '3']">
                        <el-submenu index="1">
                            <template slot="title"><i class="el-icon-message"></i>系统信息管理</template>
                            <el-menu-item index="1-1">
                                <router-link to="/dept">部门管理</router-link>
                            </el-menu-item>
                            <el-menu-item index="1-2">
                                <router-link to="/emp">员工管理</router-link>
                            </el-menu-item>
                        </el-submenu>
                    </el-menu>
                    <!-- Aside -->
                </el-aside>
                <el-container>

                    <el-main>
                        <!-- Main -->
                        <el-table :data="DeptName" style="width: 100%" border>
                            <el-table-column prop="name" label="姓名" width="180" align="center">
                            </el-table-column>
                            <el-table-column prop="date" label="最后操作时间" width="180" align="center">
                            </el-table-column>
                            <el-table-column label="操作" >
                                <el-button type="primary" size="mini">编辑</el-button>
                                <el-button type="danger" size="mini">删除</el-button>
                            </el-table-column>
                        </el-table>
                    </el-main>
                </el-container>
            </el-container>
        </el-container>

    </div>
</template>

<!--  -->
<script>

export default {
    data() {
        return {
            DeptName: [{
                name: '学工部',
                date: '2016-05-02',
            }, {
                name: '教研部',
                date: '2016-05-04',
            }, {
                name: '就业部',
                date: '2016-05-01',
            }, {
                name: '人事部',
                date: '2016-05-03',
            }, {
                name: '行政部',
                date: '2016-05-03',
            }]

        }
    },

    methods: {

    },
    mounted() {

    }
}
</script>


<!--  -->
<style></style>

 

 

 

此时重新运行有个小bug,会出现空白页面,如下(默认是'' / ''找不到路径)

 

六.打包部署

 

 

 在D:\develop\nginx-1.22.0\conf\nginx.conf 中修改端口号

 

 

 完美进入

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员希西子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值