Vue常用的12个系统指令以及自定义指令

用指令前必先了解指令的作用,用在什么地方
指令:
作用:增强HTML标签功能
a.所有指令都是v-开头
b.所有指令代码位置:标签的开头标签位置
c.所有指令都是取代之前的DOM操作

一、12个常用的系统指令

  1. v-text (类似于js中innerText)
    a.替换标签全部内容
    b.不可以识别字符串中标签

  2. v-html (类似于js中innerHTML)
    a.替换标签全部内容
    b.可以识别字符串中标签

   <div id="app">
        <!-- v-text替换所有的内容 -->
        <h2 v-text='msg'>替换全部</h2>
        <h2>是多少:{
   {
   msg}}</h2>
        <hr>
        <h2 v-text='str'></h2>
        <hr>
        <!-- v-html能识别字符串标签 -->
        <h2 v-html='str'></h2>
    </div>
    <script>
        new Vue({
   
            el:"#app",
            data:{
   
                msg:111,
                str:'<span οnclick="alert(1)">里面的内容</span>'
            },
            methods:{
   

            }
        });
    </script>
  1. v-on
    作用:绑定事件
    语法: v-on:事件名=“methods中的方法”
    ’ v-on: ‘ 简写为 ’ @ ‘

    另一种语法:
    @事件名.修饰符 = “methods中的方法”
    once(只执行一次)和prevent(阻止)常用修饰符

    <div id="app">
        {
   {
   count}}
        <button v-on:click="count = count+10">按钮</button>
        <button v-on:click="fn1()">按钮1</button>
        <!-- 没有参数时,可以省略括号 -->
        <button v-on:click= "fn1">按钮11</button>
        <!-- v-on: 简写为  @ -->
        <button @click = "fn2()">按钮2</button>
        <!-- 传参 -->
        <button @click = "fn3(100)">按钮3</button>
        <!-- $event代表当前的事件对象 -->
        <button @click = "fn4($event)">按钮4</button>
        <!-- @事件名.修饰符 = "methods中的方法"  once和prevent-->
        <!-- once只执行一次 -->
        <button @click.once="fn2()">按钮5</button>
    </div>
    <script>
        new Vue({
   
            el:"#app",
            data:{
   
                count:1,
            },
            methods:{
   
                fn1(){
   
                    console.log("fn1----");
                },
                fn2(){
   
                    console.log("fn2----");
                },
                fn3(num){
   
                    console.log(num);
                },
                fn4(e){
   
                    console.log
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值