学生管理系统

01-Vue高级使用-自定义组件

  • 学完了 Element 组件后,我们会发现组件其实就是自定义的标签。例如 就是对的封装。

  • 本质上,组件是带有一个名字且可复用的 Vue 实例,我们完全可以自己定义。

  • 定义格式

    Vue.component(组件名称, {
     props:组件的属性,
     data: 组件的数据函数,
     template: 组件解析的标签模板
    })
    
  • 代码实现

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>自定义组件</title>
        <script src="vue/vue.js"></script>
    </head>
    <body>
        <div id="div">
            <my-button>我的按钮</my-button>
        </div>
    </body>
    <script>
        Vue.component("my-button",{
            // 属性
            props:["style"],
            // 数据函数
            data: function(){
                return{
                    msg:"我的按钮"
                }
            },
            //解析标签模板
            template:"<button style='color:red'>{{msg}}</button>"
        });
    
        new Vue({
            el:"#div"
        });
    </script>
    </html>
    

02-Vue高级使用-Vue的生命周期

  • 生命周期

  • 生命周期的八个阶段

  • 代码实现

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>生命周期</title>
        <script src="vue/vue.js"></script>
    </head>
    <body>
        <div id="app">
            {{message}}
        </div>
    </body>
    <script>
        let vm = new Vue({
    				el: '#app',
    				data: {
    					message: 'Vue的生命周期'
    				},
    				beforeCreate: function() {
    					console.group('------beforeCreate创建前状态------');
    					console.log("%c%s", "color:red", "el     : " + this.$el); //undefined
    					console.log("%c%s", "color:red", "data   : " + this.$data); //undefined 
    					console.log("%c%s", "color:red", "message: " + this.message);//undefined
    				},
    				created: function() {
    					console.group('------created创建完毕状态------');
    					console.log("%c%s", "color:red", "el     : " + this.$el); //undefined
    					console.log("%c%s", "color:red", "data   : " + this.$data); //已被初始化 
    					console.log("%c%s", "color:red", "message: " + this.message); //已被初始化
    				},
    				beforeMount: function() {
    					console.group('------beforeMount挂载前状态------');
    					console.log("%c%s", "color:red", "el     : " + (this.$el)); //已被初始化
    					console.log(this.$el);
    					console.log("%c%s", "color:red", "data   : " + this.$data); //已被初始化  
    					console.log("%c%s", "color:red", "message: " + this.message); //已被初始化  
    				},
    				mounted: function() {
    					console.group('------mounted 挂载结束状态------');
    					console.log("%c%s", "color:red", "el     : " + this.$el); //已被初始化
    					console.log(this.$el);
    					console.log("%c%s", "color:red", "data   : " + this.$data); //已被初始化
    					console.log("%c%s", "color:red", "message: " + this.message); //已被初始化 
    				},
    				beforeUpdate: function() {
    					console.group('beforeUpdate 更新前状态===============》');
    					let dom = document.getElementById("app").innerHTML;
    					console.log(dom);
    					console.log("%c%s", "color:red", "el     : " + this.$el);
    					console.log(this.$el);
    					console.log("%c%s", "color:red", "data   : " + this.$data);
    					console.log("%c%s", "color:red", "message: " + this.message);
    				},
    				updated: function() {
    					console.group('updated 更新完成状态===============》');
    					let dom = document.getElementById("app").innerHTML;
    					console.log(dom);
    					console.log("%c%s", "color:red", "el     : " + this.$el);
    					console.log(this.$el);
    					console.log("%c%s", "color:red", "data   : " + this.$data);
    					console.log("%c%s", "color:red", "message: " + this.message);
    				},
    				beforeDestroy: function() {
    					console.group('beforeDestroy 销毁前状态===============》');
    					console.log("%c%s", "color:red", "el     : " + this.$el);
    					console.log(this.$el);
    					console.log("%c%s", "color:red", "data   : " + this.$data);
    					console.log("%c%s", "color:red", "message: " + this.message);
    				},
    				destroyed: function() {
    					console.group('destroyed 销毁完成状态===============》');
    					console.log("%c%s", "color:red", "el     : " + this.$el);
    					console.log(this.$el);
    					console.log("%c%s", "color:red", "data   : " + this.$data);
    					console.log("%c%s", "color:red", "message: " + this.message);
    				}
    			});
    
    		
    			// 销毁Vue对象
    			//vm.$destroy();
    			//vm.message = "hehe";	// 销毁后 Vue 实例会解绑所有内容
    
    			// 设置data中message数据值
    			vm.message = "good...";
    </script>
    </html>
    

03-Vue高级使用-异步操作

  • 在Vue中发送异步请求,本质上还是AJAX。我们可以使用axios这个插件来简化操作!

  • 使用步骤
    1.引入axios核心js文件。
    2.调用axios对象的方法来发起异步请求。
    3.调用axios对象的方法来处理响应的数据。

  • axios常用方法

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ymNe8dl9-1601978496716)(.\img\axios常用方法.png)]

  • 代码实现

    • html代码
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>异步操作</title>
        <script src="js/vue.js"></script>
        <script src="js/axios-0.18.0.js"></script>
    </head>
    <body>
        <div id="div">
            {{name}}
            <button @click="send()">发起请求</button>
        </div>
    </body>
    <script>
        new Vue({
            el:"#div",
            data:{
                name:"张三"
            },
            methods:{
                send(){
                    // GET方式请求
                    // axios.get("testServlet?name=" + this.name)
                    //     .then(resp => {
                    //         alert(resp.data);
                    //     })
                    //     .catch(error => {
                    //         alert(error);
                    //     })
    
                    // POST方式请求
                    axios.post("testServlet","name="+this.name)
                        .then(resp => {
                            alert(resp.data);
                        })
                        .catch(error => {
                            alert(error);
                        })
                }
            }
        });
    </script>
    </html>
    
    • java代码
    package com.itheima;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    @WebServlet("/testServlet")
    public class TestServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //设置请求和响应的编码
            req.setCharacterEncoding("UTF-8");
            resp.setContentType("text/html;charset=UTF-8");
    
            //获取请求参数
            String name = req.getParameter("name");
            System.out.println(name);
    
            //响应客户端
            resp.getWriter().write("请求成功");
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            this.doGet(req,resp);
        }
    }
    

04-Vue高级使用-高级使用的小结

  • 自定义组件:本质上,组件是带有一个名字且可复用的 Vue 实例,我们可以自己来定义。

    Vue.component(组件名称, {
     props:组件的属性,
     data: 组件的数据函数,
     template: 组件解析的标签模板
    })
    
  • 生命周期:核心八个阶段
    beforeCreate:创建前
    created:创建后
    beforeMount:载入前
    mounted:载入后
    beforeUpdate:更新前
    updated:更新后
    beforeDestroy:销毁前
    destroyed:销毁后

  • 异步操作:通过 axios 插件来实现。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值