v-if不会渲染到页面,v-show显示隐藏,会渲染到页面上,使用SSR时需要用v-show,v-if可能报错:
[Vue warn]: The client-side rendered virtual DOM tree is not matching server-rendered content. This is likely caused by incorrect HTML markup, for example nesting block-level elements inside <p>, or missing <tbody>. Bailing hydration and performing full client-side render
1
# 类名与样式绑定、计算属性
v-for后面需要加上 :key 值为字符串或数字类型
<div id="app">
</div>
1
2
3
2
3
var app = new Vue({
el: '#app',
data: {
message: '页面加载于 ' + new Date(),
seen:true
}
})
<template>
<div>
<p v-bind:style="msgstyle">{{ message }}</p>
<span v-bind:title="message">
鼠标悬停几秒钟查看此处动态绑定的提示信息!
</span>
<p v-if="seen">现在你看到我了</p>
<p v-else>现在你看不到我了</p>
<p v-show="seen">现在你看到我了</p>
<img :src="image">
<p v-bind:class="{ 'primary': true }" v-on:click="getimage">获取图片</p>
<i :class='img'></i>
<ul>
<li v-for='item in searchData' :key="item.id"><i :class="(item.id==0)?'yidong':(item.id==1)?'dianxin':'liantong'"></i>{{item.name}}</li>
</ul>
</div>
</template>
<script>
export default{
data(){
return{
message: '页面加载于 ' + new Date(),
search:'', // 过滤关键字
image:'', // 图片地址
seen:true,
allShops:[{
name:'Jhon',
id:0
},{
name:'Shims',
id:1
},{
name:'Jim',
id:2
}],
img:'',
msgstyle:{
color:'red',
lineHeight:'100px',
fontSize:'30px'
}
}
},
computed:{
searchData:function(){
var search = this.search;
if (search) {
return this.allShops.filter(function(shop) {
return Object.keys(shop).some(function(key) {
return String(shop[key]).indexOf(search) > -1
})
})
}
return this.allShops;
}
},
created () {
this.getimage();
},
methods:{
getimage(){
var that = this;
this.$http.get('/api')
.then(res){
that.image = res.data.image
that.img = 'liantong';
that.search = 'J';
}
}
}
}
</script>
<style>
.primary{
color:#red
}
.dianxin{
background-image:url(../images/dianxin.png)
}
.liantong{
background-image:url(../images/liantong.png)
}
.yidong{
background-image:url(../images/yidong.png)
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94