vue指令

1895人阅读

前言

大家好,我是 CoderBin,太久没碰 Vue 了,搞的连一些最基本的指令写法都有点模糊了,所以这一次就对Vue所有的内置指令进行回顾总结,前面先说明一些常用指令,不常用的放在后面。

 

0. 插值表达式

说明:插值表达式也叫Mustache语法(即双大括号),双大括号标签会被替换为相应组件实例中 msg 属性的值。同时每次 msg 属性更改时它也会同步更新。

 

  <template id="my-app">

    <!-- 1.mustache的基本使用 -->

    <h2>{{message}} - {{message}}</h2>

    

    <!-- 2.是一个表达式 -->

    <h2>{{counter * 10}}</h2>

    <h2>{{ message.split(" ").reverse().join(" ") }}</h2>

    

    <!-- 3.也可以调用函数 -->

    <!-- 可以使用computed(计算属性) -->

    <h2>{{getReverseMessage()}}</h2>

    

    <!-- 4.三元运算符 -->

    <h2>{{ isShow ? "哈哈哈": "" }}</h2>

    <button @click="toggle">切换</button>

 

    <!-- 错误用法 -->

    <!-- var name = "abc" -> 赋值语句 -->

    <!-- <h2>{{var name = "abc"}}</h2>

    <h2>{{ if(isShow) { return "哈哈哈" } }}</h2> -->

  </template>

1

2

3

4

1. v-on

说明: 给元素绑定事件监听器。

 

缩写:@

 

参数: event (使用对象语法则为可选项)

 

修饰符:

 

.stop ——调用 event.stopPropagation()。

.prevent ——调用 event.preventDefault()。

.capture ——在捕获模式添加事件监听器。

.self ——只有事件从元素本身发出才触发处理函数。

.{keyAlias} ——只在某些按键下触发处理函数。

.once ——最多触发一次处理函数。

.left ——只在鼠标左键事件触发处理函数。

.right ——只在鼠标右键事件触发处理函数。

.middle ——只在鼠标中键事件触发处理函数。

.passive ——通过 { passive: true } 附加一个 DOM 事件。

详细描述:

事件类型由参数来指定。表达式可以是一个方法名,一个内联声明,如果有修饰符则可省略。

 

当用于普通元素,只监听原生 DOM 事件。当用于自定义元素组件,则监听子组件触发的自定义事件。

当监听原生 DOM 事件时,方法接收原生事件作为唯一参数。如果使用内联声明,声明可以访问一个特殊的 $event 变量:v-on:click="handle('ok', $event)"。

v-on 还支持绑定不带参数的事件/监听器对的对象。请注意,当使用对象语法时,不支持任何修饰符。

<!-- 方法处理函数 -->

<button v-on:click="doThis"></button>

 

<!-- 动态事件 -->

<button v-on:[event]="doThis"></button>

 

<!-- 内联声明 -->

<button v-on:click="doThat('hello', $event)"></button>

 

<!-- 缩写 -->

<button @click="doThis"></button>

 

<!-- 使用缩写的动态事件 -->

<button @[event]="doThis"></button>

 

<!-- 停止传播 -->

<button @click.stop="doThis"></button>

 

<!-- 阻止默认事件 -->

<button @click.prevent="doThis"></button>

 

<!-- 不带表达式地阻止默认事件 -->

<form @submit.prevent></form>

 

<!-- 链式调用修饰符 -->

<button @click.stop.prevent="doThis"></button>

 

<!-- 按键用于 keyAlias 修饰符-->

<input @keyup.enter="onEnter" />

 

<!-- 点击事件将最多触发一次 -->

<button v-on:click.once="doThis"></button>

 

<!-- 对象语法 -->

<button v-on="{ mousedown: doThis, mouseup: doThat }"></button>

 

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

2. v-bind

说明: 动态的绑定一个或多个 attribute,也可以是组件的 prop。

 

缩写: : 或者 . (当使用 .prop 修饰符)

 

修饰符:

 

.camel ——将短横线命名的 attribute 转变为驼峰式命名。

.prop ——强制绑定为 DOM property。3.2+

.attr ——强制绑定为 DOM attribute。3.2+

用途:

 

当用于绑定 class 或 style attribute,v-bind 支持额外的值类型如数组或对象。详见下方的指南链接。

在处理绑定时,Vue 默认会利用 in 操作符来检查该元素上是否定义了和绑定的 key 同名的 DOM property。如果存在同名的 property,则 Vue 会把作为 DOM property 赋值,而不是作为 attribute 设置。这个行为在大多数情况都符合期望的绑定值类型,但是你也可以显式用 .prop 和 .attr 修饰符来强制绑定方式。有时这是必要的,特别是在和自定义元素打交道时。

当用于组件 props 绑定时,所绑定的 props 必须在子组件中已被正确声明。

当不带参数使用时,可以用于绑定一个包含了多个 attribute 名称-绑定值对的对象。

<!-- 绑定 attribute -->

<img v-bind:src="imageSrc" />

 

<!-- 动态 attribute 名 -->

<button v-bind:[key]="value"></button>

 

<!-- 缩写 -->

<img :src="imageSrc" />

 

<!-- 缩写形式的动态 attribute 名 -->

<button :[key]="value"></button>

 

<!-- 内联字符串拼接 -->

<img :src="'/path/to/images/' + fileName" />

 

<!-- class 绑定 -->

<div :class="{ red: isRed }"></div>

<div :class="[classA, classB]"></div>

<div :class="[classA, { classB: isB, classC: isC }]"></div>

 

<!-- style 绑定 -->

<div :style="{ fontSize: size + 'px' }"></div>

<div :style="[styleObjectA, styleObjectB]"></div>

 

<!-- 绑定对象形式的 attribute -->

<div v-bind="{ id: someProp, 'other-attr': otherProp }"></div>

 

<!-- prop 绑定。“prop” 必须在子组件中已声明。 -->

<MyComponent :prop="someThing" />

 

<!-- 传递子父组件共有的 prop -->

<MyComponent v-bind="$props" />

 

<!-- XLink -->

<svg><a :xlink:special="foo"></a></svg>

 

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

3. v-if

说明: 基于表达式值的真假性,来条件性地渲染元素或者模板片段。

 

<h2 v-if="isShow">哈哈哈哈</h2>

1

4. v-else

说明: 表示 v-if 或 v-if / v-else-if 链式调用的“else 块”。

 

<h2 v-if="isShow">Coder</h2>

<h2 v-else>Bin</h2>

1

2

ishow 为 true 显示 Coder,反之显示 Bin

 

5. v-else-if

说明: 表示 v-if 的“else if 块”。可以进行链式调用。

 

<template id="my-app">

  <input type="text" v-model="score">

  <h2 v-if="score > 90">优秀</h2>

  <h2 v-else-if="score > 60">良好</h2>

  <h2 v-else>不及格</h2>

</template>

1

2

3

4

5

6

v-model 后面会说明

 

6. v-show

说明:基于表达式值的真假性,来改变元素的可见性。

 

详细描述:v-show 通过设置内联样式的 display CSS 属性来工作,当元素可见时将使用初始 display 值。当条件改变时,也会触发过渡效果。

 

  <template id="my-app">

    <h2 v-show="isShow">哈哈哈哈</h2>

  </template>

 

  <script>

    const App = {

      template: '#my-app',

      data() {

        return {

          isShow: true

        }

      }

    }

    Vue.createApp(App).mount('#app');

  </script>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

v-show 不支持在 <template> 元素上使用,也不能和 v-else 搭配使用。

 

7. v-model

说明: 在表单输入元素或组件上创建双向绑定。

 

仅限: <input>、<select>、 <textarea>、components

 

修饰符:

 

.lazy ——监听 change 事件而不是 input

.number ——将输入的合法符串转为数字

.trim ——移除输入内容两端空格

基本使用:

 

  <template id="my-app">

    <!-- 1.v-bind value的绑定 2.监听input事件, 更新message的值 -->

    <!-- <input type="text" :value="message" @input="inputChange"> -->

 

    <input type="text" v-model="message">

    <h2>{{message}}</h2>

  </template>

 

  <script>

    const App = {

      template: '#my-app',

      data() {

        return {

          message: "Hello World"

        }

      },

      methods: {

        inputChange(event) {

          this.message = event.target.value;

        }

      }

    }

    Vue.createApp(App).mount('#app');

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

绑定其他表单:

 

  <template id="my-app">

    <!-- 1.绑定textarea -->

    <label for="intro">

      自我介绍

      <textarea name="intro" id="intro" cols="30" rows="10" v-model="intro"></textarea>

    </label>

    <h2>intro: {{intro}}</h2>

 

    <!-- 2.checkbox -->

    <!-- 2.1.单选框 -->

    <label for="agree">

      <input id="agree" type="checkbox" v-model="isAgree"> 同意协议

    </label>

    <h2>isAgree: {{isAgree}}</h2>

 

    <!-- 2.2.多选框 -->

    <span>你的爱好: </span>

    <label for="basketball">

      <input id="basketball" type="checkbox" v-model="hobbies" value="basketball"> 篮球

    </label>

    <label for="football">

      <input id="football" type="checkbox" v-model="hobbies" value="football"> 足球

    </label>

    <label for="tennis">

      <input id="tennis" type="checkbox" v-model="hobbies" value="tennis"> 网球

    </label>

    <h2>hobbies: {{hobbies}}</h2>

 

    <!-- 3.radio -->

    <span>你的爱好: </span>

    <label for="male">

      <input id="male" type="radio" v-model="gender" value="male">男

    </label>

    <label for="female">

      <input id="female" type="radio" v-model="gender" value="female">女

    </label>

    <h2>gender: {{gender}}</h2>

 

    <!-- 4.select -->

    <span>喜欢的水果: </span>

    <select v-model="fruit" multiple size="2">

      <option value="apple">苹果</option>

      <option value="orange">橘子</option>

      <option value="banana">香蕉</option>

    </select>

    <h2>fruit: {{fruit}}</h2>

  </template>

 

  <script>

    const App = {

      template: '#my-app',

      data() {

        return {

          intro: "Hello World",

          isAgree: false,

          hobbies: ["basketball"],

          gender: "",

          fruit: "orange"

        }

      },

      methods: {

        commitForm() {

          axios

        }

      }

    }

 

    Vue.createApp(App).mount('#app');

  </script>

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

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

v-model修饰符的使用

 

 <template id="my-app">

    <!-- 1.lazy修饰符 -->

    <!-- <input type="text" v-model.lazy="message"> -->

 

    <!-- 2.number修饰符 -->

    <!-- <input type="text" v-model.number="message">

    <h2>{{message}}</h2>

    <button @click="showType">查看类型</button> -->

 

    <!-- 3.trim修饰符 -->

    <input type="text" v-model.trim="message">

    <button @click="showResult">查看结果</button>

  </template>

 

  <script>

    const App = {

      template: '#my-app',

      data() {

        return {

          message: "Hello World"

        }

      },

      methods: {

        showType() {

          console.log(this.message, typeof this.message);

        },

        showResult() {

          console.log(this.message);

        }

      }

    }

 

    Vue.createApp(App).mount('#app');

  </script>

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

8. v-for

说明: 基于原始数据多次渲染元素或模板块。

 

详细描述:

 

指令值必须使用特殊语法 alias in expression 为正在迭代的元素提供一个别名:

 

<div v-for="item in items">

  {{ item.text }}

</div>

1

2

3

或者,你也可以为索引指定别名 (如果用在对象,则是键值):

 

<div v-for="(item, index) in items"></div>

<div v-for="(value, key) in object"></div>

<div v-for="(value, name, index) in object"></div>

1

2

3

v-for 的默认方式是尝试就地更新元素而不移动它们。要强制其重新排序元素,你需要用特殊 attribute key 来提供一个排序提示:

 

<div v-for="item in items" :key="item.id">

  {{ item.text }}

</div>

1

2

3

9. v-slot

说明: 用于声明具名插槽或是期望接收 props 的作用域插槽。

 

缩写: #

 

参数:插槽名 (可选,默认是 default)

 

仅限:

 

<template>

components (用于带有 prop 的单个默认插槽)

示例

 

<!-- 具名插槽 -->

<BaseLayout>

  <template v-slot:header>

    Header content

  </template>

 

  <template v-slot:default>

    Default slot content

  </template>

 

  <template v-slot:footer>

    Footer content

  </template>

</BaseLayout>

 

<!-- 接收 prop 的具名插槽 -->

<InfiniteScroll>

  <template v-slot:item="slotProps">

    <div class="item">

      {{ slotProps.item.text }}

    </div>

  </template>

</InfiniteScroll>

 

<!-- 接收 prop 的默认插槽,并解构 -->

<Mouse v-slot="{ x, y }">

  Mouse position: {{ x }}, {{ y }}

</Mouse>

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

10. v-text

说明: 更新元素的文本内容。

 

详细描述: v-text 通过设置元素的 textContent 属性来工作,因此它将覆盖元素中所有现有的内容。如果你需要更新 textContent 的部分,应该使用 mustache interpolations 代替。

 

<span v-text="msg"></span>

<!-- 等同于 -->

<span>{{msg}}</span>

1

2

3

11. v-html

说明: 更新元素的 innerHTML。

 

详细描述: v-html 的内容直接作为普通 HTML 插入—— Vue 模板语法是不会被解析的。如果你发现自己正打算用 v-html 来编写模板,不如重新想想怎么使用组件来代替。

 

安全说明:

在你的站点上动态渲染任意的 HTML 是非常危险的,因为它很容易导致 XSS 攻击。请只对可信内容使用 HTML 插值,绝不要将用户提供的内容作为插值

 

<div v-html="html"></div>

1

12. v-pre

说明: 跳过该元素及其所有子元素的编译。

 

详细描述:

元素内具有 v-pre,所有 Vue 模板语法都会被保留并按原样渲染。最常见的用例就是显示原始双大括号标签及内容。

 

<span v-pre>{{ this will not be compiled }}</span>

1

13. v-once

说明: 仅渲染元素和组件一次,并跳过之后的更新。

 

详细描述: 在随后的重新渲染,元素/组件及其所有子项将被当作静态内容并跳过渲染。这可以用来优化更新时的性能。

 

<!-- 单个元素 -->

<span v-once>This will never change: {{msg}}</span>

<!-- 带有子元素的元素 -->

<div v-once>

  <h1>comment</h1>

  <p>{{msg}}</p>

</div>

<!-- 组件 -->

<MyComponent v-once :comment="msg" />

<!-- `v-for` 指令 -->

<ul>

  <li v-for="i in list" v-once>{{i}}</li>

</ul>

1

2

3

4

5

6

7

8

9

10

11

12

13

14. v-cloak

说明: 用于隐藏尚未完成编译的 DOM 模板。

 

详细描述:

该指令只在没有构建步骤的环境下需要使用。

 

当使用直接在 DOM 中书写的模板时,可能会出现一种叫做“未编译模板闪现”的情况:用户可能先看到的是还没编译完成的双大括号标签,直到挂载的组件将它们替换为实际渲染的内容。

v-cloak 会保留在所绑定的元素上,直到相关组件实例被挂载后才移除。配合像 [v-cloak] { display: none } 这样的 CSS 规则,它可以在组件编译完毕前隐藏原始模板。

[v-cloak] {

  display: none;

}

1

2

3

<div v-cloak>

  {{ message }}

</div>

1

2

3

每文一句:惜时专心苦读是做学问的一个好方法。

 

本次的分享就到这里,如果本章内容对你有所帮助的话可以点赞+收藏。文章有不对的地方欢迎指出,有任何疑问都可以在评论区留言。希望大家都能够有所收获,大家一起探讨、进步!

 

vue.jsjavascript前端

来自专栏

Vue.js

 CoderBin_ 1篇文章 0人订阅

 

 

人工智能现状报告最新重要发现!!

 

发布于2022-10-04

著作权归作者所有

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值