1.事件派发:子控件->父控件

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head> <body>
<div id="app">
<div>message : {{ message | json }}</div>
<input type="hidden" v-model="message | json" />
<child-component></child-component>
</div> <template id="child-component">
<input type="text" v-model="msg" />
<button @click="notify">添加事件</button>
</template>
</body>
<script src="jquery.js"></script>
<script src="vue.js"></script>
<script> var app = new Vue({
el:"#app",
data:{
message:[]
},
components:{
'child-component':{
template:'#child-component',
data:function(){
return {
msg:''
}
},
methods:{
notify:function(){
if($.trim(this.msg)){
// 派发事件
this.$dispatch('child-msg', this.msg, 222);
this.msg = '';
}
}
}
}
},
// 事件
events:{
'child-msg':function(msg, data2){
this.message.push(msg);
console.log(this.message);
console.log(data2);
}
}
}); </script>
</html>

2.事件广播:父控件->子控件

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head> <body>
<div id="app">
<input type="text" v-model="msg" />
<child-component></child-component>
<button @click="notify">广播事件</button>
</div> <template id="child-component">
<ul>
<li v-for="item in messages">
录入内容:{{ item }}
</li>
</ul>
</template>
</body>
<script src="jquery.js"></script>
<script src="vue.js"></script>
<script> // 注册组件
Vue.component('child-component', {
template:'#child-component',
data:function(){
return {
messages:[]
}
},
events:{
'parent-msg':function(msg, data2){
console.log(data2);
this.messages.push(msg);
}
}
}); var app = new Vue({
el:'#app',
data:{
msg:''
},
methods:{
notify:function(){
if($.trim(this.msg)){ // console.log(this.msg);
// 广播
this.$broadcast('parent-msg', this.msg, 333);
this.msg = '';
}
}
}
}); </script>
</html>

3.ajax

ajax-helper.js
function AjaxHelper() {
this.ajax = function(url, type, dataType, data, callback) {
$.ajax({
url: url,
type: type,
dataType: dataType,
data: data,
success: callback,
error: function(xhr, errorType, error) {
// alert('Ajax request error, errorType: ' + errorType + ', error: ' + error)
console.log('Ajax request error, errorType: ' + errorType + ', error: ' + error);
}
})
}
}
AjaxHelper.prototype.get = function(url, data, callback) {
this.ajax(url, 'GET', 'json', data, callback)
}
AjaxHelper.prototype.post = function(url, data, callback) {
this.ajax(url, 'POST', 'json', data, callback)
} AjaxHelper.prototype.put = function(url, data, callback) {
this.ajax(url, 'PUT', 'json', data, callback)
} AjaxHelper.prototype.delete = function(url, data, callback) {
this.ajax(url, 'DELETE', 'json', data, callback)
} AjaxHelper.prototype.jsonp = function(url, data, callback) {
this.ajax(url, 'GET', 'jsonp', data, callback)
} AjaxHelper.prototype.constructor = AjaxHelper

server.php

<?php

$op = $_REQUEST;

if(empty($op)){
$op = 'people';
}
else{
$op = $_REQUEST['op'];
} $data = array(); if($op == 'people'){ $people = array(
array('name'=>'keenleung', 'age'=>25),
array('name'=>'keenleung2', 'age'=>26),
);
$data = $people;
} echo json_encode(array(
'status' => 'success',
'data' => $data
));

html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
template, simple-table{
display: none;
}
</style>
</head> <body>
<div id="app">
<simple-table></simple-table>
</div> <template id="simple-table">
<table>
<tr>
<th>姓名</th>
<th>年龄</th>
</tr>
<tr v-for="row in rows">
<td>{{ row.name }}</td>
<td>{{ row.age }}</td>
</tr>
</table>
</template>
</body>
<script src="jquery.js"></script>
<script src="vue.js"></script>
<script src="ajax-helper.js"></script>
<script> var ajaxHelper = new AjaxHelper(); var app = new Vue({
el:'#app',
components:{
'simple-table':{
template:'#simple-table',
data:function(){
return {
rows:[],
// 本地服务器
url:'http://www.mysites.com/vue/server.php',
}
},
methods:{
getRows:function(){
var vm = this;
callback = function(data){
// console.log(data); // 设置值
vm.$set('rows', data.data); // 获取值
console.log(vm.$get('rows'));
} ajaxHelper.get(vm.url, null, callback);
}
},
// 执行方法
ready:function(){
this.getRows();
}
}
}
});
</script>
</html>

vue: 代码小记的更多相关文章

  1. 团队协作统一vue代码风格,vscode做vue项目时的一些配置

    1. 安装Vetur 扩展 主要是用于让vscode能识别vue文件,对vue代码进行高丽处理,并且它内置了一些代码格式化的设置 2. 安装ESLint 如果你的项目已经开启了eslint规范, 再有 ...

  2. vscode vue代码提示错误

    在用vscode编写vue代码时,因为安装的有vetur插件,所以当代码中有v-for语法时,会提示 [vue-language-server] 'v-for' directives require ...

  3. 小白学习VUE第一篇文章---如何看懂网上搜索到的VUE代码或文章---使用VUE的三种模式:

    小白学习VUE第一篇文章---如何看懂网上搜索到的VUE代码或文章---使用VUE的三种模式: 直接引用VUE; 将vue.js下载到本地后本目录下使用; 安装Node环境下使用; ant-desig ...

  4. Vue代码分割懒加载的实现方法

    什么是懒加载 懒加载也叫延迟加载,即在需要的时候进行加载,随用随载. 为什么需要懒加载 在单页应用中,如果没有应用懒加载,运用webpack打包后的文件将会异常的大,造成进入首页时,需要加载的内容过多 ...

  5. vue进阶:vs code添加vue代码片段

    如何设置? 选择或创建 配置代码 如何使用? 一.如何设置? 进入vs code主界面-->使用快捷键“ctrl + shift + p”: 如果你是使用Preferences:Configur ...

  6. 解决Vue刷新一瞬间出现样式未加载完或者出现Vue代码问题

    解决Vue刷新一瞬间出现样式未加载完或者出现Vue代码问题: <style> [v-cloak]{ display: none; } </style> <div id=& ...

  7. vscode+eslint自动格式化vue代码的方法

    前言 使用vscode开发vue项目的时候,为了编码格式的统一化,使用eslint规范进行格式化.此时通过eslint插件可以实现对vue代码的自动格式化. 使用方式 在vscode的插件模块处,搜索 ...

  8. <tangmuchw>之新手vue项目小记--新建.vue文件,运行项目,出现error:This dependency was not found...

    错误码: This dependency was not found: * !!vue-style-loader!css-loader?{"minimize":false,&quo ...

  9. vscode中vue代码颜色插件

    vue提示插件[Vscode]       编者寄语:vscode的确是前端开发中很好的工具,安装颜色插件,从视觉上是美的享受.曾经的我遇到了vscode代码全是灰色,黑色的困惑,后来整理找到方法,整 ...

随机推荐

  1. Mysql数据库报错:Cannot add or update a child row: a foreign key constraint fails(添加多对多关系)

    #创建班级表 class Classes(models.Model): title = models.CharField(max_length=32) n=models.ManyToManyField ...

  2. BZOJ.4888.[TJOI2017]异或和(树状数组)

    BZOJ 洛谷 \(Description\) 求所有区间和的异或和. \(n\leq 10^5,\ \sum a_i\leq 10^6\). \(Solution\) 这样的题还是要先考虑按位做. ...

  3. Java笔记(七)HashMap和HashSet

    HashMap和HashSet 一)HashMap 1.Map接口 interface Map<K,V> { int size();//查看Map中的键值对个数 boolean isEmp ...

  4. [P1516]青蛙的约会 (扩展欧几里得/中国剩余定理?)

    每日做智推~ 一看就是一道数学题. 再看是一道公约数的题目. 标签是中国孙子定理. 题解是扩展欧几里得 (笑) 一开始没看数据范围 只有50分 开一个longlong就可以了 #include< ...

  5. C++学习笔记51:排序

    //直接插入排序函数模板 template <class T> void insertionSort(T a[], int n) { int i, ; T temp; ; i < n ...

  6. R语言语法基础二

    R语言语法基础二 重塑数据 增加行和列 # 创建向量 city = c("Tampa","Seattle","Hartford"," ...

  7. PAT Basic 1009

    1009 说反话 (20 分) 给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式: 测试输入包含一个测试用例,在一行内给出总长度不超过 80 的字符串.字符串由若干单词和若干空格 ...

  8. 基于ubuntu的docker安装

    系统版本:Ubuntu16.04 docker版本:18.02.0 Ubuntu 系统的内核版本>3.10(执行 uname -r 可查看内核版本)   在安装前先简单介绍一下docker,按照 ...

  9. JAVA自学作业03

    JAVA自学作业03 1.请用最有效率的方式计算2*8的结果 使用位运算是最有效率的. System.out.println(2<<3); 2.请交换两整数的值 int x = 10; i ...

  10. [原创]AndroBugs_Framework Android漏洞扫描器介绍

    [原创]AndroBugs_Framework Android漏洞扫描器介绍 1 AndroBugs_Framework Android 漏洞扫描器简介 一款高效的Android漏洞扫描器,可以帮助开 ...