Vue
1. Vue 基础
1. 声明式渲染
1. v-bind
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<title>vue123</title>
</head>
<body>
<div id="app">
{{ message }}
</div>
<!-- built files will be auto injected -->
<script src="index.js"></script>
</body>
</html>
var app = new Vue({
el:'#app',
data:{
message:'HelloWorld'
}
});
2.条件与循环
v-if
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
<div id="app">
<p v-if="seen">Now you see</p>
</div>
<!-- built files will be auto injected -->
<script>
var app = new Vue({
el: '#app',
data: {
seen :false
}
});
app.seen = false
</script>
</body>
</html>
v-for
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<title>Title</title>
</head>
<body>
<div id="app">
<ol>
<li v-for="todo in todos">
{{ todo.text }}
</li>
</ol>
</div>
<script>
var app = new Vue({
el:'#app',
data:{
todos:[
{text:'learning js'},
{text: 'learning Vue'},
{text: 'learning 整个项目'}
]
}
})
app.todos.push({text:'新项目'})
</script>
</body>
</html>
3.处理用户输入
<!DOCTYPE html>
<html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="qpp5">
<p>{{ message }}</p>
<button v-on:click="reverseMess">逆转消息
</button>
</div>
<script>
var app5 = new Vue({
el: '#qpp5',
data:{
message:'Hello Vue.js'
},
methods:{
reverseMess:function () {
this.message = this.message.split('').reverse().join('')
}
}
})
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="qpp5">
<p>{{ message }}</p>
<input v-model="message">
</div>
<script>
var app5 = new Vue({
el: '#qpp5',
data:{
message:'Hello Vue.js'
},
methods:{
reverseMess:function () {
this.message = this.message.split('').reverse().join('')
}
}
})
</script>
</body>
</html>
4.组件化应用构建
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<ol>
<todo-item></todo-item>
<todo-item></todo-item>
<todo-item></todo-item>
</ol>
</div>
<script>
Vue.component('todo-item',{
template:'<li>这是一个待办事项</li>'
})
var app = new Vue({
el:'#app'
})
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<ol>
<todo-item
v-for="item in grocerylist"
v-bind:todo="item"
v-bind:key="item.id"></todo-item>
</ol>
</div>
<script>
Vue.component('todo-item', {
// todo-item 组件现在接受一个
props: ['todo'],
template: '<li>{{ todo.text }}</li>'
})
var app = new Vue({
el:'#app',
data:{
grocerylist:[
{id:0,text:"大白梨"},
{id:1,text:"巧克力"},
{id:2,text:"剩下什么都可以"}
]
}
})
</script>
</body>
</html>