Vue的常见指令

2018-08-25  本文已影响0人  SlashBoyMr_wang

一、什么是Vue?

它是构建用户界面的JavaScript框架(让它自动生成js,css,html等)

二、Vue的引入

三、初识Vue-----简单demo

每个 Vue 应用都需要通过实例化 Vue 来实现。

语法格式如下:
const app = new Vue({
  // 选项
})

接下来让我们通过实例来看下 Vue 构造器中需要哪些内容:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.js"></script>
</head>
<body>
<div id="app">
    <!--引用数据-->
    {{name}}
    {{age}}
</div>
<script>
    //创建Vue根实例
    const app = new Vue({
        //指定作用域
        el: "#app",
        //数据
        data: {
            name: "wangjifei",
            age: '27',
        }
    })
</script>
</body>
</html>

四、常见指令介绍

<div id="app">
    <p v-text="a"></p>
    <div v-html="b"></div>
</div>
<script>
    //创建Vue根实例
    const app = new Vue({
        el: "#app",
        data: {
            a: "A",
            b: `<h1>我是html</h1>`
        }
    })
</script>
<div id="app">
    用户名:
    <input type="text" v-model.lazy.trim="name">
    <pre>{{name}}</pre>
    <hr>
    手机号:
    <input type="text" v-model.number="phone">
    <pre>{{phone}}</pre>
</div>
<script>
    const app = new Vue({
        el: "#app",
        data: {
            name: "",
            phone: "",
        }
    })
</script>
<div id="app">
    <textarea name="" id="" cols="30" rows="10" v-model="article"></textarea>
    {{article}}
</div>
<script>
    const app = new Vue({
        el: "#app",
        data: {
            article:''
        }
    })
</script>
<div id="app">
    <select v-model="from">
        <option value="1">河北</option>
        <option value="2">山西</option>
    </select>
    <pre>{{from}}</pre>
    <select v-model="where" multiple>
        <option value="1">上地</option>
        <option value="2">西二旗</option>
        <option value="3">三里屯</option>
    </select>
    <pre>{{where}}</pre>
</div>
<script>
    const app = new Vue({
        el: "#app",
        data: {
            from: '1',
            where: '1'
        }
    })
</script>
<div id="app">
    <h1>展示你们的爱好</h1>
    <ul>
        <li v-for="hobby in hobby_list">{{hobby}}</li>
    </ul>

    <h1>展示你们喜欢吃的食物</h1>
    <ul>
        <li v-for="food in food_list">{{food.name}}它的价格是:{{food.price}}</li>
    </ul>
</div>
<script>
    const app = new Vue({
        el: "#app",
        data: {
            hobby_list: ["美女", "LOL", "吃鸡"],
            food_list: [
                {
                    name: "臭豆腐",
                    price: 6,
                },
                {
                    name: "韭菜盒子",
                    price: 10,
                }
            ]
        }
    })
</script>
<div id="app">
    <a :href="path">@马云</a>
</div>
<script>
    const app = new Vue({
        el: "#app",
        data: {
            path: "http://www.taobao.com",
        }
    })
</script>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.js"></script>
    <style>
        .active {
            background: red;
            width: 100px;
            height: 100px;
        }
    </style>
</head>
<body>
<div id="app">
    <!--<a v-bind:href="path">@马云</a>-->
    <a :href="path">@马云</a>
    <button @click="on_click">点我显示盒子的样式</button>
    <div v-on="{mouseenter: onMouseenter, mouseleave: onMouseleave}">鼠标事件</div>
</div>

<script>
    const app = new Vue({
        el: "#app",
        data: {
            path: "http://www.taobao.com",
        },
        methods: {
            on_click: function () {
                this.show = !this.show
            },
            onMouseenter: function () {
                console.log("鼠标移入了")
            },
            onMouseleave: function () {
                console.log("鼠标移出了")
            }
        }
    })
</script>
</body>
</html>
<div id="app">
    <div v-if="role == 'vip'">
        <h1>欢迎会员登陆</h1>
    </div>
    <div v-else-if="role == 'vvip'">
        <h1>您的专属秘书为您服务</h1>
    </div>
    <div v-else>
        <p>gun~~~~~</p>
    </div>
</div>

<script>
    const app = new Vue({
        el: "#app",
        data: {
            role: "vip",
        }
    })
</script>

这里, v-if 指令将根据表达式 "role=='vip'" 的值(true 或 false )来决定是否插入 h1标签。

<div id="app">
    <button @click="on_click">点我</button>
    <p v-show="show">天苍苍</p>
    <p v-if="a =='if_show'">野茫茫</p>
    <p v-else>风吹草低见牛羊</p>
</div>
<script>
    const app = new Vue({
        el: "#app",
        data: {
            show: false,
            a: "if_show"
        },
        methods: {
            on_click: function () {
                this.show = !this.show
            }
        }
    })
</script>
<div id="app">
    <div class="active" v-pin.left.bottom="show">
    </div>
</div>
<script>
    //自定义指令必须调用directive方法来实现,pin是自定义指令名,el为当前绑定自定义指令的html标签,binding是一个包含自定义指令中的各种属性或值的对象,
    Vue.directive("pin", function (el, binding) {
        console.log(el)
        console.log(binding)
        let val = binding.value;
        //modifiers中存储的是自定义指令中的所有修饰符内容
        let positons = binding.modifiers;
        if(val){
            for(let key in positons){
                el.style.position = "fixed";
                console.log(key)
                el.style[key]=0;
            }
        }else {
            el.style.position = "static";
        }
    });
    const app = new Vue({
        el: "#app",
        data: {
            show: true,
        }
    })
</script>
<div id="app">
    <table border="1">
        <thead>
            <th>学科</th>
            <th>分数</th>
        </thead>
        <tbody>
            <tr>
                <td>数学</td>
                <td><input type="text" v-model.number="math"></td>
            </tr>
            <tr>
                <td>英语</td>
                <td><input type="text" v-model.number="english"></td>
            </tr>
            <tr>
                <td>物理</td>
                <td><input type="text" v-model.number="physics"></td>
            </tr>
            <tr>
                <td>总分</td>
                <td>{{sum_num}}
                </td>
            </tr>
            <tr>
                <td>平均分</td>
                <td>{{average}}</td>
            </tr>
        </tbody>
    </table>
</div>
<script>
    const app = new Vue({
        el: "#app",
        data: {
            math: 95,
            english: 59,
            physics: 100,
        },
        computed: {
            sum_num: function () {
                let total = this.math + this.english  + this.physics
                return total
            },
            average: function () {
                let ret = Math.round(this.sum_num/3)
                return ret
            }
        }
    })
</script>
上一篇 下一篇

猜你喜欢

热点阅读