开心档之Vue教程1
编辑
Vue.js class
class 与 style 是 HTML 元素的属性,用于设置元素的样式,我们可以用 v-bind 来设置样式属性。
Vue.js v-bind 在处理 class 和 style 时, 专门增强了它。表达式的结果类型除了字符串之外,还可以是对象或数组。
class 属性绑定
我们可以为 v-bind:class 设置一个对象,从而动态的切换 class:
实例 1
实例中将 isActive 设置为 true 显示了一个绿色的 div 块,如果设置为 false 则不显示:
<div v-bind:class="{ 'active': isActive }"></div>
以上实例 div class 为:
<div class="active"></div>
我们也可以在对象中传入更多属性用来动态切换多个 class 。
实例 2
text-danger 类背景颜色覆盖了 active 类的背景色:
<div class="static"
v-bind:class="{ 'active' : isActive, 'text-danger' : hasError }">
</div>
以上实例 div class 为:
<div class="static active text-danger"></div>
我们也可以直接绑定数据里的一个对象:
实例 3
text-danger 类背景颜色覆盖了 active 类的背景色:
<div id="app">
<div v-bind:class="classObject"></div>
</div>
实例 3 与 实例 2 的渲染结果是一样的。
此外,我们也可以在这里绑定返回对象的计算属性。这是一个常用且强大的模式:
实例 4
new Vue({
el: '#app',
data: {
isActive: true,
error: {
value: true,
type: 'fatal'
}
},
computed: {
classObject: function () {
return {
base: true,
active: this.isActive && !this.error.value,
'text-danger': this.error.value && this.error.type === 'fatal',
}
}
}
})
数组语法
我们可以把一个数组传给 v-bind:class ,实例如下:
实例 5
<div v-bind:class="[activeClass, errorClass]"></div>
以上实例 div class 为:
<div class="active text-danger"></div>
我们还可以使用三元表达式来切换列表中的 class :
实例 6
errorClass 是始终存在的,isActive 为 true 时添加 activeClass 类:
<div v-bind:class="[errorClass ,isActive ? activeClass : '']"></div>
我们可以在 v-bind:style 直接设置样式:
实例 7
<div id="app">
<div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }">菜鸟教程</div>
</div>
<div style="color: green; font-size: 30px;">菜鸟教程</div>
实例 8
<div id="app">
<div v-bind:style="styleObject">菜鸟教程</div>
</div>
实例 9
<div id="app">
<div v-bind:style="[baseStyles, overridingStyles]">菜鸟教程</div>
</div>
注意:当 v-bind:style 使用需要特定前缀的 CSS 属性时,如 transform ,Vue.js 会自动侦测并添加相应的前缀。 Vue.js 组件 - 自定义事件 父组件是使用 props 传递数据给子组件,但如果子组件要把数据传递回去,就需要使用自定义事件! 我们可以使用 v-on 绑定自定义事件, 每个 Vue 实例都实现了事件接口(Events interface),即:
- 使用
$on(eventName)
监听事件 - 使用
$emit(eventName)
触发事件
另外,父组件可以在使用子组件的地方直接用 v-on 来监听子组件触发的事件。 以下实例中子组件已经和它外部完全解耦了。它所做的只是触发一个父组件关心的内部事件。 实例 <div id="app"> <div id="counter-event-example"> <p>{{ total }}</p> <button-counter v-on:increment="incrementTotal"></button-counter> <button-counter v-on:increment="incrementTotal"></button-counter> </div> </div> <script> Vue.component('button-counter', { template: '<button v-on:click="incrementHandler">{{ counter }}</button>', data: function () { return { counter: 0 } }, methods: { incrementHandler: function () { this.counter += 1 this.$emit('increment') } }, }) new Vue({ el: '#counter-event-example', data: { total: 0 }, methods: { incrementTotal: function () { this.total += 1 } } }) </script>
Prop prop 是子组件用来接受父组件传递过来的数据的一个自定义属性。 父组件的数据需要通过 props 把数据传给子组件,子组件需要显式地用 props 选项声明 "prop": Prop 实例 <div id="app"> <child message="hello!"></child> </div> <script> // 注册 Vue.component('child', { // 声明 props props: ['message'], // 同样也可以在 vm 实例中像 "this.message" 这样使用 template: '<span>{{ message }}</span>' }) // 创建根实例 new Vue({ el: '#app' }) </script>
注意: prop 是单向绑定的:当父组件的属性变化时,将传导给子组件,但是不会反过来。 Prop 验证 组件可以为 props 指定验证要求。 为了定制 prop 的验证方式,你可以为 props 中的值提供一个带有验证需求的对象,而不是一个字符串数组。例如: Vue.component('my-component', { props: { // 基础的类型检查 (`null` 和 `undefined` 会通过任何类型验证) propA: Number, // 多个可能的类型 propB: [String, Number], // 必填的字符串 propC: { type: String, required: true }, // 带有默认值的数字 propD: { type: Number, default: 100 }, // 带有默认值的对象 propE: { type: Object, // 对象或数组默认值必须从一个工厂函数获取 default: function () { return { message: 'hello' } } }, // 自定义验证函数 propF: { validator: function (value) { // 这个值必须匹配下列字符串中的一个 return ['success', 'warning', 'danger'].indexOf(value) !== -1 } } } })
-
String
-
Number
-
Boolean
-
Array
-
Object
-
Date
-
Function
-
Symbol
type 也可以是一个自定义构造器,使用 instanceof 检测。
相关文章
- vue面试必须掌握的点
- VUE 状态管理模式
- Vue之事件监听
- Framework7 Vue 教程 入门 学习
- vue elementui navmenu 多级导航菜单(水平、垂直)
- Vue笔记(11) vue-router
- Vue响应式依赖收集原理分析-vue高级必备
- Vue富文本编辑器_前端富文本编辑器插件
- vue父组件向子组件传值_vue什么是父子组件
- Vue调试工具安装(vue devtools)
- Vue项目:菜农管理 【maven + spring boot + RESTFul + SSM+vue + axios】
- 开心档之Vue教程2
- 开心档之Vue教程1
- IntelliJ IDEA使用 vue-cli 创建Vue项目
- 前端二面vue面试题(边面边更)1
- 「 [学习日记] vue拦截原理 」
- 【Vue】1564- 8 个很棒的 Vue 开发技巧
- 开心档之Vue教程2
- 首发ChatGPT3.5小程序开源vue!
- linux下快速部署Vue项目(linux部署vue)
- 从零开始Vue项目中使用Redis(vue使用redis)