avatar

vue入门

1. vue实例

1.1 vue实例的创建和作用

1
2
3
4
5
6
7
8
9
10
11
12
13
const app = new Vue({
el: '#root',
template: '<h1>hello world {{text}}</h1>',
data: {
text: 0
}
})

setInterval(() => {
// app.text += 1; // 有效果, 页面text的值在增加
// app.$options.data.text += 1; // 无效果, 页面的text不增加
app.$data.text += 1; // 有效果, 页面text的值在增加
})

1.2 vue实例的属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
console.log(app.$data); // 传入new Vue 对象中的data属性
console.log(app.$props); // 传入new Vue 对象中的props属性
console.log(app.$el); // app对象 挂载的dom节点
console.log(app.$options); // 传入new Vue 对象中的整个对象, 注意直接通过修改app.$options.data对象的值是没有效果的, 因为Vue 对传入的对象进行了处理, 没有直接使用传入的app.$options.data对象
console.log(app.$root === app); // true
console.log(app.$children); // 就是dom节点的子节点
console.log(app.$slots); // 插槽
console.log(app.$scopedSlots); // 插槽
console.log(app.$refs); // 模板或者组件的引用, 可以快速定位到想取到的dom节点或者组件
console.log(app.$isServer); // 判断是否是服务端渲染

// $optons上有一个比较特殊的属性: render方法, 是会生效的, 待第二次调用render方法的时候, 才会生效
app.$options.render = h => {
return h('div', {}, 'new render function')
}

1.3 vue实例的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 监听器:
const textWatch = app.$watch('text', (newValue, oldValue) => {
console.log(`${newValue} : ${oldValue}`);
});

// 注意在组件里的options进行声明的wtach则不需要手动进行注销, 组件上的所有wathc会跟随组件的注销自动销毁
// 注意组件销毁的时候要将不再使用的watch给注销掉, 直接调用 xxx.$watch() 的返回值就行
textWatch();


// 事件:
app.$emit('text', '1', '2');

app.$on('text', (a, b) => {
console.log(`text emit ${a}, ${b}`);
})

// $once只会触发一次, 其他的跟$on一样
app.$once('text', (a, b) => {
console.log(`text emit ${a}, ${b}`);
})

// $forceUpdate(), 使组件强制重新渲染,
app.$forceUpdate();

2. vue的生命周期

  • beforeCreate
  • created
  • beforeMount
  • mounted
  • beforeUpdate
  • updated
  • beforeDestroy
  • destroyed

3. vue的属性

watch: 一般情况下, watch在初始化的时候是不会执行一次的, 如果想要在组件初始化的时候就执行, 需要在computed属性对应的监听对象下面写immediate: true, 深度监听要用deep: true, watch监听的对象还可以用对象的点语法来写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
computed: {
firstName: {
handler(newValue, oldValue) {
this.fullName = newValue + this.lastName;
},
immediate: true, // 组件初始化的时候立即执行一次, 默认为false;
deep: true, // 深度监听, 默认为false
},
lastName: {
handler(newValue, oldValue) {
this.fullName = this.firstName + newValue;
},
immediate: true, // 组件初始化的时候立即执行一次, 默认为false;
deep: true, // 深度监听, 默认为false
},
'obj.a': {
// ...
}
}

注意一般情况下不要在computed和watch里面去修改原来的值, 有可能造成无限循环

4. vue的指令

4.1 原生指令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
v-bind    简写  :
v-on 简写 @
v-text
v-html
v-show
v-hide
v-if
v-else-if
v-else
v-for
v-model
v-pre
v-cloak(基本上用不到)
v-once

5. vue插槽

5.1 普通插槽

作用: vue组件不用于一般的html标签, 在组件内部再写html标签不会渲染, 但是一些组件的功能仅仅是给另一些组件包一层样式, 在里面还需要另外的组件:

1
2
3
4
5
<template>
<div class="feature">
<slot></slot>
</div>
</template>

使用: 直接在组件里面包着要放入的内容, 此处带有插槽的组件为Feature

1
2
3
<Feature>
<h1>header</h1>
</Feature>

5.2 具名插槽

作用: 组件内部需要多处不同的放置东西的情况

1
2
3
4
5
6
7
8
9
10
<template>
<div class="feature">
<div class="header">
<slot name="header"></slot>
</div>
<div class="content">
<slot name="content"></slot>
</div>
</div>
</template>

使用:

1
2
3
4
<Feature>
<h1 slot="header">header</h1>
<h1 slot="content">content</h1>
</Feature>

5.3 作用域插槽

作用: 调用插槽的组件处可以使用定义插槽组件内部的一些变量

1
2
3
4
5
6
7
<template>
<div class="feature">
<div class="header">
<slot :value="test" aaa="111"></slot>
</div>
</div>
</template>

使用:

1
2
3
<Feature>
<h1 slot-scope="props">{{props.value}} {{props.aaa}} {{test}}</h1>
</Feature>

6. vue修饰符

在上例使用的 event.pr巳ventDefault()也可以用 Vue 事件的修饰符来实现,在@绑定的事件后加 小圆点”.”,再跟一个后缀来使用修饰符。 Vue支持以下修饰符:

  • .stop
  • .prevent
  • .capture
  • .self
  • .once
  • .lazy
  • .trim
  • .number

具体用法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!-- 阻止单击事件冒泡  -->
<a @click.stop="handle"></a>

<!-- 提交事件不再重载页面 -->
<form @submit.prevent="handle"></form>

<!-- 修饰符可以串联 -->
<a @click.stop.prevent="handle"></a>

<!-- 只有修饰符 -->
<form @submit.prevent></form>

<!-- 添加事件侦听器时使用事件捕获模式 -->
<div @click.capture="handle">...</div>

<!-- 只当事件在该元素本身(而不是子元素) 触发时触发回调 -->
<div @click.self="handle">...</div>

<!-- 只触发一次,组件同样适用 -->
<div @click.once="handle">...</div>

在表单元素上监昕键盘事件时,还可以使用按键修饰符,比如按下具体某个键时才调用方法: 只有在 keyCode 是 13 时调用 vm.submit()

1
<input @keyup.13="submit">

也可以自己配置具体按键: Vue.config.keyCodes.f1 = 112; //全局定义后,就可以使用自 keyup.f1 例如:

1
2
3
4
<!-- Shift + S -->
<input @keyup.shift.83="handleSave">
<!-- Ctrl + Click -->
<div @click.ctrl="doSomething">Do something</div>

除了具体的某个 keyCode 外, Vue 还提供了 一些快捷名称,以下是全部的别名:

  • .enter
  • .tab
  • .delete (捕获”删除”和”退格”键)
  • .esc
  • .space
  • .up
  • .down
  • .left
  • .right 这些按键修饰符也可以组合使用,或和鼠标一起配合使用:
  • .ctrl
  • .alt
  • .shift
  • .meta (Mac 下是 Command 键, Windows 下是窗口键)

7. 自定义组件的v-model

实现一个具有双向绑定的v-model组件要满足下面两个需求

  • 接收一个 value 属性

  • 在有新的 value 时触发 input 事件

1
2
3
4
5
6
7
8
9
<div id="app"> 
<!-- <price-input v-model="price"></price-input> -->

<!-- 手动实现了v-model双向绑定 -->
<!-- 3、父组件的input事件被触发,将传来的值赋给父组件的变量price -->
<!-- 4、父组件value的值绑定到price -->
<price-input :value="price" @input="onInput"></price-input>
<p>{{price}}</p>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Vue.component('price-input', {
// 5、将父组件的value值通过props传递给子组件
// 1、当有数据输入时触发了该组件的input事件
template: '<input :value="value" @input="updateVal($event.target.value)" type="text">',
props: ["value"],
methods: {
updateVal: function(val) {
// 2、手动触发父组件的input事件并将值传给父组件
this.$emit('input', val);
}
}
});
var app = new Vue({
el: '#app',
data: {
price: ''
},
methods: {
onInput: function(val) {
this.price = val;
}
}
});
文章作者: Kwin
文章链接: http://huangkun.host/2020/03/25/20180823-vue入门/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Kwin 's Blog
打赏
  • 微信
    微信
  • 支付宝
    支付宝

评论