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. VDOM总结

    https://segmentfault.com/a/1190000016129036

  2. NP:建立可视化输入的二次函数数据点集np.linspace+np.random.shuffle+np.random.normal

    import numpy as np import matplotlib.pyplot as plt def fix_seed(seed=1): #重复观看一样东西 # reproducible np ...

  3. HDU 2888 Check Corners (模板题)【二维RMQ】

    <题目链接> <转载于 >>> > 题目大意: 给出一个N*M的矩阵,并且给出该矩阵上每个点对应的值,再进行Q次询问,每次询问给出代询问子矩阵的左上顶点和右下 ...

  4. 数据库相关--net start mysql 服务无法启动(win7系统)解决

    系统:win7 旗舰版 64位 MySQL:8.0.11 家里台式机上不久之前安装了MySQL,一段时间没碰过后,突然启动不了了(我有一头小毛驴,我从来也不骑,有一天我心血来潮骑它去赶集) 先是在系统 ...

  5. Rabbit的机器人-二分答案

    Rabbit的机器人 思路 : 可以 推知 挡板的位置与最后 一步的方向有关 .如果是 R 根据题目要求那么最终结果一定是在>0的位置, 因为按照题意要求的最终不能回到重复走过的位置.所以如果有 ...

  6. AE文件特别大

    解决AE的输出文件太大_百度经验 在渲染队列的输出组件中把格式选项改为H.264,然后你会得到一个MP4文件. 然后最关键的一步,打开格式选项,把目标比特率和最大比特率均设10(比特率越高,视频质量越 ...

  7. Spring Boot基础讲解

    Spring Boot Spring Boot 是由Pivotal团队提供的框架,它并不是一个全新的框架,而是将已有的 Spring 组件整合起来,设计目的是用来简化新Spring应用的初始搭建以及开 ...

  8. DWM1000 长距离模块讨论

    蓝点DWM1000 模块已经打样测试完毕,有兴趣的可以申请购买了,更多信息参见 蓝点论坛 正文: DWM1000 作为超宽室内带测距/定位的代表,官方给出的模块标准距离30-50m,而目前X宝上有很多 ...

  9. MySql中的约束

    mysql中的约束使用和oracle使用差别不大. 1.主键约束 如同人对应身份证,主键能够唯一地标识表中的一条记录,可以结合外键来定义数据表之间的关系. 主键约束要求主键列的数据唯一,并且不允许为空 ...

  10. ubuntu下使用nvm安装nodejs

    sudo apt-get install curl curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.6/install. ...