单向数据绑定v-bind
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="vue.min.js"></script>
<div id="app">
{{message}}
<h1 v-bind:title="message">
{{content}}
</h1>
</div>
<script>
new Vue({
el: '#app',
data: {
content: '我是标题',
message: '页面加载于' + new Date().toLocaleString()
}
})
</script>
</body>
</html>
vue事件操作
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="vue.min.js"></script>
<div id="app">
<button v-on:click="search()">查询1</button>
<button @click="search()">查询2</button>
</div>
<script>
new Vue({
el: '#app',
data: {
searchMap: {
keyword: 'shang gui'
},
result: {
}
},
methods: {
search() {
console.log('search...')
}
}
})
</script>
</body>
</html>
条件指令
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<input type="checkbox" v-model="ok" />是否同意
<h1 v-if="ok">this is v-if</h1>
<h1 v-else>this is v-else</h1>
</div>
<script src="vue.min.js"></script>
<script>
new Vue({
el: '#app',
data: {
ok: true
}
})
</script>
</body>
</html>
v-for
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<ul>
<li v-for="n in 10">
{{n + 100}}
</li>
</ul>
<hr />
<table>
<tr v-for="user in users">
<td>{{user.id}}</td>
<td>{{user.name}}</td>
</tr>
</table>
</div>
<script src="vue.min.js"></script>
<script>
new Vue({
el: '#app',
data: {
users: [
{ id: 1, name: 'zs' },
{ id: 2, name: 'zs2' },
{ id: 3, name: 'z3' },
{ id: 4, name: 'zs4' },
{ id: 5, name: 'zs5' }
]
}
})
</script>
</body>
</html>
组件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<Navbar></Navbar>
</div>
<script src="vue.min.js"></script>
<script>
new Vue({
el: '#app',
components: {
'Navbar': {
template: '<ul><li>首页</li><li>学员管理</li></ul>'
}
}
})
</script>
</body>
</html>
路由
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<h1>Hello App!</h1>
<p>
<router-link to="/">首页</router-link>
<router-link to="/student">会员管理</router-link>
<router-link to="/teacher">讲师管理</router-link>
</p>
<router-view></router-view>
</div>
<script src="vue.min.js"></script>
<script src="vue-router.min.js"></script>
<script>
const Welcome = { template: '<div>欢迎</div>' }
const Student = { template: '<div>student list</div>' }
const Teacher = { template: '<div>teacher list</div>' }
const routes = [
{ path: '/', redirect: '/welcome' },
{ path: '/welcome', component: Welcome },
{ path: '/student', component: Student },
{ path: '/teacher', component: Teacher }
]
const router = new VueRouter({
routes
})
const app = new Vue({
el: '#app',
router
})
</script>
</body>
</html>
axios
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="vue.min.js"></script>
<script src="axios.min.js"></script>
<div id="app">
<table border="1">
<tr v-for="item in teachers">
<td>{{item.id}}</td>
<td>{{item.name}}</td>
<td>{{item.intro}}</td>
<td>{{item.career}}</td>
<td>{{item.level}}</td>
<td>{{item.sort}}</td>
<td>{{item.isDeleted}}</td>
<td>{{item.gmtCreate}}</td>
<td>{{item.gmtModified}}</td>
</tr>
</table>
</div>
<script>
new Vue({
el: '#app',
data: {
teachers: []
},
created() {
this.getTeachers()
},
methods: {
getTeachers() {
axios.get("data.json")
.then(response => {
this.teachers = response.data.data.items
console.log(this.teachers)
})
.catch(error => {
})
}
}
})
</script>
</body>
</html>
|