原文地址:http://www.jianshu.com/p/293387d240b2

Hello World

<div id="app">{{ message }}<button v-on:click="clickMe()">点击</button> <button v-on:click="clickMe">无参数的简写</button> </div>
new Vue({el: '#app', data: { message: 'Hello Vue.js!' }, methods: { clickMe: function(){} } });

指令

循环

循环数组

<!-- Vue 1 这么写 -->
<li v-for="item in items"> 第{{ $index }}条:{{ item.message }}</li> <div v-for="item in items" track-by="id"> <!-- Vue 2 这么写 --> <li v-for="(item, index) in items"> 第{{ index }}条:{{ item.message }}</li> <div v-for="item in items" v-bind:key="item.id">

循环对象

<!-- Vue 1 这么写 -->
<li v-for="(key, value) in obj"></li> <!-- Vue 2 这么写 --> <li v-for="(value, key) in obj"></li>

循环数字

<span v-for="n in 10">{{ n }} </span> <!-- Vue 1 从0开始,Vue 2从1开始 -->

条件

<!-- 如果ok为false, 不输出在 HTML 中 -->
<div v-if="ok">Yes</div> <div v-else>No</div> <!-- 如果ok为false,只是 display:none 而已 --> <h1 v-show="ok">Hello!</h1>

事件绑定

<button v-on:click="say('hi')">点击</button> <!-- 简写 --> <button @click="say('hi')">点击</button> <!-- 传入 event 对象 --> <button @click="say('hi', $event)">点击</button> <!-- 阻止单击事件冒泡 --> <button @click.stop="doSth">点击</button> <!-- 阻止默认行为 --> <button @submit.prevent="doSth">点击</button> <!-- 修饰符可以串联 --> <a @click.stop.prevent="doThat"></a> <!-- 按键修饰符:回车时才会执行 --> <input @keyup.13="submit"><!-- 13 为 keycode --> <input @keyup.enter="submit"> <!-- 支持的全部按钮为 enter, tab, delete, space, up, down, left, right 字母 -->

表单的双向绑定

<input type="text" v-model="message"> <!-- 自定义选中值。否则 选中为value值,不选为空 --> <input type="checkbox" v-model="toggle" v-bind:true-value="a" v-bind:false-value="b">

绑定属性

<div v-bind:class="{ 'class-a': isA, 'class-b': isB }"></div> <div v-bind:class="classArr"></div> <!-- classArr 是一个数组 --> <!-- 简写 --> <div :class="{ 'class-a': isA, 'class-b': isB }"></div> <div :style="{ color: activeColor, fontSize: fontSize + 'px' }"></div> <img :src="imgSrc"> <a :href="baseURL + '/path'">

在 Vue 2 中,如果属性值是变量,必须用绑定属性的写法。

// wrong
<img src="{{imgSrc}}">
// right
<img :src="imgSrc">

避免闪烁: v-cloak

[v-cloak] {display: none;
}
<div v-cloak>{{ message }} </div>

不会显示 <div> 的内容,直到编译结束。

单向绑定

单向绑定的意思是,即使绑定变量的值发生变化,显示的内容仍旧就是最初绑定时候的值。

<!-- Vue 1 这么写 -->
<span>This will never change: {{* msg }}</span> <!-- Vue 2 不支持 -->

输出 HTML

<!-- Vue 1 这么写 -->
<div>{{{ raw_html }}}</div> <!-- {{}} 中的 HTML 内容的会转为纯文本 --> <!-- Vue 2 这么写 --> <div v-html="raw_html"></div>

计算属性

<div id="demo">{{fullName}}</div>
new Vue({data: {firstName: 'Foo', lastName: 'Bar' }, computed: { fullName: function () { return this.firstName + ' ' + this.lastName } } });

自定义指令

Vue.directive('my-directive', {bind: function () {// 准备工作 // 例如,添加事件处理器或只需要运行一次的高耗任务 this.el;// 添加指令的元素 this.vm.$get(name)// 获得该指令的上下文 ViewModel this.expression;// 指令的表达式的值 }, update: function (newValue, oldValue) { // 值更新时的工作 // 也会以初始值为参数调用一次 }, unbind: function () { // 清理工作 // 例如,删除 bind() 添加的事件监听器 } })
<div v-my-directive="someValue"></div>

监听数据变化

new Vue({data: {firstName: 'Foo' }, watch: { firstName: function (val, oldVal) { } } });

过滤器

{{ msg | capitalize }}// 'abc' => 'Abc'

常见内置过滤器
capitalize, uppercase, lowercase, json, limitBy, filterBy。所有见这里。

Vue 2 中把这些内置的过滤器都删除了。

自定义过滤器

Vue.filter('wrap', function (value, begin, end) { return begin + value + end; });
<!-- 'hello' => 'before hello after' -->
<!-- Vue 1 这么写 -->
<span v-text="message | wrap 'before' 'after'"></span> <!-- Vue 2 这么写 --> <span v-text="message | wrap('before', 'after')"></span>

生命周期相关的钩子函数

// Vue 1
new Vue({created: function(){}, beforeCompile: function(){}, compiled: function(){}, ready: function(){},// DOM 元素已经加入到HTML中 beforeDestroy: function(){}, destroyed: function(){} }); // Vue 2 new Vue({ created: function(){}, mounted : function(){},// 相对与 1 中的 ready beforeDestroy: function(){}, destroyed: function(){} });

过渡效果

<!-- Vue 1 这么写 -->
<div v-if="show" transition="my-transition"></div> <!-- Vue 2 这么写 --> <transition v-bind:name="my-transition"> <!-- ... --> </transition>
/* 必需 */
.my-transition-transition {transition: all .3s ease; } /* .my-transition-enter 定义进入的开始状态 */ .my-transition-enter{} /* .my-transition-leave 定义离开的结束状态 */ .my-transition-leave {}

组件

Vue 2 和 Vue 1 的组件的区别有些大。

Vue 1

定义和调用组件

<my-compprop="literal string":prop1="defaultOneWay":prop2.sync="twoWay" :prop3.once="oneTime"> </my-comp>
Vue.component('my-comp', {template: 'html strings', props: { prop: String, prop1: { type: Number, required: true }, prop2: { type: Number, default: 88 }, // 对象/数组的默认值应当由一个函数返回 prop3: { type: Object, default: function () { return { msg: 'hello' } } }, // 自定义验证函数 prop4: { validator: function (value) { return value > 10 } } }, data: functin(){ return { } } }

父子组件通信

// 子组件
var child = new Vue.component('child', { events: { 'parent-msg': function (msg) {} } }); // 子组件向父组件传消息 child.$dispatch('child-msg', child.msg); // 父组件 var parent = new Vue({ events: { 'child-msg': function (msg) { // 父组件向所有子组件传消息 this.$broadcast('parent-msg', 'received it'); } } });

this.$parent 访问它的父组件。
this.$root 访问它的根组件。
this.$children 访问它的子组件。

小技巧

渲染一个包含多个元素的块

<template v-for="item in items"> <li>{{ item.msg }}</li> <li class="divider"></li> </template> <template v-if="user"> <img :src="user.avatarUrl" alt=""> <div class="name">{{user.name}}</div> </template>

template 用于包含多个元素的块,最终的渲染结果不会包含 template 元素。

Vue.set 和 Vue.delete

用于解决 不能检测到属性添加,属性删除的限制。

Vue.nextTick

// 修改数据
vm.msg = 'Hello'
// DOM 没有更新
Vue.nextTick(function () { // DOM 更新了 })

Vue 在检测到数据变化时是异步更新 DOM 的。具体见 异步更新队列。vm 上也有 this.$nextTick

vue 插件

路由: vue-router

官方文档

定义路由

var Vue = require('vue');
var VueRouter = require('vue-router'); Vue.use(VueRouter); router.map({ '/login': {component: require('login')}, '/user/:': {component: ...}, // 异步加载组件。加载器用的 webpack '/another': {component: function(resolve) { require.ensure([], function(require) { resolve(require('...')); }); }}, }); router.redirect({ '*': '/login' }); // 默认路由 router.beforeEach(function(transition) { // console.info('show loading'); transition.next(); }).afterEach(function(transition) { // console.info('hide loading'); }); // 启动 router.start(Vue.extend({}), '#app');

使用路由

<a v-link.literal="/a/b/c"></a> <a v-link="{ path: Foo }">Go to Foo</a>
new Vue({ready: function(){this.$route.path; this.$route.params; this.$route.query; }, methods: { jumpUrl: function () { // 代码跳转 this.$route.router.go('/a'); this.$route.router.go({ path: '/a', replace: true // 是否产生新的历史记录 }); this.$route.router.go({ name: 'a', // 具名路径 params: {}, query: {} }); } } });

异步请求: vue-resource

官网

Vue.http.get('/someUrl', [options]).then(successCallback, errorCallback);
Vue.http.post('/someUrl', [body], [options]).then(successCallback, errorCallback);

拦截

Vue.http.interceptors.push(function(request, next) {var data = request.data;// 添加 url 前缀 request.url = serverPrefix + request.url; // 加请求参数 request.data.sessionid = localStorage.getItem('sessionid'); next(function (response) { if(登陆超时){ setTimeout(function () { router.go('/login'); }); } else { // modify response response.body = '...'; } }); });

支持 Promise

例如

Vue.http.post('/someUrl', [optinos]).then(function(res) { var data = res.data; return new Promise(function(resolve, reject) { if (!data.error) { reject() } else { resolve(data); } }.bind(this)); }) .then(function(res) { var data = res.data; return new Promise(function(resolve, reject) { Vue.http.post('/someUrl', data).then(function (res) { if(res.data){ resolve(); } }); }.bind(this)); }, failFn) .then(succFn, failFn);

其他

vm.$options.filters.filter名称 可以获取到具体的 filter

转载于:https://www.cnblogs.com/raocheng/articles/6298018.html

Vuejs 写法实例相关推荐

  1. button layui 点击事件_Layui 带多选框表格监听事件以及按钮自动点击写法实例

    不话不多说了,直接上代码吧: 获取选中行数据 获取选中数目 验证是否全选 提交选择 id运动名运动时长运动类型 var msg=""; var idlist="" ...

  2. Oracle存储过程日常工作整理写法实例

    Oracle存储过程日常工作整理写法实例 create or replace package body CUX_HRMIS_SYNC_PKG is /*  *-- Creation time:2016 ...

  3. php 类 单例,PHP里的单例类写法实例

    PHP里的单实例类在进行数据交换,节省内存上还是很有意义的.写个简单例子. 类1,单实例类本身: class UTIL { private static $instance; public funct ...

  4. Python 相对路径、绝对路径的写法实例演示

    Python 里的路径分相对路径和绝对路径. 绝对路径 绝对路径好理解,我们以Girl.py来举例子. 这就是它的绝对路径: "E:/Python/Lib/site_packages/Gir ...

  5. oracle返回表id,Oracle 函数返回表实例2种写法实例

    --注:用ps/sql工具执行语句 --一合同预算状态函数 --1. drop type htjs_business_row drop type htjs_business_state_row_typ ...

  6. mysql if语句 oracle,Oracleifelse语句的写法实例

    接收contract_no和item_no值,在inventory表中查找,如果产品:已发货,在arrival_date中赋值为今天后的7天已订货,在arriva 接收contract_no和item ...

  7. 应用程序连接Oracle rac的URL写法

    http://blog.csdn.net/hu_shengyang/article/details/7043271 Oracle RAC 集群示意图 RAC,全称real application cl ...

  8. PromSql的写法详解

    一: 介绍 PromSql是 Prometheus的数据库查询语法,可以方便的查找监控指标的值并且做一些数学运算帮助我们能快速和方便的去统计数据以及设置告警阈值,并且Promsql查询的是时序数据库就 ...

  9. JAVA常用基础API(经典实例)

    API文档注释写法 实例: package apidoc; /*** 在类上面写文档注释,是用来说明当前类的设计目的.功能* @author ta* @version 1.0* @see java.l ...

最新文章

  1. docker 添加端口映射_Docker容器修改端口映射
  2. 跑三小时的monkey测试该怎么算_百亿次的锤炼 - 带逛Dragonboat的各类测试
  3. php 单例模式原理,PHP单例模式demo详解
  4. 【收集】13款Linux系统有
  5. OpenStack 如何跨版本升级
  6. [Ext JS 4] 拖放[drag and drop]
  7. LeetCode(181)——超过经理收入的员工(MySQL)
  8. Android 调试技巧之快速重启生效
  9. CrazyWing:Python自动化运维开发实战 十八、Python面向对象
  10. Sync Framework 词汇表
  11. 电脑之间利用串口传输文件
  12. NOIP2016 游记
  13. 推荐几款不错的企业网站,前端设计师寻求设计灵感!
  14. Gap Statistic算法详解
  15. android DISALLOW_EVIL_CONSTRUCTORS
  16. 如何设置文件的默认打开方式
  17. 直播预告|无监督学习的公平性研究——美国布兰迪斯大学助理教授刘洪甫
  18. 加州大学欧文分校 计算机专业,加州大学欧文分校信息与计算机科学
  19. 恒生电子面试过程纪录
  20. Jenkins Pipeline 小试牛刀以及引入指定的环境变量

热门文章

  1. http 断点续传,Windows下HTTP方式单线程下载
  2. STL之deque和其他容器
  3. 你的GitHub,怎么和我用的不太一样?
  4. jQuery基础-事件
  5. IDEA mybatis-generator-maven-plugin 插件的使用
  6. spring mvc 配置解析之xml
  7. Windows Phone 7 自适应键盘输入
  8. 书摘---创业36条军规1:创业是怎么回事
  9. ibator1.2.2
  10. 北向资金进行股票、期货指数、基金策略