一、vue开发环境
开发环境为visual studio code,预先安装中文汉化包,安装插件为Live Server(浏览器实时预览)
二、vue基础
1、vue的第一个例子
< html>
< head>
< title> vue学习第一课</ title>
</ head>
< body>
< div id = " app" >
{{message}}
</ div>
< script src = " https://cdn.jsdelivr.net/npm/vue/dist/vue.js" > </ script>
< script>
var app = new Vue ( {
el : "#app" ,
data : {
message : "Hello World!"
}
} ) ;
</ script>
</ body>
</ html>
2、vue基础第二个例子
< html>
< head>
< title> vue数组及作用域范围</ title>
</ head>
< body>
{{message}}
< div id = " app" >
{{message}}
< p>
{{school}}
</ p>
< p>
学校:{{school.name}}
</ p>
< p>
电话:{{school.mobile}}
</ p>
< p>
校区:{{campus}}
</ p>
< p>
校区:{{campus[1]}}
</ p>
</ div>
< script src = " https://cdn.jsdelivr.net/npm/vue/dist/vue.js" > </ script>
< script>
var app = new Vue ( {
el : "#app" ,
data : {
message : "你好世界!" ,
school : {
name : "师范大学" ,
mobile : "66666666"
} ,
campus : [ "一二一校区" , "北京校区" ]
}
} ) ;
</ script>
</ body>
</ html>
三、v指令
1、v-text,v-content,两种功能相同,语法为v-text=“”,例如v-text=“message”
< div id = " app" >
< h2 v-text = " message" > </ h2>
< h2 v-text = " message+'北京'" > </ h2>
< h2 v-text = " message" > 北京</ h2>
< h2 v-text = " info" > 北京</ h2>
< h2> {{message}} China</ h2>
< h2> {{info}} China</ h2>
</ div>
< script src = " https://cdn.jsdelivr.net/npm/vue/dist/vue.js" > </ script>
< script>
var app = new Vue ( {
el : "#app" ,
data : {
message : "你好世界!" ,
info : "师范大学"
}
} ) ;
</ script>
2、v-html,如果内容有html使用,语法为v-html=“”,例如v-html=“message”
< div id = " app" >
< h2 v-text = " message" > </ h2>
< h2 v-html = " message" > </ h2>
</ div>
< script src = " https://cdn.jsdelivr.net/npm/vue/dist/vue.js" > </ script>
< script>
var app = new Vue ( {
el : "#app" ,
data : {
message : "<a href=\"https://www.ynnu.edu.cn\" target=\"_blank\" >师范大学</a>" ,
}
} ) ;
</ script>
3、v-on可以简写为@,语法为v-on:绑定事件,如v-on:click
< div id = " app" >
< input type = " button" v-on: click= " messagebox" value = " 单击" />
< input type = " button" @click = " messagebox" value = " 单击" />
< input type = " button" @dblclick = " messagebox" value = " 双击" />
< input type = " button" @click = " changeinfo" value = " 修改" />
< p> {{info}}</ p>
< p> 请输入您的名字:< input type = " text" @keyup.enter = " msg" /> </ p>
</ div>
< script src = " https://cdn.jsdelivr.net/npm/vue/dist/vue.js" > </ script>
< script>
var app = new Vue ( {
el : "#app" ,
data : {
info : "你是姐姐还是哥哥?" ,
name : ""
} ,
methods : {
messagebox : function ( ) {
alert ( "世界你好!" ) ;
} ,
changeinfo : function ( ) {
this . info+= ",我是弟弟!" ;
} ,
msg : function ( p )
{
alert ( "欢迎您老铁!" ) ;
}
}
} ) ;
</ script>
v-on应用,计数器
< div id = " app" >
< input type = " button" @click = " jian" value = " -" />
< span> {{info}}</ span>
< input type = " button" @click = " jia" value = " +" />
</ div>
< script src = " https://cdn.jsdelivr.net/npm/vue/dist/vue.js" > </ script>
< script>
var app = new Vue ( {
el : "#app" ,
data : {
info : 0
} ,
methods : {
jia : function ( ) {
if ( this . info< 10 ) { this . info+= 1 ; }
else { alert ( "别点了,已经最大了" ) ; }
} ,
jian : function ( ) {
if ( this . info> 0 ) { this . info-= 1 ; }
else { alert ( "别点了,已经最小了" ) ; }
}
}
} ) ;
</ script>
v-show,语法为v-show=“”,true显示,false是隐藏,v-show的值是真假
< div id = " app" >
< input type = " button" value = " 切换v-show值" @click = " change" />
< p v-show = " info" >
sssssssssssssssssssssssssssssssssdadasdadadsadw
</ p>
< p>
< img v-show = " info" src = " 1.png" style = " width : 400px; height : 200px; " />
</ p>
< p>
< img v-show = " age=18" src = " 1.png" style = " width : 400px; height : 200px; " />
</ p>
</ div>
< script src = " https://cdn.jsdelivr.net/npm/vue/dist/vue.js" > </ script>
< script>
var app = new Vue ( {
el : "#app" ,
data : {
info : true ,
age : 18
} ,
methods : {
change : function ( ) {
this . info= ! this . info;
}
}
} ) ;
</ script>
v-if,与v-show类似,但是v-show是操作dispaly的属性,而v-if是直接删除或添加
< div id = " app" >
< input type = " button" value = " 切换v-show值" @click = " change" />
< p v-if = " info" >
sssssssssssssssssssssssssssssssssdadasdadadsadw
</ p>
< p>
< img v-if = " info" src = " 1.png" style = " width : 400px; height : 200px; " />
</ p>
< p>
与v-shwo的对比
</ p>
</ p>
< p>
< img v-show = " info" src = " 1.png" style = " width : 400px; height : 200px; " />
</ p>
</ div>
< script src = " https://cdn.jsdelivr.net/npm/vue/dist/vue.js" > </ script>
< script>
var app = new Vue ( {
el : "#app" ,
data : {
info : true
} ,
methods : {
change : function ( ) {
this . info= ! this . info;
}
}
} ) ;
</ script>
v-bind,简写为:,语法为v-bind:属性=“”,简写为:属性=“”,操作的是控件的属性,如src,class,
< html>
< head>
< title> V-bind</ title>
< style>
.active {
border : 1px solid red;
}
</ style>
</ head>
< body>
< div id = " app" >
< p>
< img v-bind: src= " info" />
</ p>
< p>
< img :src = " info" />
</ p>
< p>
< img :src = " info" :title = " imgtitle+'!!!'" />
</ p>
< p>
< img :src = " info" @click = " change" :class = " isactive?'active':''" />
</ p>
< p>
< img :src = " info" @click = " change" :class = " {active:isactive}" />
</ p>
</ div>
< script src = " https://cdn.jsdelivr.net/npm/vue/dist/vue.js" > </ script>
< script>
var app = new Vue ( {
el : "#app" ,
data : {
info : "1.png" ,
isactive : false ,
imgtitle : "百度一下"
} ,
methods : {
change : function ( ) {
this . isactive= ! this . isactive;
}
}
} ) ;
</ script>
</ body>
</ html>
v-for,循环遍历数组,集合,语法为v-for=“item in 数组名"或者v-for=”(item,index) in 数组名",类似于键值对
< div id = " app" >
< ul>
< li v-for = " item in arr" > {{item}}</ li>
</ ul>
< ul>
< li v-for = " (item,index) in arr1" > {{index}}---{{item}}</ li>
</ ul>
< select>
< option v-for = " item in arr2" :value = " item.value" :selected = " item.selected" > {{item.text}}</ option>
</ select>
</ div>
< script src = " https://cdn.jsdelivr.net/npm/vue/dist/vue.js" > </ script>
< script>
var app = new Vue ( {
el : "#app" ,
data : {
arr : [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] ,
arr1 : [ "上海" , "北京" , "广州" , "深圳" , "杭州" ] ,
arr2 : [ { value : "1" , text : "上海" , selected : false } , { value : "2" , text : "北京" , selected : true } , { value : "1" , text : "深圳" , selected : false } ]
}
} ) ;
</ script>
v-model,数据双向绑定,常和表单标签一起使用
< div id = " app" >
< p> 请输入您的名字:< input type = " text" v-model = " name" /> </ p>
< p> 请输入您的密码:< input type = " password" v-model = " pwd" /> </ p>
< h2>
{{name}}
</ h2>
< h2>
{{pwd}}
</ h2>
</ div>
< script src = " https://cdn.jsdelivr.net/npm/vue/dist/vue.js" > </ script>
< script>
var app = new Vue ( {
el : "#app" ,
data : {
name : "你是姐姐还是哥哥?" ,
pwd : "123456" ,
}
} ) ;
</ script>