Vue基础语法
一、挂载点,模版和实例
二、Vue实例中的数据,事件和方法
1、v-html指令和v-text指令
v-html :不转义
v-text :转义过后的内容
<div id="root">
<div v-html="content"></div>
<div v-text="content"></div>
</div>
<script>
new Vue({
el:"#root",
data:{
content:"<h1>hello</h1>"
}
})
</script>
2、v-on指令
<div v-on:click="()=>{alert(123)}">
{{content}}
</div>

正确做法:
<div v-on:click="handleClick">
{{content}}
</div>
</div>
<script>
new Vue({
el:"#root",
data:{
content:"hello",
},
methods:{
handleClick:function(){
alert(123);
}
}
})
</script>
<div id="root">
<div v-on:click="handleClick">
{{content}}
</div>
</div>
<script>
new Vue({
el:"#root",
data:{
content:"hello",
},
methods:{
handleClick:function(){
this.content="world" //面向数据编程
}
}
})
</script>
三、Vue中的属性绑定和双向数据绑定
1、属性绑定v-bind:title简写:bind
<div id="root">
<div v-bind:title="title">helloworld</div>
<div :title="title">缩写</div>
</div>
<script>
new Vue({
el:"#root",
data:{
title:"this is hello world"
}
})
</script>
2、双向数据绑定v-model
<div id="root">
<div>{{content}}</div>
<input type="text" v-model="content">
</div>
<script>
new Vue({
el:"#root",
data:{
content:"this is content"
}
})
</script>
四、Vue中的计算属性和侦听器
1、计算属性 computed
和react中的reselect特别像
好处:firstName,lastName都没改变,fullName会取上一次的缓存值,性能高。
<div id="root">
姓:<input type="text" v-model="lastName">
名:<input type="text" v-model="firstName">
<div>{{firstName}}{{lastName}}</div>
<div>{{fullName}}</div>
</div>
<script>
new Vue({
el:"#root",
data:{
firstName:'starof',
lastName:'liu'
},
computed:{
fullName:function(){
return this.firstName+this.lastName;
}
}
})
</script>
2、侦听器 watch
监听数据的变化
监听fistName和lastName,每次变化加一。
<div id="root">
姓:<input type="text" v-model="lastName">
名:<input type="text" v-model="firstName">
<div>{{firstName}}{{lastName}}</div>
FullName: <span>{{fullName}}</span>
<div>{{count}}</div>
</div>
<script>
new Vue({
el:"#root",
data:{
firstName:'starof',
lastName:'liu',
count:0
},
computed:{
fullName:function(){
return this.firstName+this.lastName;
}
},
watch:{
firstName:function(){
this.count++
},
lastName:function(){
this.count++
}
}
})
</script>
监听计算属性的改变
new Vue({
el:"#root",
data:{
firstName:'starof',
lastName:'liu',
count:0
},
computed:{
fullName:function(){
return this.firstName+this.lastName;
}
},
watch:{
fullName:function(){
this.count++
}
}
})
五、v-if、v-show和v-for指令
1、v-if
值为false直接从DOM中移除。
<div id="root">
<div v-if="showHello">hello world</div>
<button @click="handleToogle">toogle</button>
</div>
<script>
new Vue({
el:"#root",
data:{
showHello:true
},
methods:{
handleToogle:function(){
this.showHello=!this.showHello;
}
}
})
</script>
2、v-show
处理上例这种频繁显示隐藏使用v-show更好。
<div id="root">
<div v-show="showHello">hello world</div>
<button @click="handleToogle">toogle</button>
</div>
<script>
new Vue({
el:"#root",
data:{
showHello:true
},
methods:{
handleToogle:function(){
this.showHello=!this.showHello;
}
}
})
</script>
3、v-for
<div id="root">
<ul>
<li v-for="item of list">{{item}}</li>
</ul>
</div>
<script>
new Vue({
el:"#root",
data:{
list:[1,2,3]
}
})
</script>
循环时候使用:key可以提高效率。key值不能重复。
<li v-for="item of list" :key="item">{{item}}</li>
可以这么写:
<div id="root">
<ul>
<li v-for="(item,index) of list" :key="index">{{item}}</li>
</ul>
</div>
<script>
new Vue({
el:"#root",
data:{
list:[1,2,2,3]
}
})
</script>
但是频繁对列表进行变更,排序等操作时,index作为key值是有问题的。
本文作者starof,因知识本身在变化,作者也在不断学习成长,文章内容也不定时更新,为避免误导读者,方便追根溯源,请诸位转载注明出处:http://www.cnblogs.com/starof/p/9061617.html 有问题欢迎与我讨论,共同进步。
Vue基础语法的更多相关文章
- python 全栈开发,Day89(sorted面试题,Pycharm配置支持vue语法,Vue基础语法,小清单练习)
一.sorted面试题 面试题: [11, 33, 4, 2, 11, 4, 9, 2] 去重并保持原来的顺序 答案1: list1 = [11, 33, 4, 2, 11, 4, 9, 2] ret ...
- 2-5 vue基础语法
一.vue基础语法 语法: {{msg}} html赋值: v-html="" 绑定属性: v-bind:id="" 使用表达式: {{ok? "ye ...
- 一、vue基础语法(轻松入门vue)
轻松入门vue系列 Vue基础语法 一.HelloWord 二.MVVM设计思想 三.指令 1. v-cloak 2. v-text 3. v-html 4. v-show 4. v-pre 5. v ...
- Vue基础语法-数据绑定、事件处理和扩展组件等知识详解(案例分析,简单易懂,附源码)
前言: 本篇文章主要讲解了Vue实例对象的创建.常用内置指令的使用.自定义组件的创建.生命周期(钩子函数)等.以及个人的心得体会,汇集成本篇文章,作为自己对Vue基础知识入门级的总结与笔记. 其中介绍 ...
- Vue 1-- ES6 快速入门、vue的基本语法、vue应用示例,vue基础语法
一.ES6快速入门 let和const let ES6新增了let命令,用于声明变量.其用法类似var,但是声明的变量只在let命令所在的代码块内有效. { let x = 10; var y = 2 ...
- Vue(1)- es6的语法、vue的基本语法、vue应用示例,vue基础语法
一.es6的语法 1.let与var的区别 ES6 新增了let命令,用来声明变量.它的用法类似于var(ES5),但是所声明的变量,只在let命令所在的代码块内有效.如下代码: { let a = ...
- 一、vue基础--语法
用到的前台编程工具是Visual Studio Code,暂时是官网下载vue.js到本地使用 一.Visual Studio Code需要安装的插件: jshint :js代码规范检查 Beau ...
- Vue 基础语法入门(转载)
使用vue.js原文介绍:Vue.js是一个构建数据驱动的web界面库.Vue.js的目标是通过尽可能简单的API实现响应式数据绑定和组合的视图组件.vue.js上手非常简单,先看看几个例子: 例一: ...
- 2. Vue基础语法
模板语法: Mustache语法: {{}} Html赋值: v-html="" 绑定属性: v-bind:id="" 使用表达式: {{ok?'Yes': ...
- Vue基础语法与指令
项目初始化 用vscode打开终端,输入npm init -y生成package.json 然后安装vue npm install vue 需要注意的是,我遇到了这个问题 出现原因:文件夹名和生成的p ...
随机推荐
- 利用window.performance.timing进行性能分析
性能分析... window.performance.timing中相关属性语义: // .navigationStart 准备加载页面的起始时间 // .unloadEventStart 如果前一个 ...
- django rest framework serializers序列化
serializers是将复杂的数据结构变成json或者xml这个格式的 serializers有以下几个作用: - 将queryset与model实例等进行序列化,转化成json格式,返回给用户(a ...
- 最大公约数和最小公倍数(Greatest Common Divisor and Least Common Multiple)
定义: 最大公约数(英语:greatest common divisor,gcd).是数学词汇,指能够整除多个整数的最大正整数.而多个整数不能都为零.例如8和12的最大公因数为4. 最小公倍数是数论中 ...
- Linux基本命令总结(六)
接上篇: 27,diff在命令行中打印每一个行的改动.最新版本的diff还支持二进制文件.diff程序的输出被称为补丁 (patch),因为Linux系统中还有一个patch程序,可以根据diff的输 ...
- 关于进程、线程、应用程序域(AppDomain)
什么是进程?什么是线程?什么是应用程序集?及三者之间的关系(以下做一些简单的概念性的理解) 三者共同的基础对象!!!——应用程序 1.进程是操作系统用于隔离众多正在运行的应用程序的机制,进程的缺点是降 ...
- [报错]java.lang.ClassCastException
Caused by: java.lang.ClassCastException: org.apache.xml.dtm.ref.DTMManagerDefault cannot be cast to ...
- 2017-12-19python全栈9期第四天第一节之昨日内容回顾与作业讲解之插入insert和extend
#!/user/bin/python# -*- coding:utf-8 -*-li = ['zs','ls','ww','zl']li.insert(4,'cc')print(li)li.exten ...
- busybox(四)完善
目录 busybox(四)完善 proc挂载 手动挂载 proc解析 使用脚本自动挂载 使用mount-a挂载 udev/mdev 挂载 使用jffs2 文件系统格式 安装zlib 安装jffs2 生 ...
- CMDB服务器管理系统【s5day89】:采集资产之整合资产
1.业务逻辑单独写 1.代码目录结构 2.client.py from src.plugins import PluginManager class BaseClient(object): def p ...
- kubernetes云平台管理实战: 集群部署(一)
一.环境规划 1.架构拓扑图 2.主机规划 3.软件版本 [root@k8s-master ~]# cat /etc/redhat-release CentOS Linux release 7.4.1 ...