listArray:['a','b','c'] ,今天小编就来说说关于vue 知识总结?下面更多详细答案一起来看看吧!

vue 知识总结(vue基础知识)

vue 知识总结

基础认识变量数组

listArray:['a','b','c']

对象

listObject:{ GirlOne:'a', GirlTwo:'b', GirlThree:'c' }

'<div>{{counter}}</div>'

mounted

mounted() { console.log('页面加载完成后-自动执行') },

v-onv-if,v-else

:class="message=='jspang.com'?'one':'two'" template: ` <h2 @click="handleItemClick" v-if="message=='jspang.com'" class="one" > {{message}} </h2> <h2 @click="handleItemClick" v-else class="three"> {{message}} </h2> `

v-for

<ul> <li v-for="(item,index) in listArray" :key="index item"> [{{index}}]{{item}} </li> </ul>

data(){ return{ listObject:{ GirlOne:'a', GirlTwo:'b', GirlThree:'c' } } } <ul> <li v-for="(value,key,index) in listObject" :key="key"> [{{index}}]{{value}}-{{key}} </li> </ul>

<span v-for="count in 99">{{count}},</span>

<ul> <template v-for="(item,index) in listArray" :key="index item" > <li v-if="item != 'a'"> [{{index}}]{{item}} </li> </template> </ul>

Demo

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>佳丽列表</title> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="app"></div> </body> <script> Vue.createApp({ data() { return { inputValue: '', list: ['1号', '2号', '3号'] , setMeal: 'china america', isShowMeal: false , } }, methods: { handleAddItem() { this.list.push(this.inputValue) this.inputValue = '' }, welcomeBtnClick(){ // alert('111') this.content = 'huanying' }, byeBtnClick(){ // alert('222') this.content = 'baibai' }, showOrHideBtnClick(){ this.isShowMeal = !this.isShowMeal // 将操作者取反 false变成true } }, template: ` <div> <div> <button v-on:click="welcomeBtnClick">laila</button> <button v-on:click="byeBtnClick">zoula</button> <div v-if='isShowMeal'>{{setMeal}}</div> <button v-on:click="showOrHideBtnClick">show/not show</button> </div> <input v-model="inputValue" /> <button v-on:click="handleAddItem">增加佳丽</button> <ul> <li v-for= "item of list">{{item}}</li> </ul> </div> ` }).mount("#app") </script> </html>

v-html,v-bind,v-once

<h2 v-bind:title="message2">{{message2}}</h2> <h2 :title="message2">{{message2}}</h2>

<script> const app = Vue.createApp({ data() { return { message: 'jspang.com', count: 5, message2: 'xxxxxx' } }, methods: { handleItemClick() { this.message = this.message == 'jspang.com' ? "技术胖" : "jspang.com" } }, template: `<h2 v-on:click="handleItemClick" v-html="message" v-once > </h2> <div>{{count>2?'大':'小'}}</div> # 三元表达式 在模板中可以使用js的表达式 <h2 v-bind:title="message2">{{message2}}</h2> # 不用v-bind title就是message2字符串 ` }) const vm = app.mount("#app") </script>

v-show

<script> const app=Vue.createApp({ data(){ return{ show: true, } }, template:` <h2 v-show="show">JSPang.com</h2> ` }) const vm=app.mount("#app") </script>

vue的方法createApp()

const app = Vue.createApp(){}

mount()

app.mount("#app")

根组件vmmvvm

<script> const app = Vue.createApp({ data() { return { message: 'jspang.com' //1.在这里定义了数据,也就是`model`数据 } }, template: "<h2>{{message}}</h2>" //2.在这里定义了模板,也就是`view`, //定义后的自动关联,就叫做`vm`,viewModel数据视图连接层。 }) app.mount("#app") </script>

生命周期函数

methods: { handleItemClick() { alert('jspang.com') } }, template: "<h2 v-on:click='handleItemClick'>{{message}}</h2>"

beforeCreate():在实例生成之前会自动执行的函数 created(): 在实例生成之后会自动执行的函数 beforeMount(): 在模板渲染完成之前执行的函数 mounted(): 在模板渲染完成之后执行的函数 beforeUpdate() :当data中的数据变化时, 会立即自动执行的函数 updated():当data中的数据发生变化,页面重新渲染完后,会自动执行的函数 beforeUnmount() :当Vue应用失效时,会自动执行的函数 unmounted() : 当Vue应用失效时,且DOM完全销毁之后,会自动执行

vue2和3生命周期函数对比

Vue2--------------vue3 beforeCreate -> setup() created -> setup() beforeMount -> onBeforeMount mounted -> onMounted beforeUpdate -> onBeforeUpdate updated -> onUpdated beforeDestroy -> onBeforeUnmount destroyed -> onUnmounted activated -> onActivated deactivated -> onDeactivated errorCaptured -> onErrorCaptured

模板的动态参数

<script> const app = Vue.createApp({ data() { return { message: 'xxxx', name: 'title', event: 'click' } }, methods: { hanldClick(){ alert('欢迎光临红浪漫') } }, template:` <h2 @[event]="hanldClick" ## 等价于 v-on:click = 'hanldClick' :[name]="message" ## 等价于 v-bind:title = 'xxxx' > {{message}} </h2> ` }) const vm = app.mount("#app") </script>

阻止默认事件

<form action="https://jspang.com" @click.prevent="hanldeButton"> <button type="submit">默认提交</button> </form>

computed计算属性,watch监听器

<script> const app=Vue.createApp({ data(){ return{ message:'jspang.com' , price: 10, count: 2, } }, methods:{ getTotal(){ // return this.price * (this.count ); return Date.now() }, addCount(){ this.count }, }, watch:{ count(current, prev){ console.log('count changed watch') console.log('现在的值watch:',current) console.log('变化前的值watch:',prev) } }, computed:{ total(){ return this.price * this.count; }, getTime(){ return Date.now(); }, countComputed(current, prev){ console.log('count changed computed') console.log('现在的值computed:',current) console.log('变化前的值computed:',prev) } }, template:` <h2>{{getTime}}</h2> <h2>{{getTotal()}}</h2> <h2> {{total}}</h2> <button @click='addCount'>more</button> <h2> {{countComputed}}</h2> ` }) const vm=app.mount("#app") </script>

模板绑定样式

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Demo14</title> <script src="https://unpkg.com/vue@next" ></script> <style> .red {color:red} .green {color: green} .background {background-color: yellow} .redImport {color: red !important;} </style> </head> <body> <div id="app"></div> </body> <script> const app=Vue.createApp({ data(){ return { classString:'red', classObject:{red:true,background:true}, classArray1:['green','background',{redImport:true}], classArray2:['green','background',{red:true}], } }, template:` <h2 :class="classArray1">1</h2> <h2 :class="classArray2">2</h2> ` }) const vm=app.mount("#app") </script> </html>

app.component('sonCom',{ template:` <div>SonCom</div> ` }) template:` <h2 :class="classArray">JSPang.com</h2> <sonCom /> `

data(){ return{ //..... styleString:'color:orange;', styleObject:{ color:'red', background:'yellow' } } }, template:` <h2 :style="styleString">JSPang.com</h2> <h2 :style="styleObject">JSPang.com</h2> `

绑定事件方法和参数

<script> const app=Vue.createApp({ data(){ return{ count:0 } }, methods:{ addCountClick(event){ this.count ; console.log(event) console.log(event.target) }, addCountClick2(num,event){ this.count = num console.log(event.target) }, handleBtnClick1(){ alert(1) }, handleBtnClick2(){ alert(2) }, }, template:` <div>目前已点佳丽数量{{count}}.</div> <button @click=addCountClick>增加一位佳丽</button> <button @click="count ">增加一位佳丽2</button> <button @click=addCountClick2(2,$event)>增加一位佳丽3</button> <button @click=handleBtnClick1(),handleBtnClick2()>增加一位佳丽4</button> ` }) const vm=app.mount("#app") </script>

事件修饰符

methods:{ addCountClick(){ this.count }, handleBtnClick1(){ alert(1) }, }, template:` <div @click="handleBtnClick1"> <div>number is{{count}}.</div> <button @click=" addCountClick()">add</button> </div> `

template:` <div @click.self="handleBtnClick1"> 点我 <div>目前已点佳丽数量{{count}}.</div> <button @click=" addCountClick()">增加一位佳丽</button> </div> `

template:` <div @click.capture="handleBtnClick1"> //修改了此处 我是最外层的DIV文字 <div>目前已点佳丽数量{{count}}.</div> <button @click=" addCountClick()">增加一位佳丽</button> </div> `

<button @click.once="addCountClick()">增加一位佳丽</button>

按键,鼠标修饰符

<script> const app=Vue.createApp({ data(){ return{} }, methods:{ handleKeyDown(){ console.log('keydown') } }, template:` <div> <input @keydown="handleKeyDown"/> <input @keydown.enter="handleKeyDown"/> </div> ` }) const vm=app.mount("#app") </script>

<script> const app=Vue.createApp({ data(){ return{} }, methods:{ handleClick(){ alert('click') } }, template:` <div @click.right="handleClick">xxxxxx</div> ` }) const vm=app.mount("#app") </script>

数据双向绑定v-model

<script> const app=Vue.createApp({ data(){ return{ name: '', list: [], checked:false, str: '', testAreaName: '' } }, methods:{ }, template:` <div> <div>{{name}}</div> <input v-model="name" /> <div>{{checked}}<input type="checkbox" v-model="checked" /></div> <div> <div>{{testAreaName}}</div> <div><textarea v-model="testAreaName" /></div> </div> <div> {{list}} a<input type="checkbox" v-model="list" value="a" /> b<input type="checkbox" v-model="list" value="b" /> c<input type="checkbox" v-model="list" value="c" /> </div> <div> {{str}} a<input type="radio" v-model="str" value="a" /> b<input type="radio" v-model="str" value="b" /> c<input type="radio" v-model="str" value="c" /> </div> </div> ` }) const vm=app.mount("#app") </script>

[toc]

基础认识变量数组

listArray:['a','b','c']

对象

listObject:{ GirlOne:'a', GirlTwo:'b', GirlThree:'c' }

'<div>{{counter}}</div>'

mounted

mounted() { console.log('页面加载完成后-自动执行') },

v-onv-if,v-else

:class="message=='jspang.com'?'one':'two'" template: ` <h2 @click="handleItemClick" v-if="message=='jspang.com'" class="one" > {{message}} </h2> <h2 @click="handleItemClick" v-else class="three"> {{message}} </h2> `

v-for

<ul> <li v-for="(item,index) in listArray" :key="index item"> [{{index}}]{{item}} </li> </ul>

data(){ return{ listObject:{ GirlOne:'a', GirlTwo:'b', GirlThree:'c' } } } <ul> <li v-for="(value,key,index) in listObject" :key="key"> [{{index}}]{{value}}-{{key}} </li> </ul>

<span v-for="count in 99">{{count}},</span>

<ul> <template v-for="(item,index) in listArray" :key="index item" > <li v-if="item != 'a'"> [{{index}}]{{item}} </li> </template> </ul>

Demo

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>佳丽列表</title> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="app"></div> </body> <script> Vue.createApp({ data() { return { inputValue: '', list: ['1号', '2号', '3号'] , setMeal: 'china america', isShowMeal: false , } }, methods: { handleAddItem() { this.list.push(this.inputValue) this.inputValue = '' }, welcomeBtnClick(){ // alert('111') this.content = 'huanying' }, byeBtnClick(){ // alert('222') this.content = 'baibai' }, showOrHideBtnClick(){ this.isShowMeal = !this.isShowMeal // 将操作者取反 false变成true } }, template: ` <div> <div> <button v-on:click="welcomeBtnClick">laila</button> <button v-on:click="byeBtnClick">zoula</button> <div v-if='isShowMeal'>{{setMeal}}</div> <button v-on:click="showOrHideBtnClick">show/not show</button> </div> <input v-model="inputValue" /> <button v-on:click="handleAddItem">增加佳丽</button> <ul> <li v-for= "item of list">{{item}}</li> </ul> </div> ` }).mount("#app") </script> </html>

v-html,v-bind,v-once

<h2 v-bind:title="message2">{{message2}}</h2> <h2 :title="message2">{{message2}}</h2>

<script> const app = Vue.createApp({ data() { return { message: 'jspang.com', count: 5, message2: 'xxxxxx' } }, methods: { handleItemClick() { this.message = this.message == 'jspang.com' ? "技术胖" : "jspang.com" } }, template: `<h2 v-on:click="handleItemClick" v-html="message" v-once > </h2> <div>{{count>2?'大':'小'}}</div> # 三元表达式 在模板中可以使用js的表达式 <h2 v-bind:title="message2">{{message2}}</h2> # 不用v-bind title就是message2字符串 ` }) const vm = app.mount("#app") </script>

v-show

<script> const app=Vue.createApp({ data(){ return{ show: true, } }, template:` <h2 v-show="show">JSPang.com</h2> ` }) const vm=app.mount("#app") </script>

vue的方法createApp()

const app = Vue.createApp(){}

mount()

app.mount("#app")

根组件vmmvvm

<script> const app = Vue.createApp({ data() { return { message: 'jspang.com' //1.在这里定义了数据,也就是`model`数据 } }, template: "<h2>{{message}}</h2>" //2.在这里定义了模板,也就是`view`, //定义后的自动关联,就叫做`vm`,viewModel数据视图连接层。 }) app.mount("#app") </script>

生命周期函数

methods: { handleItemClick() { alert('jspang.com') } }, template: "<h2 v-on:click='handleItemClick'>{{message}}</h2>"

beforeCreate():在实例生成之前会自动执行的函数 created(): 在实例生成之后会自动执行的函数 beforeMount(): 在模板渲染完成之前执行的函数 mounted(): 在模板渲染完成之后执行的函数 beforeUpdate() :当data中的数据变化时, 会立即自动执行的函数 updated():当data中的数据发生变化,页面重新渲染完后,会自动执行的函数 beforeUnmount() :当Vue应用失效时,会自动执行的函数 unmounted() : 当Vue应用失效时,且DOM完全销毁之后,会自动执行

vue2和3生命周期函数对比

Vue2--------------vue3 beforeCreate -> setup() created -> setup() beforeMount -> onBeforeMount mounted -> onMounted beforeUpdate -> onBeforeUpdate updated -> onUpdated beforeDestroy -> onBeforeUnmount destroyed -> onUnmounted activated -> onActivated deactivated -> onDeactivated errorCaptured -> onErrorCaptured

模板的动态参数

<script> const app = Vue.createApp({ data() { return { message: 'xxxx', name: 'title', event: 'click' } }, methods: { hanldClick(){ alert('欢迎光临红浪漫') } }, template:` <h2 @[event]="hanldClick" ## 等价于 v-on:click = 'hanldClick' :[name]="message" ## 等价于 v-bind:title = 'xxxx' > {{message}} </h2> ` }) const vm = app.mount("#app") </script>

阻止默认事件

<form action="https://jspang.com" @click.prevent="hanldeButton"> <button type="submit">默认提交</button> </form>

computed计算属性,watch监听器

<script> const app=Vue.createApp({ data(){ return{ message:'jspang.com' , price: 10, count: 2, } }, methods:{ getTotal(){ // return this.price * (this.count ); return Date.now() }, addCount(){ this.count }, }, watch:{ count(current, prev){ console.log('count changed watch') console.log('现在的值watch:',current) console.log('变化前的值watch:',prev) } }, computed:{ total(){ return this.price * this.count; }, getTime(){ return Date.now(); }, countComputed(current, prev){ console.log('count changed computed') console.log('现在的值computed:',current) console.log('变化前的值computed:',prev) } }, template:` <h2>{{getTime}}</h2> <h2>{{getTotal()}}</h2> <h2> {{total}}</h2> <button @click='addCount'>more</button> <h2> {{countComputed}}</h2> ` }) const vm=app.mount("#app") </script>

模板绑定样式

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Demo14</title> <script src="https://unpkg.com/vue@next" ></script> <style> .red {color:red} .green {color: green} .background {background-color: yellow} .redImport {color: red !important;} </style> </head> <body> <div id="app"></div> </body> <script> const app=Vue.createApp({ data(){ return { classString:'red', classObject:{red:true,background:true}, classArray1:['green','background',{redImport:true}], classArray2:['green','background',{red:true}], } }, template:` <h2 :class="classArray1">1</h2> <h2 :class="classArray2">2</h2> ` }) const vm=app.mount("#app") </script> </html>

app.component('sonCom',{ template:` <div>SonCom</div> ` }) template:` <h2 :class="classArray">JSPang.com</h2> <sonCom /> `

data(){ return{ //..... styleString:'color:orange;', styleObject:{ color:'red', background:'yellow' } } }, template:` <h2 :style="styleString">JSPang.com</h2> <h2 :style="styleObject">JSPang.com</h2> `

绑定事件方法和参数

<script> const app=Vue.createApp({ data(){ return{ count:0 } }, methods:{ addCountClick(event){ this.count ; console.log(event) console.log(event.target) }, addCountClick2(num,event){ this.count = num console.log(event.target) }, handleBtnClick1(){ alert(1) }, handleBtnClick2(){ alert(2) }, }, template:` <div>目前已点佳丽数量{{count}}.</div> <button @click=addCountClick>增加一位佳丽</button> <button @click="count ">增加一位佳丽2</button> <button @click=addCountClick2(2,$event)>增加一位佳丽3</button> <button @click=handleBtnClick1(),handleBtnClick2()>增加一位佳丽4</button> ` }) const vm=app.mount("#app") </script>

事件修饰符

methods:{ addCountClick(){ this.count }, handleBtnClick1(){ alert(1) }, }, template:` <div @click="handleBtnClick1"> <div>number is{{count}}.</div> <button @click=" addCountClick()">add</button> </div> `

template:` <div @click.self="handleBtnClick1"> 点我 <div>目前已点佳丽数量{{count}}.</div> <button @click=" addCountClick()">增加一位佳丽</button> </div> `

template:` <div @click.capture="handleBtnClick1"> //修改了此处 我是最外层的DIV文字 <div>目前已点佳丽数量{{count}}.</div> <button @click=" addCountClick()">增加一位佳丽</button> </div> `

<button @click.once="addCountClick()">增加一位佳丽</button>

按键,鼠标修饰符

<script> const app=Vue.createApp({ data(){ return{} }, methods:{ handleKeyDown(){ console.log('keydown') } }, template:` <div> <input @keydown="handleKeyDown"/> <input @keydown.enter="handleKeyDown"/> </div> ` }) const vm=app.mount("#app") </script>

<script> const app=Vue.createApp({ data(){ return{} }, methods:{ handleClick(){ alert('click') } }, template:` <div @click.right="handleClick">xxxxxx</div> ` }) const vm=app.mount("#app") </script>

数据双向绑定v-model

<script> const app=Vue.createApp({ data(){ return{ name: '', list: [], checked:false, str: '', testAreaName: '' } }, methods:{ }, template:` <div> <div>{{name}}</div> <input v-model="name" /> <div>{{checked}}<input type="checkbox" v-model="checked" /></div> <div> <div>{{testAreaName}}</div> <div><textarea v-model="testAreaName" /></div> </div> <div> {{list}} a<input type="checkbox" v-model="list" value="a" /> b<input type="checkbox" v-model="list" value="b" /> c<input type="checkbox" v-model="list" value="c" /> </div> <div> {{str}} a<input type="radio" v-model="str" value="a" /> b<input type="radio" v-model="str" value="b" /> c<input type="radio" v-model="str" value="c" /> </div> </div> ` }) const vm=app.mount("#app") </script>

,