概述
一个前端框架,用于将数据渲染到页面展示
开发步骤
代码实现
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue入门案例title>
head>
<body>
<div id="app">
{{msg}}
div>
body>
<script src="js/vue.js">script>
<script>
var vue = new Vue({
el: "#app",
data: {
msg: "helloworld"
}
});
script>
html>
语法
概述
常用指令
概述
v-html
v-text
代码实现
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue文本操作title>
head>
<body>
<div id="app">
<span v-html="msg">span><br>
<span v-text="msg">span><br>
<span>{{msg}}span><br>
div>
body>
<script src="js/vue.js">script>
<script>
var vue = new Vue({
el: "#app",
data: {
msg: "hello world"
}
})
script>
html>
概述
代码实现
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue事件处理title>
head>
<body>
<div id="app">
<button v-on:click="fn1()">按钮1button>
<button @click="fn2()">按钮2button>
<input type="text" @change="fn3()">
div>
body>
<script src="js/vue.js">script>
<script>
var vue = new Vue({
el: "#app",
data: {},
methods: {
fn1(){
console.log("fn1...");
},
fn2(){
console.log("fn2...");
},
fn3(){
console.log("fn3...");
}
}
})
script>
html>
概述
.prevent :禁止组件原有的事件触发
代码实现
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue事件修饰符title>
head>
<body>
<div id="app">
<div @click="fn1()">
<button @click.stop="fn2()">按钮button>
div>
<a href="index.html" @click.prevent="fn3()">超链接a>
div>
body>
<script src="js/vue.js">script>
<script>
var vue = new Vue({
el: "#app",
data: {},
methods: {
fn1(){
console.log("div点击了~~~~");
},
fn2(){
console.log("button点击了~~~~");
},
fn3(){
console.log("a点击了~~~~");
}
}
})
script>
html>
概述
使用v-model指令来达到双向绑定效果
单向绑定
数据,只可以从vue对象到vue容器中
双向绑定
需求
代码实现
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue双向绑定title>
head>
<body>
<div id="app">
<input type="text" v-model="content"><br>
<span v-text="content">span>
div>
body>
<script src="js/vue.js">script>
<script>
var vue = new Vue({
el: "#app",
data: {
content: "aaa"
}
})
script>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue双向绑定title>
head>
<body>
<div id="app">
爱好:
<input type="checkbox" value="lol" v-model="hobbys">lol
<input type="checkbox" value="king" v-model="hobbys">王者荣耀
<input type="checkbox" value="java" v-model="hobbys">java
<input type="checkbox" value="basketball" v-model="hobbys">篮球
<br>
<span v-text="hobbys">span>
div>
body>
<script src="js/vue.js">script>
<script>
var vue = new Vue({
el: "#app",
data: {
hobbys:[
"java"
]
}
})
script>
html>
概述
语法
-
代码实现
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue列表渲染title>
head>
<body>
<div id="app">
<ul>
<li v-for="(user,index) in userList">
{{index}}
<span v-text="user.id">span>
<span v-text="user.name">span>
<span v-text="user.age">span>
li>
ul>
div>
body>
<script src="js/vue.js">script>
<script >
var vue = new Vue({
el:"#app",
data:{
userList : [
{
id : 1,
name : "张三",
age : 18
},
{
id : 2,
name : "李四",
age : 19
}
]
}
})
script>
html>