vue(2)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://unpkg.com/vue@3"></script>
</head>
<body>
<div id="app">
<p>{{reverseStr}}</p>
<ul>
<li>{{text.split('').reverse().join('')}}</li>
<li>{{text.split('').reverse().join('')}}</li>
<!--上面两行,同样的功能要计算两次,消耗性能,并且导致HTML代码臃肿,下面用计算属性更优-->
<li>{{reverseStr}}</li>
<li>{{reverseStr}}</li>
<li>{{reverseStr}}</li>
<li>{{reverseStr}}</li>
</ul>
</div>
<script>
const {createApp}=Vue
createApp({
data(){
return{
text:'Hello,world!'
}
},
computed:{
reverseStr(){//这里一定要指定this.text,this指的是当前实例,可以理解为data里的数据
return this.text.split('').reverse().join('')
}
}
}).mount('#app')
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://unpkg.com/vue@3"></script>
</head>
<body>
<div id="app">
<p>{{count}}</p>
<button v-on:click="increment()">+</button>
<button @click="increment()">+</button>
<button v-on:click="decrement()">-</button>
<button @click="decrement()">-</button>
<button v-on:click="increment(5)">+5</button>
<button v-on:click="increment(6)">+6</button>
<button v-on:click="decrement(5)">-5</button>
<button v-on:click="decrement(6)">-6</button>
</div>
<script>
const {createApp}=Vue
createApp({
data(){
return{
count:0
}
},
computed:{//计算属性
},
methods:{//方法
increment (a=1) {
// this.count=this.count+a
this.count+=a
},
decrement (a=1) {
this.count-=a
}
}
}).mount('#app')
</script>
</body>
</html>
计算属性VS方法
注意:计算属性和方法,在确定上确实是相同的,然而不同之处在于计算属性会基于其响应式依赖的缓存。一个计算属性仅会在其响应式依赖更新时才会被重新计算。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://unpkg.com/vue@3"></script>
</head>
<body>
<div id="app">
<ul>
<li v-if="type==='A'">AAAA</li>
<li v-else-if="type==='B'">BBBB</li>
<li v-else>CCCC</li>
</ul>
<p v-show="seen">现在你看的到我</p>
</div>
<script>
const {createApp}=Vue
createApp({
data(){
return{
type:'B',
seen:false
}
}
}).mount('#app')
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://unpkg.com/vue@3"></script>
</head>
<body>
<div id="app">
<table>
<thead>
<tr>
<td>id</td>
<td>书名</td>
<td>价格</td>
</tr>
</thead>
<tbody>
<tr v-for="(book,index) of books":key="index">
<!-- key一般是数据库里的数据上的唯一且不重复的主键——即id-->
<td>{{book.id}}</td>
<td>{{book.name}}</td>
<td>{{book.price}}</td>
</tr>
</tbody>
</table>
</div>
<script>
const {createApp}=Vue
createApp({
data(){
return{
books:[
{id:0,name:'三体',price:68},
{id:1,name:'平凡的世界',price:128},
{id:1,name:'围城',price:48}
]
}
}
}).mount('#app')
</script>
</body>
</html>
表单输入绑定
(1)单行文本
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://unpkg.com/vue@3"></script>
</head>
<body>
<div id="app">
<!-- <input type="text" :value="text" @inout="ipt($event)">-->
<input type="text" v-model="text">
<p>{{text}}</p>
</div>
<script>
const {createApp}=Vue
createApp({
data(){
return{
text:'Hello,world!'
}
},
/* methods:{
ipt(event){
this.text=event.target.value
}
}*/
}).mount('#app')
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../../vue-shopping-cart/node_modules/vue/dist/vue.global.js"></script>
</head>
<body>
<div id="app">
<textarea name="" id="" v-model="message"></textarea>
<!-- 多行文本,及换行符,可以被v-model实时修改数据-->
<span>Musnfjbj</span>
<!-- 此css属性可以展示字符串中的换行符-->
<p style="white-space: pre-line;">{{message}}</p>
</div>
</body>
</html>
<script>
const {createApp}=Vue
createApp({
data(){
return{
message:''
}
}
}).mount('#app')
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../../vue-shopping-cart/node_modules/vue/dist/vue.global.js"></script>
</head>
<body>
<div id="app">
<label for="ipt"></label>
<input type="checkbox" id="ipt" :checked="checked">数据单向绑定,data驱动UI,UI不可以修改data
<p>{{checked}}</p>
</div>
</body>
</html>
<script>
const {createApp}=Vue
createApp({
data(){
return{
checked:false
}
}
}).mount('#app')
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../../vue-shopping-cart/node_modules/vue/dist/vue.global.js"></script>
</head>
<body>
<div id="app">
<label for="ipt"></label>
<input type="checkbox" id="ipt" v-model="checked">
<p>{{checked}}</p>
</div>
</body>
</html>
<script>
const {createApp}=Vue
createApp({
data(){
return{
checked:true
}
}
}).mount('#app')
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../../vue-shopping-cart/node_modules/vue/dist/vue.global.js"></script>
</head>
<body>
<div id="app">
<h2>选中的人是:{{checkedNames}}</h2>
<input type="checkbox" v-model="checkedNames" value="Jack">Jack
<input type="checkbox" v-model="checkedNames" value="John">John
<input type="checkbox" v-model="checkedNames" value="Mike">Mike
</div>
</body>
</html>
<script>
const {createApp}=Vue
createApp({
data(){
return{
checkedNames:[]
}
}
}).mount('#app')
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../../vue-shopping-cart/node_modules/vue/dist/vue.global.js"></script>
</head>
<body>
<div id="app">
<h2>选中的人是:{{picked}}</h2>
<input type="radio" v-model="picked" value="1">男
<input type="radio" v-model="picked" value="2">女
</div>
</body>
</html>
<script>
const {createApp}=Vue
createApp({
data(){
return{
picked:1
}
}
}).mount('#app')
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../../vue-shopping-cart/node_modules/vue/dist/vue.global.js"></script>
</head>
<body>
<div id="app">
<div>Selected: {{ selected }}</div>
<select v-model="selected">
<option disabled value="">Please select one</option>
<option>A</option>
<option>B</option>
<option>C</option>
</select>
</div>
</body>
</html>
<script>
const {createApp}=Vue
createApp({
data(){
return{
selected:''
}
}
}).mount('#app')
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../../vue-shopping-cart/node_modules/vue/dist/vue.global.js"></script>
</head>
<body>
<div id="app">
<div>Selected: {{ selected }}</div>
<select v-model="selected" multiple>
<option>Aaaaaaaa</option>
<option>Bbbbbbbb</option>
<option>Cccccccc</option>
</select>
</div>
</body>
</html>
<script>
const {createApp}=Vue
createApp({
data(){
return{
selected:[]
}
}
}).mount('#app')
</script>
.lazy
默认情况下,v-model 会在每次 input 事件后更新数据 (
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../../vue-shopping-cart/node_modules/vue/dist/vue.global.js"></script>
</head>
<body>
<div id="app">
<input type="text" v-model="message">
<input type="text" v-model.lazy="message">
</div>
</body>
</html>
<script>
const {createApp}=Vue
createApp({
data(){
return{
message:''
}
}
}).mount('#app')
</script>
.number
如果你想让用户输入自动转换为数字,你可以在 v-model 后添加 .number 修饰符来管理输入:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../../vue-shopping-cart/node_modules/vue/dist/vue.global.js"></script>
</head>
<body>
<div id="app">
<input type="text" v-model="message">
<input type="text" v-model.number="message">
</div>
</body>
</html>
<script>
const {createApp}=Vue
createApp({
data(){
return{
message:''
}
}
}).mount('#app')
</script>
number 修饰符会在输入框有 type="number" 时自动启用。
.trim
如果你想要默认自动去除用户输入内容中两端的空格,你可以在 v-model 后添加 .trim 修饰符:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../../vue-shopping-cart/node_modules/vue/dist/vue.global.js"></script>
</head>
<body>
<div id="app">
<input type="text" v-model="message">
<input type="text" v-model.trim="message">
<p>{{message}}</p>
</div>
</body>
</html>
<script>
const {createApp}=Vue
createApp({
data(){
return{
message:''
}
}
}).mount('#app')
</script>
浙公网安备 33010602011771号