Vue
初始化
vue
1 2
| <script src="https://unpkg.com/vue@next"></script> <div id="root"></div>
|
js
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
| const app = Vue.createApp({ data() { return { inputValue: '', list: [] } }, methods: { handleAddItem() { this.list.push(this.inputValue); this.inputValue = ''; } }, template: ` <div> <input v-model="inputValue" /> <button v-on:click="handleAddItem" v-bind:title="inputValue" > 增加 </button> <ul> <todo-item v-for="(item, index) of list" v-bind:content="item" v-bind:index="index" /> </ul> </div> ` }); app.component('todo-item', { props: ['content', 'index'], template: '<li>{{index}} -- {{content}}</li>' }); app.mount('#root');
|
MVVM设计模式,M -> Model 数据, V -> View 视图, VM -> ViewModel 视图数据连接层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
const app = Vue.createApp({ data() { return { message: 'hello world' } }, template: "<div>{{message}}</div>" });
const vm = app.mount('#root');
|
vm.$data.message
vm.$option
生命周期函数
生命周期函数
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
| const app = Vue.createApp({ data() { return { message: 'hello world' } }, beforeCreate(){ }, created(){ },
beforeMount(){ },
mounted(){ },
beforeUpdate(){ },
updated(){ },
beforeUnmount(){ },
unmounted(){ }, template: "<div>{{message}}</div>" }); const vm = app.mount('#root');
|
常用模板语法
- 差值表达式
- v-once 只绑定第一次
- v-html
- v-bind (:) 绑定到标签的属性,使用动态数据
- v-on (@) 绑定到标签的事件 (方法放在methods)
- v-if v-show
- v-model 数据绑定
- 动态参数
- 修饰符
动态参数,动态属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| const app = Vue.createApp({ data() { return { name: "title", msg:"111", event:"cilck" } }, methods: { handleClick() { alert('click') } }, template: `<div :[name]="msg" @[event]="handleClick"></div>` });
|
修饰符.prevent阻止默认行为 等价于e.prevnetDedault()
1 2 3 4 5
| template: ` <form action="https://www.baidu.com" @click.prevent="handleClick"> <button type="submit">提交</button> </form> `
|
数据,方法,计算属性和侦听器
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
| const app = Vue.createApp({ data() { return { message: "hello world", count: 2, price: 5, newTotal: 10, } }, watch: { price(current, prev) { this.newTotal = current * this.count; } }, computed: { total() { return Date.now() + this.count; } }, methods: { formatString(string) { return string.toUpperCase(); }, getTotal() { return Date.now(); }, }, template: ` <div>{{total}} {{message}} {{newTotal}} </div> ` }); const vm = app.mount('#root');
|
样式
1 2 3 4 5 6 7 8
| <style> .red { color: red; } .green { color: green; } </style>
|
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
| const app = Vue.createApp({ data() { return { classString: 'red', classObject: { red: false, green: true }, classArray: ['red', 'green', {brown: false}], styleString: 'color: yellow;background: orange', styleObject: { color: 'orange', background: 'yellow' } } }, template: ` <div :style="classString"> Hello World </div> <div :style="classArray"> Hello World </div> <div :style="classArray"> Hello World </div> <div :style="styleString"> Hello World </div> <div :style="styleObject"> Hello World </div> <demo /> ` }); app.component('demo', { template: ` <div :class="$attrs.class">one</div> <div :class="$attrs.class">two</div> ` })
|
条件渲染
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| const app = Vue.createApp({ data() { return { show: false, conditionOne: false, conditionTwo: true } }, template: ` <div v-if="show">Hello World</div> <div v-if="conditionOne">if</div> <div v-else-if="conditionTwo">elseif</div> <div v-else>else</div> <div v-show="show">Bye World</div> ` });
|
列表渲染
list (item,index) in listArray
对象 (value, key, index) in listObject
:key唯一key,提高性能,复用DOM
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
| const app = Vue.createApp({ data() { return { listArray: ['dell', 'lee', 'teacher'], listObject: { firstName: 'dell', lastName: 'lee', job: 'teacher' } } }, methods: { handleAddBtnClick() { } }, template: ` <div> <template v-for="(value, key, index) in listObject" :key="index" > <div v-if="key !== 'lastName'"> {{value}} -- {{key}} </div> </template> <div v-for="item in 10">{{item}}</div> <button @click="handleAddBtnClick">新增</button> </div> ` });
|
事件绑定
1
| <div @click="handleClick(), handleClick2()">123</div>
|
修饰符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
const app = Vue.createApp({ methods: { handleClick() { console.log('click') }, }, template: ` <div> <div @click.ctrl.exact="handleClick">123</div> </div> ` });
|
双向数据绑定
v-model
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
| const app = Vue.createApp({ data() { return { message: 'hello', checkbox1:true, checkbox2:[] } }, template: ` <div> {{message}} <input v-model="message" /> <textarea v-model="message" /> <input type="checkbox" v-model="checkbox1" /> <input type="radio" v-model="checkbox1" /> <select v-model="message" > <option>A</option> <option>B</option> <option>C</option> </select> <select v-model="message" multple> <option>A</option> <option>B</option> <option>C</option> </select> <input v-model.lazy="message" /> </div> ` });
|
ref获取DOM
ref获取DOM节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| const app = Vue.createApp({ data() { return { count: 1} }, mounted:{ this.$refs.count }, template: ` <div> <button ref="count">Add</button> </div> ` });
|
ref获取DOM节点
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 app = Vue.createApp({ data() { return { count: 1 } }, mounted() { this.$refs.common.sayHello() }, template: ` <div> <common-item ref="common"/> </div> ` }); app.component('common-item', { methods: { sayHello(){ console.log("hello"); } }, template: `<div>sayHello</div>` });
|
provide / inject
传一个值给子组件的子组件,可以多次用动态传值props接受来完成,也可以用Provide / Inject
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| const app = Vue.createApp({ data() { return { count: 1} }, template: ` <div> <child :count="count" /> </div> ` }); app.component('child', { props: ['count'], template: `<child-child :count="count"/>` }); app.component('child-child', { props: ['count'], template: `<div>{{count}}</div>` });
|
Provide / Inject完成上述例子,同时provide传递的数据是一次性的,不是双向数据绑定的
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 app = Vue.createApp({ data() { return { count: 1} }, provide() { return { count: this.count, } }, template: ` <div> <child :count="count" /> <button @click="count += 1">Add</button> </div> ` }); app.component('child', { template: `<child-child />` }); app.component('child-child', { inject: ['count'], template: `<div>{{count}}</div>` });
|