当前位置: 首页 > news >正文

烟台专业做网站公司珠海网站免费制作

烟台专业做网站公司,珠海网站免费制作,自建网站三种模式,阿里巴巴做网站多少钱文章目录前言1. v-text / {{ expression }}2.v-html3.v-bind4.v-on5. v-model6.v-for7.v-if / v-else-if / v-else9.v-show10.v-cloak11.v-pre12.组件注册指令13.动态组件指令14.自定义指令15.过滤器指令前言 Vue.js 是一款流行的前端框架,它通过指令(Di…

文章目录

  • 前言
  • 1. v-text / {{ expression }}
  • 2.v-html
  • 3.v-bind
  • 4.v-on
  • 5. v-model
  • 6.v-for
  • 7.v-if / v-else-if / v-else
  • 9.v-show
  • 10.v-cloak
  • 11.v-pre
  • 12.组件注册指令
  • 13.动态组件指令
  • 14.自定义指令
  • 15.过滤器指令

在这里插入图片描述

前言

在这里插入图片描述
Vue.js 是一款流行的前端框架,它通过指令(Directive)实现了对 DOM 元素的控制,使得开发者能够更加方便地管理页面的展示和交互。下面是 Vue.js 常用指令及其使用场景:

1. v-text / {{ expression }}

v-text 指令可以用来将元素的文本内容设置为指定的值,{{ expression }} 语法也可以实现同样的效果。

使用方式如下:

<template><div><span v-text="message"></span><span>{{ message }}</span></div>
</template><script>
export default {data() {return {message: 'Hello, Vue!',}},
}
</script>

在上面的代码中,使用 v-text 指令和 {{ expression }} 语法将 message 数据对象中的值显示在元素中。

2.v-html

v-html 指令可以用来将元素的 HTML 内容设置为指定的值。

使用方式如下:

<template><div v-html="htmlContent"></div>
</template><script>
export default {data() {return {htmlContent: '<h1>Hello, Vue!</h1>',}},
}
</script>

在上面的代码中,使用 v-html 指令将 htmlContent 数据对象中的值作为 HTML 内容渲染在元素中。

3.v-bind

v-bind 指令可以用来动态地绑定 HTML 特性,例如元素的 class、style、href 等。通过将值绑定到 Vue.js 组件实例中的数据,可以轻松地动态更新元素。

使用方式如下:

<template><div v-bind:class="{ active: isActive }">{{ message }}</div>
</template><script>
export default {data() {return {isActive: true,message: 'Hello, Vue!',}},
}
</script>

在上面的代码中,使用 v-bind:class 指令将组件实例中的 isActive 数据动态地绑定到 div 元素的 class 中,根据 isActive 的值动态地添加或删除 active 类。

除了简写的 v-bind:class,也可以写成 v-bind:style、v-bind:href 等形式,根据需要动态地绑定元素的不同特性。

同时,为了简化模板语法,Vue.js 还提供了缩写的语法形式,在指令名前加上冒号即可,例如 :class=“{ active: isActive }”,与 v-bind:class=“{ active: isActive }” 效果相同。

4.v-on

v-on 指令可以用来绑定元素的事件或组件的自定义事件。

使用方式如下:

<template><div><button v-on:click="onClick">点击</button><my-component v-on:custom-event="onCustomEvent"></my-component></div>
</template><script>
import MyComponent from './MyComponent.vue';export default {components: {'my-component': MyComponent,},methods: {onClick() {console.log('Button clicked');},onCustomEvent(payload) {console.log('Custom event triggered with payload:', payload);},},
}
</script>

在上面的代码中,使用 v-on 指令绑定了 button 元素的 click 事件和 my-component 组件的 custom-event 自定义事件,并通过 methods 属性定义了对应的事件处理函数。

5. v-model

v-model 指令可以用来在表单元素(如 input、select、textarea 等)和 Vue.js 组件实例中的数据之间建立双向绑定。这意味着当用户在表单元素中输入数据时,Vue.js 组件实例中的数据会自动更新;反之,当 Vue.js 组件实例中的数据更新时,表单元素中的数据也会自动更新。

使用方式如下:

<template><input v-model="message" /><div>{{ message }}</div>
</template><script>
export default {data() {return {message: 'Hello, Vue!',}},
}
</script>

在上面的代码中,使用 v-model 指令将 input 元素的值与组件实例中的 message 数据建立双向绑定。当用户在 input 元素中输入数据时,组件实例中的 message 数据会自动更新;反之,当组件实例中的 message 数据更新时,input 元素中的值也会自动更新。同时,div 元素中的文本内容也会随着 message 数据的更新而动态更新。

除了上面的例子中使用的 input 元素,v-model 指令还可以用在 select、textarea 等表单元素中,根据需要建立双向绑定。

6.v-for

v-for 指令可以用来根据数据对象中的属性循环渲染元素。

使用方式如下:

<template><ul><li v-for="item in items" :key="item.id">{{ item.text }}</li></ul>
</template><script>
export default {data() {return {items: [{ id: 1, text: 'Item 1' },{ id: 2, text: 'Item 2' },{ id: 3, text: 'Item 3' },],}},
}
</script>

在上面的代码中,使用 v-for 指令根据 items 数据对象中的属性循环渲染 li 元素。

7.v-if / v-else-if / v-else

v-if / v-else-if / v-else 指令可以用来根据条件判断动态地显示或隐藏元素。

使用方式如下:

<template><div><div v-if="isShown">This is shown</div><div v-else-if="isHidden">This is hidden</div><div v-else>This is default</div></div>
</template><script>
export default {data() {return {isShown: true,isHidden: false,}},
}
</script>

在上面的代码中,使用 v-if / v-else-if / v-else 指令根据条件动态地显示或隐藏了三个 div 元素。

9.v-show

v-show 指令可以用来根据条件判断动态地显示或隐藏元素,与 v-if 不同的是,v-show 是通过设置元素的 display 样式来实现的。

使用方式如下:

<template><div><div v-show="isShown">This is shown</div><div v-show="isHidden">This is hidden</div></div>
</template><script>
export default {data() {return {isShown: true,isHidden: false,}},
}
</script>

在上面的代码中,使用 v-show 指令根据条件动态地显示或隐藏了两个 div 元素。

10.v-cloak

v-cloak 指令可以用来在 Vue.js 加载时防止元素显示未编译的 Mustache 标签。

使用方式如下:

<template><div v-cloak>{{ message }}</div>
</template><style>
[v-cloak] {display: none;
}
</style><script>
export default {data() {return {message: 'Hello, Vue!',}},
}
</script>

在上面的代码中,使用 v-cloak 指令在 div 元素显示之前防止 Mustache 标签的未编译显示,并设置了 [v-cloak] 样式以使元素在 Vue.js 加载完成之前隐藏。

11.v-pre

v-pre 指令可以用来防止 Vue.js 将指令中的表达式进行编译,保留原始的文本内容。

使用方式如下:

<template><div v-pre>{{ message }}</div>
</template><script>
export default {data() {return {message: 'Hello, Vue!',}},
}
</script>

在上面的代码中,使用 v-pre 指令保留了 div 元素中的原始文本内容,而不进行编译。

12.组件注册指令

Vue.component
Vue.component 方法可以用来注册全局组件。

使用方式如下:

<template><div><my-component></my-component></div>
</template><script>
import MyComponent from './MyComponent.vue';export default {components: {'my-component': MyComponent,},
}
</script>

在上面的代码中,使用 Vue.component 方法注册了一个名为 my-component 的全局组件,并在模板中使用了该组件。

13.动态组件指令

keep-alive / component
keep-alive 和 component 指令可以用来动态地渲染组件,通过设置不同的组件名或组件实例来实现组件的动态切换。

使用方式如下:

<template><div><component v-bind:is="currentComponent"></component><button v-on:click="switchComponent">切换组件</button></div>
</template><script>
import ComponentA from './ComponentA.vue';
import ComponentB from './ComponentB.vue';export default {data() {return {currentComponent: 'component-a',}},components: {'component-a': ComponentA,'component-b': ComponentB,},methods: {switchComponent() {this.currentComponent = this.currentComponent === 'component-a' ? 'component-b' : 'component-a';},},
}
</script>

在上面的代码中,通过设置 v-bind:is 属性来动态渲染组件,通过 v-on:click 事件来切换组件。

14.自定义指令

Vue.directive
Vue.directive 方法可以用来注册自定义指令。

使用方式如下:

<template><div v-my-directive>自定义指令</div>
</template><script>
export default {directives: {'my-directive': {inserted: function (el) {el.style.color = 'red';}}},
}
</script>

在上面的代码中,通过 Vue.directive 方法注册了一个名为 my-directive 的自定义指令,并在模板中使用了该指令。

15.过滤器指令

Vue.filter
Vue.filter 方法可以用来注册全局过滤器。

使用方式如下:

<template><div>{{ message | reverse }}</div>
</template><script>
export default {data() {return {message: 'Hello, Vue!',}},filters: {reverse: function (value) {return value.split('').reverse().join('');}},
}
</script>

在上面的代码中,通过 Vue.filter 方法注册了一个名为 reverse 的全局过滤器,并在模板中使用了该过滤器。

以上就是 Vue.js 中常用的指令及其使用场景。希望能够帮助你更好的理解vue指令。欢迎转发或在评论区交流讨论。

http://www.yayakq.cn/news/755373/

相关文章:

  • 做网站的颜色增城新塘镇 企业网站建设
  • 做什么网站比较简单郑州建设信息网简介
  • 网站里面如何做下载的app创新logo设计
  • 织梦采集侠官方网站wordpress中国风
  • 青岛开发区网站建设多少钱wordpress站外搜索
  • 那个做网站好做学校网站的内容
  • 做一个什么网站好网站子目录绑定二级域名
  • 中关村手机网抚州seo排名
  • 网站运营与维护建企聘企业管理有限公司
  • 售后服务 网站建设甘肃省城乡城乡建设厅网站
  • 海宁市住房与建设规划局网站wordpress长文章分页
  • 网站制作费可以做业务宣传费企业做营销型网站
  • 济南网站设计哪家好永康物流网站开发平台
  • 杭州装饰网站建设做网站 售后服务里都写啥
  • 中国网站的建设企业网站制作的方法
  • 阿里云php做网站网站名称注意事项
  • 哪些网站免费做职业测评extract wordpress
  • 免费建站绑定域名wordpress 30天
  • 又做投资的网站吗做的最好的视频教学网站
  • 高效网站推广费用滨州做网站建设价格
  • 代刷网址推广seo优化方案模板
  • 苏州华亭建设工程有限公司网站企业制作宣传片拍摄
  • 上海嘉定建设局网站做行业分析的网站
  • 建设部执业资格注册中心网站装修平台排行榜
  • 济南美赞网站建设公司建网站的策划方案
  • 赤峰网站设计网络营销外包网络推广
  • 企业为什么选择网站建设淄博网站排名seo
  • 阜阳做网站有吗成都极客联盟
  • 网站建设公司怎么寻找客户呢iis7.5配置网站
  • 现在网站开发模式网络营销推广方案书