vue基础

1.生命周期

事务

2.基础语法

  • 插值操作

        mustache语法{{}}:不仅仅可以直接写变量,也可以写简单的表达式

  • v-once

        该指令表示元素和组件只渲染一次,不会随着数据的改变而改变。

  • v-html

        以html格式进行解析。

  • v-text

        以text格式进行解析。

  • v-pre

  跳过这个元素和他子元素的编译过程,显示原本的mustache语法。

  • v-cloak

  防止new vue还没执行的时候,页面出现{{xx}}

 在vue解析之前,有属性v-cloak;解析之后,没有属性v-cloak

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			[v-cloak]{
				display: none;
			}
		</style>
	</head>
	<body>
		<div id="app">
			<h4 v-cloak>{{url}}</h4>
		</div>
		<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
		<script type="text/javascript">
			var app = new Vue({
				el:'#app',
				data:{
					url:'====',
				},
				methods:{
					// add(){
						
					// }
				}
			})
		</script>
	</body>
</html>

3.绑定属性

4.class(对象语法、数组语法),style

  • 对象语法
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			.normal{
				font-size: 32px;
				font-weight: bold;
			}
			.red{
				color: red;
			}
			.line{
				text-decoration: line-through;
			}
		</style>
	</head>
	<body>
		<div id="app">
			<p class="normal" :class="{red:isred,line:isline}">直接绑定class</p>
			<p class="normal" :class="getclass()">通过methods的getclass时间返回class对象</p>
			<button type="button" @click="change">点击</button>
		</div>
		<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
		<script type="text/javascript">
			var app = new Vue({
				el:'#app',
				data:{
					isred:true,
					isline:true
				},
				methods:{
					change:function(){
						this.isred = !this.isred;
						this.isline = !this.isline
					},
					getclass:function(){
						return {red:this.isred,line:this.isline}
					}
				}
			})
		</script>
	</body>
</html>

点击前和点击后

  • 数组语法

注意:

  • {key:value}里,value加单引号解析为字符串,不加解析为变量
  • 写css属性名,font-size写成,fontSize或者font-size"

5.计算属性(computed)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			.normal{
				font-size: 32px;
				font-weight: bold;
			}
			.red{
				color: red;
			}
			.line{
				text-decoration: line-through;
			}
		</style>
	</head>
	<body>
		<div id="app">
			<p>{{firstname}}{{lastname}}</p>   <!-- 张三丰 -->
			<p>{{fullname}}</p>                <!-- 张三丰 -->
			<p>总价:{{sumprice}}</p>		   <!-- 总价:168 -->
		</div>
		<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
		<script type="text/javascript">
			var app = new Vue({
				el:'#app',
				data:{
					firstname:'张',
					lastname:'三丰',
					books:[
						{name:"js入门到精通",price:33},
						{name:"深入理解计算机原理",price:35},
						{name:"ui设计黄金法则",price:100},
					]
				},
				computed:{
					fullname:function(){
						return this.firstname+this.lastname
					},
					sumprice:function(){
						let result = 0;
						for(let i = 0;i < this.books.length;i++){
							result = this.books[i].price + result
						}
						return result
					}
				},
				methods:{
					
				}
			})
		</script>
	</body>
</html>

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值