温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

vue中的for循环及自定义指令怎么用

发布时间:2022-08-31 10:17:27 来源:亿速云 阅读:225 作者:iii 栏目:开发技术

这篇“vue中的for循环及自定义指令怎么用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“vue中的for循环及自定义指令怎么用”文章吧。

vue for循环及自定义指令

v-for

1.v-for用来循环的数组怎么发生变化可以被vue检测到:

push、pop、shift、unshift、splice、sort、reverse等方法可以被检测到

vue对于这些方法的处理是重写了这些方法,并在最后会触发一次notify方法来通知这个array已经发生变化

vue还增加了两个方法来观测array的变化:

  • $set:如果直接设置array中的元素,不会触发视图的变化

this.selectArray[1] = 'newValue'  // 不会触发视图变化
this.selectArray.$set(1, 'newValue') // 会触发视图变化
  • $remove:是splice的语法糖,用来从目标元素中查找并且删除这个元素

let itemIndex = this.selectArray.indexOf(selectItem)
this.selectArray.splice(itemIndex,1) // 删除这个元素
this.selectArray.$remove(selectItem) // 同样效果,不用查找index

vue不能检测到下面数组的变化:

使用索引设置元素:

this.selectArray[1] = 'newValue'

解决办法:使用$set方法

修改数据的长度:

this.selectArray.length = 0

解决方法:使用空数组来替换:this.selectArray = []

2.使用v-for遍历对象

使用别名

<li v-for = "(key,value) in obj"> {{key}}-{{value}}</li>

不使用别名,使用$key

<li v-for = "value in obj"> {{$key}}-{{value}} </li>

注意:es5无法检测到对象增加新属性,所以vue提供了三个方法来监视对象属性:

  • $add(key,value)

  • $set(key,value)

  • $delete(key)

自定义指令

Vue.directive('directiveName',{
    // 这里就是指令对象的内部
    // 可以使用this来获取有用的参数
    bind: () => {
        //  准备工作:添加事件处理器等
        dom.addEventListener........
    },
    update: (oldVal,newVal) => {
        // 值更新的时候的工作
        //  初始化的时候也会被调用
    },
    unbind: () => {
        // 清理工作,比如接触bind添加的事件处理器
    }
})

Vue.directive('directiveName',(value) => {
    // 代替update函数
})
// 使用指令
<div directiveName="argumentValue"></div>

在指令对象中,可以只用this来获取一些有用的参数:

  • this.el: 指令绑定的元素

  • this.vm:指令的上下文viewModel

  • this.expression: 指令的表达式

  • this.arg:指令的参数

  • this.name: 指令的名字

  • this.modifiers:一个对象,指令的修饰符

  • this.descriptor: 一个对象,指令的解析结果

vue自定义指令动态参数

通过自定义指令中的修饰符的key作为值,更改显示的颜色

动态指令参数

当参数是动态的时候。

main.js

//当参数的值是动态的时候
Vue.directive('color2', {
  bind: function(el, binding) {
    el.style.color = binding.value;
  }
})
Vue.directive('color3', {
  bind: function(el, binding) {
    el.style.color = binding.arg;
  }
})

template.vue中

<template>
<div class="demo">
  <!-- value -->
  <p v-color2='purpleUser'><i class="el-icon-user-solid"></i></p>
  <p v-color2='redUser'><i class="el-icon-user-solid"></i></p>
  <p v-color2='greenUser'><i class="el-icon-user-solid"></i></p>
  <!-- arg -->
  <p v-color3:[purpleUser]><i class="el-icon-user-solid"></i></p>
  <p v-color3:[redUser]><i class="el-icon-user-solid"></i></p>
  <p v-color3:[greenUser]><i class="el-icon-user-solid"></i></p>
</div>
</template>
<script>
export default {
  data() {
    return {
      purpleUser: 'purple',
      redUser: 'red',
      greenUser: 'green'
    }
  },
  created() {},
  methods: {}
}
</script>
<style lange='scss' scoped>
p {
  display: inline-block;
  font-size: 40px;
}
</style>

参数是静态的,将key的值作为value,改变颜色

main.js

Vue.directive('color', {
  bind: function(el, binding) {
    const color = Object.keys(binding.modifiers)[0]; //将key的值作为value,改变颜色,Object.keys获取key的值;
    el.style.color = color;
  }
})

template.vue中

<template>
<div class="demo">
  <p v-color.purple><i class="el-icon-user-solid"></i></p>
  <p v-color.red><i class="el-icon-user-solid"></i></p>
  <p v-color.green><i class="el-icon-user-solid"></i></p>
</div>
</template>
<script>
export default {
  data() {
    return {}
  },
  created() {},
  methods: {}
}
</script>
<style lange='scss' scoped>
p {
  display: inline-block;
  font-size: 40px;
}
</style>

以上的方法,最终实现效果是一样的。

vue中的for循环及自定义指令怎么用

以上就是关于“vue中的for循环及自定义指令怎么用”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI