vue基本语法
vue基本语法
v-bind
<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--导入vue.js-->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
<!-- view层 模板-->
<div id="app">
<span v-bind:title="message">
鼠标悬停查看此处绑定信息
</span>
</div>
<script>
var vm=new Vue({
el:"#app",
//model:数据
data:{
message:"hello,this is my first vue!"
}
});
</script>
</body>
</html>
v-if/v-else
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
<div id="app">
<h1 v-if="ok">Yes</h1>
<h1 v-else>NO</h1>
</div>
<script>
var vm=new Vue({
el:"#app",
data:{
ok : true
}
});
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
<div id="app">
<h1 v-if="type==='A'">A</h1>
<h1 v-else-if="type==='B'">B</h1>
<h1 v-else-if="type==='C'">C</h1>
<h1 v-else>D</h1>
</div>
<script>
var vm=new Vue({
el:"#app",
data:{
type:'A'
}
});
</script>
</body>
</html>
v-for
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
<div id="app">
<li v-for="item in items">
{{item.message}}
</li>
</div>
<script>
var vm = new Vue({
el:"#app",
data:{
items:[
{message:"yuan"},
{message: "yu"}
]
}
});
</script>
</body>
</html>
v-on:绑定事件
<!DOCTYPE html>
<html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
<div id="app">
<button v-on:click="sayHi">click me</button>
</div>
<script>
var vm = new Vue({
el:"#app",
data:{
message:"yuanyu"
},
methods:{
//方法必须定义在Vue的methods对象中,通过v-on绑定事件
sayHi:function () {
alert(this.message);
}
}
});
</script>
</body>
</html>
v-model:双向绑定
<!DOCTYPE html>
<html lang="en" xmlns:v-model="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
<div id="app">
要输入的文本<input type="text" v-model="message"> {{message}}
</div>
<script>
var vm=new Vue({
el:"#app",
data:{
message:"123"
},
methods:{}
});
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:v-model="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
<div id="app">
<p>
<!--要输入的文本<input type="text" v-model="message"> {{message}}-->
<textarea name="" id="" cols="30" rows="10" v-model="message"></textarea>
{{message}}
</p>
<br>
<p>
性别:
<input type="radio" name="sex" value="男" v-model="yuanyu">男
<input type="radio" name="sex" value="女" v-model="yuanyu">女
</p>
<p>
输入的性别是:{{yuanyu}}
</p>
<p>
下拉框:
<select name="" id=" " v-model="selected">
<option value=" "diasbled>--请选择--</option>
<option>A</option>
<!-- <option selected>B</option> <!–selected表示默认选中–>-->
<option>B</option>
<option>C</option>
</select>
被选中的是:{{selected}}
</p>
</div>
<script>
var vm=new Vue({
el:"#app",
data:{
message:"123",
yuanyu:"",
selected:" "
},
methods:{}
});
</script>
</body>
</html>
vue组件(自定义标签)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
<div id="app">
<yuanyu></yuanyu>
</div>
<script>
//定义一个vue组件component
Vue.component("yuanyu",{
template:" <li>Hello!</li>"
});
var vm=new Vue({
el:"#app",
data:{
items:[]
},
methods:{}
});
</script>
</body>
</html>
在视图层通过a对item进行绑定,在组件中加入props:[" "],否则没有结果显示
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
<div id="app">
<yuanyu v-for="item in items" v-bind:a="item"></yuanyu>
<!-- item为遍历属性,a为绑定属性-->
</div>
<script>
//定义一个vue组件component(注册组件)
Vue.component("yuanyu",{
props:["a"], //参数
template:" <li>{{a}}</li>" //模板
});
var vm=new Vue({
el:"#app",
data:{
items:["yuan","yu"]
},
methods:{}
});
</script>
</body>
</html>