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 ...
随机推荐
- MD 的常用语法格式
参考资料:MarkDown 语言常用语法 注意:vscode 中,可以使用 ctrl + shift + v 进行预览: 一.标题 一般使用 # 来进行层级标识.共 6 个层级,再多不识别. # = ...
- 关于计时器的js函数
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- ☆ [HNOI2012] 永无乡 「平衡树启发式合并」
题目类型:平衡树启发式合并 传送门:>Here< 题意:节点可以连边(不能断边),询问任意两个节点的连通性与一个连通块中排名第\(k\)的节点 解题思路 如果不需要询问排名,那么并查集即可 ...
- nginx编译安装指定参数
--prefix=/app/nginx # 安装目录 --conf-path=/app/nginx/conf/nginx.conf # 配置文件 --sbin-path=/app/nginx/sbin ...
- Ubuntu 服务器上面--安装和配置mysql 【转】
更新源列表 打开"终端窗口",输入"sudo apt-get update"-->回车-->"输入root用户的密码"--> ...
- 第四章:条件语句(if)和循环结构(while)
1.流程控制 含义与作用 Python程序执行,一定按照某种规律在执行 a.宏观一定是自上而下(逻辑上方代码一定比逻辑下方代码先执行):顺序结构b.遇到需要条件判断选择不同执行路线的执行方式:分支结构 ...
- 使用WebClient进行文件上传
注释部分为异步上传,几行代码就能搞定 public static bool Upload(string url, string path) { using (WebClient client = ne ...
- 第十五节:HttpContext五大核心对象的使用(Request、Response、Application、Server、Session)
一. 基本认识 1. 简介:HttpContext用于保持单个用户.单个请求的数据,并且数据只在该请求期间保持: 也可以用于保持需要在不同的HttpModules和HttpHandlers之间传递的值 ...
- MySQL无法插入中文的解决方案
本人在做数据库的连接过程中,发现无法插入中文值.原因是mysql的默认编码是latin1,只须将编码改为utf8即可. 在mysql的命令行窗口中输入 status 会出现当前的编码.在mysql的安 ...
- SpringBoot系列: 所有配置属性和官方文档
Spring Boot 通用配置参数https://docs.spring.io/spring-boot/docs/current/reference/html/common-application- ...