JavaEE(17)Vue(上)

1. Vue

1. 第一个Vue程序

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div id="app">
    {{msg}}
</div>

</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script>

    var vm = new Vue({
        el:"#app",
        data:{
            msg:"hello,vue!"
        }
    });

</script>

2. v-bind绑定

<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div id="app">
    <span v-bind:title="msg">
        鼠标悬停几秒可以查看动态绑定的信息
    </span>
</div>

</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script>
    var vm = new Vue({
        el: "#app",
        data: {
            msg: "hello,vue!"
        }
    })
</script>

【说明】v-bind绑定:将元素节点的title特性和Vue实例的msg属性保持一致

3. v-if,v-else,v-else-if

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div id="app">

    <h1 v-if="ok">Yes</h1>
    <h1 v-else>No</h1>
    
</div>

</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script>

    var vm = new Vue({
        el: "#app",
        data: {
            ok: true
        }
    });

</script>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div id="app">
    <h1 v-if="score >= 85 && score <= 100">优秀</h1>
    <h1 v-else-if="score >= 60 && score < 85">中等</h1>
    <h1 v-else-if="score >= 30 && score < 60">不及格</h1>
    <h1 v-else="score >= 0 && score < 30"></h1>
</div>

</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script>

    var vm = new Vue({
       el: "#app",
       data: {
           score: 77
       }
    });

</script>

4. v-for

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div id="app">

    <li v-for="(item,index) in items">
        {{item.msg}}=={{index}}
    </li>
    
    <!--
        华为==0
        苹果==1
        诺基亚==2     
    -->

</div>

</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script>
    var vm = new Vue({
        el: "#app",
        data: {
            items: [
                {msg: "华为"},
                {msg: "苹果"},
                {msg: "诺基亚"}
            ]
        }
    });
</script>

5. v-on:绑定事件

<!DOCTYPE html>
<html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div id="app">
    <button v-on:click="sayHello">Click me</button>
</div>

</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script>
    var vm = new Vue({
        el: "#app",
        data: {
            msg: "hello vue~"
        },
        methods: {  //方法必须定义在vue的methods对象中
            sayHello: function () {
                alert(this.msg);
            }
        }
    })
</script>

6. v-model:双向数据绑定
【解释】双向数据绑定:数据发生变化时,视图也发生变化;视图发生变化时,数据也发生变化

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div id="app">
    输入的文本:<input type="text" v-model="msg">
    <br>
    {{msg}}
</div>

</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script>
    var vm = new Vue({
        el: "#app",
        data: {
            msg: ""
        }
    });
</script>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div id="app">

    性别
    <input type="radio" value="" v-model="sex"><input type="radio" value="" v-model="sex"><p>
        选中了==>{{sex}}
    </p>

</div>

</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script>
    var vm = new Vue({
        el: "#app",
        data: {
            sex: ""
        }
    });
</script>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div id="app">
    下拉框
    <select v-model="phone">
        <option value="" disabled>--请选择--</option>
        <option>苹果</option>
        <option>华为</option>
        <option>诺基亚</option>
    </select>

    <p>
        手机品牌:{{phone}}
    </p>

</div>

</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script>
    var vm = new Vue({
        el: "#app",
        data: {
            phone: ""
        }
    });
</script>

2. Vue组件

1. 使用Vue.component()方法注册组件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div id="app">

    <nelws></nelws>

</div>

</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script>

    /*
        注意:
            1.Vue.component必须写在var vm = new Vue前面
            2.第一个"nelws"代表组件的名称
     */
    Vue.component("nelws",{
        template: '<h1>Hello</h1>'
    });

    var vm = new Vue({
        el: "#app"
    });

</script>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div id="app">

    <!--组件:传递给组件中的值:props-->
    <nelws v-for="item in items" v-bind:nel="item"></nelws>
    <!--
	    Java
		C++
		Python
		Go
	-->
</div>

</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script>

    Vue.component("nelws",{
        props: ['nel'],
        template:'<li>{{nel}}</li>'
    });

    var vm = new Vue({
        el: "#app",
        data: {
            items: ["Java","C++","Python","Go"]
        }
    });

</script>

3. axios

1. 简单的axios

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div id="app">

</div>

</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script type="text/javascript" src="https://cdn.bootcss.com/axios/0.19.0-beta.1/axios.js"></script>
<script>
    var vm = new Vue({
        el: "#app",
        mounted(){  //钩子函数
            axios.get("data.json").then(response=>(console.log(response.data)));
        }
    })
</script>

2. 复杂一点的axios

{
  "name":"nelws",
  "url": "http://baidu.com",
  "page": "1",
  "isNonProfit":"true",
  "address": {
    "street": "长安区",
    "city":"陕西西安",
    "country": "中国"
  },
  "links": [
    {
      "name": "B站",
      "url": "https://www.bilibili.com/"
    },
    {
      "name": "4399",
      "url": "https://www.4399.com/"
    },
    {
      "name": "百度",
      "url": "https://www.baidu.com/"
    }
  ]
}
<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div id="app">
    <div>{{info.name}}</div>
    <div>{{info.address.street}}</div>
    <div v-for="link in info.links">
        {{link.name}}=={{link.url}}
    </div>
    <a v-bind:href="info.url">Click me!</a>
</div>

</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script type="text/javascript" src="https://cdn.bootcss.com/axios/0.19.0-beta.1/axios.js"></script>
<script>
    var vm = new Vue({
        el: "#app",

        //data:属性
        data(){
          return{
              //请求的返回参数格式,必须和json字符串的一样
              info:{
                  name: null,
                  address: {
                      street: null,
                      city: null,
                      country: null
                  },
                  url: null,
                  links: {
                      name: null,
                      url: null
                  }
              }
          }
        },
        mounted(){  //钩子函数
            axios.get("data.json").then(response=>(console.log(this.info=response.data)));
        }
    })
</script>

4. 计算属性

1. 概念:计算属性重点突出属性,首先他是个属性,其次有计算能力(计算是动词),这里的计算就是个函数,在调用方法时,每次都需要计算,产生系统开销。不经常变化的可以使用计算属性,计算属性主要就是将不经常发生变化的结果进行缓存,节省系统开销

2. 代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div id="app">

    <!--currentTime1():方法!-->
    <p>Time1 {{currentTime1()}}</p>

    <!--currentTime2:属性!-->
    <p>Time2 {{currentTime2}}</p>

</div>

</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script>

    var vm = new Vue({
       el: "#app",
       data: {
           msg: "hello,vue~"
       },
       methods: {
           currentTime1: function () {
               return Date.now();
           }
       },
       computed: {
           currentTime2: function () {
               return Date.now();
           }
       }
    });

</script>

5. 插槽slot

1. 简单使用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div id="app">

    <todo>
        <todo-title slot="todo-title" v-bind:title="title"></todo-title>
        <todo-items slot="todo-items" v-for="item in items" v-bind:item="item"></todo-items>
    </todo>

</div>

</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script>

    Vue.component("todo",{
       template: '<div>\
                        <slot name="todo-title"></slot>\
                        <ul>\
                            <slot name="todo-items"></slot>\
                        </ul>\
                  </div>'
    });

    Vue.component("todo-title",{
       props: ['title'],
       template: '<div>{{title}}</div>>'
    });

    Vue.component("todo-items",{
        props: ['item'],
        template: '<li>{{item}}</li>'
    });

    var vm = new Vue({
        el: "#app",
        data: {
            title: "编程语言",
            items: ['Java','C++','Python','Go']
        }
    });

</script>

2. 自定义事件分发

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div id="app">

    <todo>
        <todo-title slot="todo-title" v-bind:title="title"></todo-title>
        <todo-items slot="todo-items" v-for="(item,index) in items" v-bind:item="item" v-bind:index="index" v-on:remove="removeItem(index)" v-bind:key="index"></todo-items>
    </todo>

</div>

</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script>

    Vue.component("todo",{
       template: '<div>\
                        <slot name="todo-title"></slot>\
                        <ul>\
                            <slot name="todo-items"></slot>\
                        </ul>\
                  </div>'
    });

    Vue.component("todo-title",{
       props: ['title'],
       template: '<div>{{title}}</div>>'
    });

    Vue.component("todo-items",{
        props: ['item','index'],
        template: '<li>{{index}}--{{item}} <button v-on:click="remove">删除</button></li>',
        methods: {
            remove: function (index) {
                //this.$emit 自定义事件分发
                this.$emit('remove',index);
            }
        }
    });

    var vm = new Vue({
        el: "#app",
        data: {
            title: "编程语言",
            items: ['Java','C++','Python','Go']
        },
        methods: {
            removeItem: function (index) {
                console.log("删除了" + this.items[index] + "OK");
                this.items.splice(index,1);  //一次删除一个元素
            }
        }
    });

</script>
JAVAEE是指Java Platform, Enterprise Edition的缩写,是Java平台中的一个扩展,用于开发企业级应用程序。它提供了一系列的API和工具,以简化企业级应用程序的开发和部署。 Vue是一种流行的JavaScript框架,用于构建用户界面。它允许开发者使用组件化的方式来构建复杂的交互式应用程序。Vue具有简单易用的语法和灵活的生态系统,可以与其他工具和库无缝集成。 在JavaEE中结合使用Vue,可以使用Vue作为前端框架,JavaEE作为后端框架来开发企业级应用程序。前端使用Vue来实现用户界面的交互和展示,后端使用JavaEE来处理业务逻辑和数据存储。 通过使用JavaEEVue,可以实现前后端分离的架构,提高开发效率和代码可维护性。前后端可以通过RESTful风格的API进行数据传输,使用统一格式的JSON来表示数据。 同时,JavaEE提供了丰富的库和工具,如Spring家族、Spring Boot、MyBatis等,可以帮助开发者快速搭建和开发企业级应用程序。而Vue则提供了丰富的组件和工具,如Element UI,可以帮助开发者构建美观且易用的用户界面。 综上所述,JAVAEEVue可以结合使用,实现前后端分离的企业级应用程序开发。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [跟我学制作javaEE网上书店销售管理系统(沙箱支付)springboot+vue](https://blog.csdn.net/zhgl322/article/details/125456716)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [JavaEE 笔记03:基于Vue,SpringBoot的前后端分离的简单作业管理系统](https://blog.csdn.net/Yiang0/article/details/106863440)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值