VUE二:基本语法

本文详细介绍了Vue.js的基本语法,包括悬停提示信息、条件判断、循环操作、事件绑定、双向数据绑定、组件使用以及如何进行Axios异步通信和计算属性的设置,是初学者入门Vue.js的重要参考资料。
摘要由CSDN通过智能技术生成

悬停提示信息

<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<!--View层,模板-->
<div id="app">
    <span v-bind:title="message">鼠标悬停几秒钟查看提示信息!!!</span>
</div>
<script>
    var vm = new Vue({
        el: "#app",
        //Model:数据
        data: {
            message: "hello Vue!"
        }
    });
</script>
</body>
</html>

判断

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
    <h1 v-if="type==='A'">A</h1>
    <h1 v-else-if="type==='B'">B</h1>
    <h1 v-else="type==='C'">C</h1>
</div>
<script>
    var vm = new Vue({
        el: "#app",
        data: {
            type: 'A'
        }
    });
</script>
</body>
</html>

循环

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>

</head>
<body>
<div id="app">
    <li v-for="item in items">
        {{item.message}}
    </li>
</div>
<script>
    var vm = new Vue({
        el: "#app",
        data: {
            items: [
                {message: 'Java'},
                {message: 'Python'}
            ]
        }
    });
</script>
</body>
</html>

事件绑定

<!DOCTYPE html>
<html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>

</head>
<body>
<div id="app">
    <button v-on:click="say">点击</button>
</div>
<script>
    var vm = new Vue({
        el: '#app',
        data: {
            message: 'Hello Vue!!!'
        },
        //方法必须定义在Vue实例的methods对象中
        methods:{
            say: function (event) {
                //this在方法里指向当前Vue实例
                alert(this.message);
            }
        }
    });
</script>
</body>
</html>

双向绑定

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>

</head>
<body>
<div id="app">
    <select v-model="select">
        <option value="" disabled>请选择:</option>
        <option>Java</option>
        <option>C++</option>
        <option>Python</option>
    </select>
    <span>选择的值为:{{select}}</span>
</div>
<script>
    var vm = new Vue({
        el: '#app',
        data: {
            select: ''
        }
    });
</script>
</body>
</html>

组件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>

</head>
<body>
<div id="app">
<!--传递给组件中的值:props-->
    <test v-for="item in items" v-bind:t="item"></test>
</div>
<script>
<!--定义一个Vue组件component-->
    Vue.component("test",{
        props: ['t'],
        template: '<li>{{t}}</li>'
    });
    var vm = new Vue({
        el:'#app',
        data: {
            items: ["Java","Python","C++"]
        }
    });
</script>
</body>
</html>

Axios异步通信

先创建一个json文件

{
  "name": "嘵奇",
  "url": "https://www.baidu.com",
  "page": 1,
  "isNonProfit": true,
  "address": {
    "street": "松岭路",
    "city": "山东青岛",
    "country": "中国"
  },
  "links": [
    {
      "name": "CSDN",
      "url": "https://mp.csdn.net/console/article"
    },
    {
      "name": "JD",
      "url": "https://prodev.jd.com/"
    }
  ]
}
<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script type="text/javascript"></script>
<!--解决闪烁问题-->
    <style>
        [v-clock]{
            display: none;
        }
    </style>

</head>
<body>
<div id="app" v-clock>
    <li>{{info.name}}</li>
    <li>{{info.address.country}}</li>
    <a v-bind:href="info.url">点击跳转</a>
</div>
<script>
    var vm = new Vue({
        el: "#app",
        data(){
            return{
                info:{
                    name: null,
                    address: {
                        street: null,
                        city: null,
                        country: null
                    },
                    url: null
                }
            }
        },
        mounted(){
            axios.get('../Axios异步通信/data.json').then(response=>(this.info=response.data));
        }
    });
</script>

</body>
</html>

计算属性

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
    <p>方法:{{currentTime1()}}</p>
    <p>属性:{{currentTime2}}</p>
</div>
<script>
    var vm = new Vue({
        el: "#app",
        data: {
            message: 'hello Vue!!!'
        },
        methods: {
            currentTime1:function () {
                return Date.now();
            }
        },
        computed: {
            currentTime2: function () {
                this.message;
                return Date.now();
            }
        }
    });
</script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值