程序员鸡皮
文章 分类 评论
112 3 31

站点介绍

一名PHP全栈程序员的日常......

Vue3自定义指令参数修饰符值(3)

abzzp 2024-08-31 680 0条评论 前端 vue

首页 / 正文
本站是作为记录一名北漂程序员编程学习以及日常的博客,欢迎添加微信BmzhbjzhB咨询交流......

发布于2024-07-04

自定义指令参数修饰符值

在vue3中我们如何获取自定义的参数的内容,并根据业务来修改展示的内容呢,需要依靠mounted方法中的bindings参数来获取。

参考实例
directives/unit.js文件

export default function directiveUnit(app){
  app.directive("unit",{
    mounted(el,bindings){
      const defaultText = el.textContent
      let unit = bindings.value
      if(!unit){
        unit = "¥"
      }
      el.textContent = unit + defaultText;
    }
  })
}

同时我们需要在directives/index.js中引入

import directiveUnit from "./unit"

export default function directives(app) {
  directiveUnit(app)
}

这里是我们的示例文件

<template>
  <div class="app">
    <!-- 1.参数-修饰符-值  -->
     <h2 v-why:kobe.abc.cba="message">哈哈哈</h2>
     <!-- 2.价格拼接单位符号 -->
      <h2 v-unit>{{ 111 }}</h2>
  </div>
</template>  
<script setup>
import { ref } from 'vue';

const counter = ref(0)

const message = 'Hello world';
const vWhy = {
  mounted(el,bindings){
    console.log(bindings);
    el.textContent = bindings.value;
  }
}
</script>
<style scoped>
</style> 

感谢大家观看,我们下次再见

评论(0)

最新评论

  • 湖山風光

    欣赏你的照片, 我明白, 世界很美。感谢 旅行灵感。

  • 古競技場

    读起来像小说。继续保持 带来的灵感。

  • 奧運聖火

    让人精神焕发的 帖子! 我准备订票了。

  • abzzp

    以后焦虑了或者迷茫了记得回来看看[[流泪]]

  • 三十三瀑布

    我非常喜欢 出行博客。鼓舞人心查看路线。

  • 天星碼頭

    你们的博客 百分百 帮助选择路线。坚持!

  • 日落鼓掌

    信息丰富的 出行资源! 越来越棒!

  • digital banking

    My brother suggested I might like this blog. He was entirely right. This post actually made my day. You cann't imagine just how much time I had spent for this information! Thanks!

  • 碧海藍灣

    我热爱, 写得很实在。你的博客 就是 最好的例子。很出色。

  • digital banking

    I've learn several good stuff here. Certainly price bookmarking for revisiting. I surprise how so much effort you put to make such a great informative website.

日历

2026年02月

1234567
891011121314
15161718192021
22232425262728

文章目录

站点公告
本站是作为记录一名北漂程序员编程学习以及日常的博客,欢迎添加微信BmzhbjzhB咨询交流......
点击小铃铛关闭
配色方案