2019-07-07
1. v-model ##这是双向绑定的事件,例如:
<input type="text" v-model="inputValue">
2. v-on:click ##这是点击事件,例如:(v-on:click 可简写成@click)
<button v-on:click="handleBtnClick">提交</button>
3. v-for ##这是循环函数
<ul>
<li v-for="item in list">{{item}}</li>
</ul>
4. v-bind ##父组件向子组件传值, (v-bind 可简写成 : ,例如: :content, :index, :item)
<todo-item v-bind:content="item"></todo-item>
Vue.component("TodoItem",{
props: ['content'],
template: "<li>{{content}}</li>"
})
5. 创建全局组件
Vue.component("TodoItem",{
props: ['content'],
template: "<li>{{content}}</li>"
})
6. 创建局部组件
var TodoItem = {
props: ['content'],
template: "<li>{{content}}</li>"
}
var app = new Vue({
components: {
TodoItem: TodoItem,
}
})
7. 数组也可以是一个对象
<div id="app-4">
<ol>
<li v-for="todo in todos">
{{ todo.text }}
</li>
</ol>
</div>
var app4 = new Vue({
el: '#app-4',
data: {
todos: [
{ text: '学习 JavaScript' },
{ text: '学习 Vue' },
{ text: '整个牛项目' }
]
}
})
8. v-text ##往元素里面添加text文本
<div id="app" v-text="name"></div>
var vm = new Vue({
el: '#app',
data: {
name: 'hello'
}
})
9. v-html ##往元素里面添加html内容
<div id="app" v-html="name"></div>
var vm = new Vue({
el: '#app',
data: {
name: '<h1>hello<h1>'
}
})
10. 计算属性 (有缓存机制)
<div id="app">
{{fullName()}}
</div>
var vm = new Vue({
el: '#app',
data: {
firstName: "one",
lastName: "two"
},
computed: {
fullName: function() {
return this.firstName + " " + this.lastName
}
}
})
11. 方法 (无缓存机制)
<div id="app">
{{fullName()}}
</div>
var vm = new Vue({
el: '#app',
data: {
firstName: "one",
lastName: "two"
},
methods: {
fullName: function() {
return this.firstName + " " + this.lastName
}
}
})
12. 侦听器 (有缓存机制)与computed计算属性类似
<div id="app">
{{fullName()}}
</div>
var vm = new Vue({
el: '#app',
data: {
firstName: "one",
lastName: "two"
},
watch: {
firstName: function() {
this.fullName = this.firstName + " " + this.lastName
},
lastName: function() {
this.fullName = this.firstName + " " + this.lastName
}
}
})
13. 计算属性的get和set ##可以进行额外的操作
<div id="app">
{{fullName}}
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
firstName: "one",
lastName: "two"
},
computed: {
fullName: {
get: function() {
return this.firstName + " " + this.lastName
},
set: function(value) {
var arr = value.split(" ");
this.firstName = arr[0];
this.lastName = arr[1];
}
}
}
})
</script>
14. class的对象绑定
<style type="text/css">
.activated {
color: blue
}
</style>
</head>
<body>
<div id="app">
<div @click="handleDivClick"
:class="{activated: isActivated}"
>
hello
</div>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
isActivated: false
},
methods: {
handleDivClick: function() {
this.isActivated = !this.isActivated;
}
}
})
</script>
</body>
15. class的数组绑定
<div id="app">
<div @click="handleDivClick"
:class="[activated]"
>
hello
</div>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
activated: ""
},
methods: {
handleDivClick: function() {
this.activated = this.activated === "activated" ? "" : "activated"
}
}
})
</script>
16. class的数组对象绑定
<div id="app">
<div :style="[styleObj, {fontSize: '20px'}]"
@click="handleDivClick"
>
hello
</div>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
styleObj: {
color: "red"
}
},
methods: {
handleDivClick: function() {
this.styleObj.color = this.styleObj.color === "red" ? "black" : "red";
}
}
})
</script>
17. v-if 与 v-show的对比(都是决定是否显示元素)
<div id="app">
<div v-if="show">{{message}}</div>
<div v-show="show">{{message}}</div>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
message: "hhhh",
show: false
}
})
</script>
18. v-if 与 v-else (需要紧贴着使用,中间不能加其他元素)
<div id="app">
<div v-if="show">{{message}}</div>
<div v-else>Bye World</div>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
message: "hhhh",
show: false
}
})
</script>
19. v-if 与 v-else-if 与 v-else
<div id="app">
<div v-if="show === 'a'">{{message}}</div>
<div v-else-if="show === 'b'">ZeZeZe</div>
<div v-else>Bye World</div>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
message: "hhhh",
show: 'a'
}
})
</script>
20. Vue的小bug例子
<div id="app">
<div v-if="show">
用户名: <input key="username" />
</div>
<div v-else>
邮箱: <input key="email" />
</div>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
message: "hhhh",
show: true
}
})
</script>