• vue实现自定义指令和指令的周期


    <template>
      <div>
        <div v-for="(emp, i) in emps" :key="i">
          <span>{{ emp.ename }}span>
          <span>{{ emp.birthday }}span>
          
          <span v-date="emp.birthday">span>
        div>
      div>
    template>
    
    <script>
    export default {
      directives: {
        // v-date
        date(el, bindings) {
          // 如何把时间戳 转成 年/月/日
          var d = new Date(bindings.value)
          var year = d.getFullYear()
          var month = d.getMonth() + 1 // 0-11  +1 转1-12
          var day = d.getDate()
    
          el.innerText = `${year}/${month}/${day}`
        },
      },
      data() {
        return {
          emps: [
            { ename: '壮壮', birthday: 626014629000 },
            { ename: '边边', birthday: 636025629000 },
            { ename: '小海', birthday: 616021629000 },
            { ename: '浩浩', birthday: 606022429000 },
          ],
        }
      },
    }
    script>
    
    <style lang="scss" scoped>style>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    指令的周期

    <template>
      <div>
        <input type="text" />
        <br />
        
        <input type="text" v-focus />
        <br />
        <input type="text" />
      div>
    template>
    
    <script>
    export default {
      directives: {
        // 概念: 生命周期. 一个事物从诞生到销毁的过程
        // 例如人的一生: 肚子里->出生->成长=>死了
        // DOM元素: 内存中先创建 -> 绘制到页面上 -> 销毁
        // 获得焦点:
        // 指令默认是在 DOM的内存阶段触发的, 即元素还没显示到页面上, 导致无法获得焦点
        // focus(el) {
        //   el.focus()
        // },
        // 指令有两种语法:
        // 1. 值是函数: 则在DOM元素创建时自动触发
        // 2. 值是对象类型: 精确配置触发的时机
        focus: {
          // ctrl+i :能看到提示
          // sql:的增语句  insert into 表 value (值),  插入
          // 代表DOM元素 插入到 页面上, 展示出来时
          inserted(el) {
            el.focus()
          },
        },
      },
    }
    script>
    
    <style lang="scss" scoped>style>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
  • 相关阅读:
    python 协程 (概念+示例代码)
    java基础09
    300元开放式耳机哪款好一点、百元开放式耳机性价比挂耳式推荐
    【操作系统】存储器管理:对换
    UE4(unreal engine4)地形编辑工具中的无法显示样条线
    31、学习 Java 中的枚举类型
    PMP 2022-11-01
    蓝蓝设计提供地理信息系统GIS界面设计
    数仓:Doris在美团的应用实践
    Nginx配置转发
  • 原文地址:https://blog.csdn.net/hdj0511/article/details/127415601