设为首页 加入收藏

TOP

Vue 常用指令(六)
2019-09-17 15:21:16 】 浏览:69
Tags:Vue 常用 指令
令:自定义指令

遇到一些复杂的需求,vue提供的指令不能完美的处理,有时候我们需要自定义指令,针对这一需求,vue提供了自定义指令,如下所示:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>自定义属性</title>
    <style>
        .box {
            width: 100px;
            height: 100px;
            border: 1px;
            background-color: #ff0000;
        }
    </style>
    <script src="./vue.js"></script>
</head>
<body>
    <div id="app01" class="box" v-pos.left.bottom="post">

    </div>

    <script>
        Vue.directive("pos", function (el, bindding) {
            let decorators = bindding.modifiers;
            if (bindding.value) {
                el.style['position'] = 'fixed';
                for (let key in decorators) {
                    el.style[key] = 0;
                }
            } else {
                el.style.position = "static";
            }
        });
        // 自定义属性
        let app01 = new Vue({
            el: "#app01",
            data: {
                post: true
            }
        })
    </script>

</body>
</html>

el是我们自定义指令所在的DOM元素;

bindding是自定义指令的一些具体数据,请记住,最重要的一点是,不管是现在还是将来,任何情况下,我们都需要根据数据进行业务处理,所以,此处最关键的也是数据,即bindding.value。


常用指令:获取DOM元素

某些情况下,我们需要直接获取DOM元素,并对元素进行一些加工处理。vue提供给我们$refs来获取DOM元素

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>获取DOM</title>
    <script src="./vue.js"></script>
</head>
<body>
    <div id="app01">
        <div ref="myBox">dogfa</div>
        <button v-on:click="dogfa">点击dogfa变红</button>
    </div>


    <script>
        // 错误实例button放在div外面
        let app01 = new Vue({
            el: "#app01",
            methods: {
                dogfa: function () {
                    this.$refs.myBox.style.color = "red";
                }
            }
        })
    </script>
</body>
</html>

在DOM元素上面绑定了ref之后,vue根实例上面就会出现$refs变量,它是一个object类型,key为ref后面的自定义名称,而value为DOM元素。我们通过this.$refs拿到object,之后就可以通过自定义的key名,来找到DOM元素


二、案例:todoList

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>使用Vue实现todoList</title>
  <script src="../statics/vue.min.js"></script>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    .container {
      width: 500px;
      height: auto;
      margin: 0 auto;
    }
    .container .header {
      text-align: center
    }
    li {
      list-style-type: none
    }
    div.left {
      float: left;
      margin-left: 50px;
    }
    div.right {
      float: right;
      margin-right: 50px;
    }
    li {
      margin-top: 5px;
    }
  </style>
</head>
<body>

  <div id="app">
    <div class="container">
      <!--头部添加待办-->
      <div class="header">
        <input id="addthings" @keyup.enter="todoEnter" v-model="todo">
        <span @click="todoEnter">添加待办</span>
      </div>

      <!--已办和待办最外层的盒子-->
      <div class="box">
        <!--已办事项-->
        <div class="left">
          <span>待办事项</span>
          <div class="todos">
            <ul>
              <li v-for="(todo, index) in todoThings" :key="index">
                <input type="radio" name="todo" v-on:click="addThing(index)"><span>{{ todo }}</span>
              </li>
            </ul>
首页 上一页 3 4 5 6 7 下一页 尾页 6/7/7
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇小tips:JS/CSS实现字符串单词首.. 下一篇JS核心之DOM操作 下

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目