vue: 代码小记
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
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: 代码小记的更多相关文章
- 团队协作统一vue代码风格,vscode做vue项目时的一些配置
1. 安装Vetur 扩展 主要是用于让vscode能识别vue文件,对vue代码进行高丽处理,并且它内置了一些代码格式化的设置 2. 安装ESLint 如果你的项目已经开启了eslint规范, 再有 ...
- vscode vue代码提示错误
在用vscode编写vue代码时,因为安装的有vetur插件,所以当代码中有v-for语法时,会提示 [vue-language-server] 'v-for' directives require ...
- 小白学习VUE第一篇文章---如何看懂网上搜索到的VUE代码或文章---使用VUE的三种模式:
小白学习VUE第一篇文章---如何看懂网上搜索到的VUE代码或文章---使用VUE的三种模式: 直接引用VUE; 将vue.js下载到本地后本目录下使用; 安装Node环境下使用; ant-desig ...
- Vue代码分割懒加载的实现方法
什么是懒加载 懒加载也叫延迟加载,即在需要的时候进行加载,随用随载. 为什么需要懒加载 在单页应用中,如果没有应用懒加载,运用webpack打包后的文件将会异常的大,造成进入首页时,需要加载的内容过多 ...
- vue进阶:vs code添加vue代码片段
如何设置? 选择或创建 配置代码 如何使用? 一.如何设置? 进入vs code主界面-->使用快捷键“ctrl + shift + p”: 如果你是使用Preferences:Configur ...
- 解决Vue刷新一瞬间出现样式未加载完或者出现Vue代码问题
解决Vue刷新一瞬间出现样式未加载完或者出现Vue代码问题: <style> [v-cloak]{ display: none; } </style> <div id=& ...
- vscode+eslint自动格式化vue代码的方法
前言 使用vscode开发vue项目的时候,为了编码格式的统一化,使用eslint规范进行格式化.此时通过eslint插件可以实现对vue代码的自动格式化. 使用方式 在vscode的插件模块处,搜索 ...
- <tangmuchw>之新手vue项目小记--新建.vue文件,运行项目,出现error:This dependency was not found...
错误码: This dependency was not found: * !!vue-style-loader!css-loader?{"minimize":false,&quo ...
- vscode中vue代码颜色插件
vue提示插件[Vscode] 编者寄语:vscode的确是前端开发中很好的工具,安装颜色插件,从视觉上是美的享受.曾经的我遇到了vscode代码全是灰色,黑色的困惑,后来整理找到方法,整 ...
随机推荐
- hdu 3499 flight 【分层图】+【Dijkstra】
<题目链接> 题目大意: 现在给你一些点,这些点之间存在一些有向边,每条边都有对应的边权,有一次机会能够使某条边的边权变为原来的1/2,求从起点到终点的最短距离. 解题分析: 分层图最短路 ...
- 001.CDN概述
一 互联网应用质量概述 1.1 互联网应用质量 互联网应用质量指标--QoE,其主要指标: 服务成功率:指用户所请求的服务成功完成的几率. 服务建立时间:指从服务请求到服务呈现所花费的时间,并且会因为 ...
- Django项目中使用Redis
Django项目中使用Redis DjangoRedis 1 redis Redis 是一个 key-value 存储系统,常用于缓存的存储.django-redis 基于 BSD 许可, 是一个使 ...
- 解决Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-f8IeEI/MYSQL-python/
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-f8IeEI/MYS ...
- SpringBoot使用JdbcTemplate
前言 本文是对SpringBoot使用JdbcTemplate操作数据库的一个介绍,,提供一个小的Demo供大家参考. 操作数据库的方式有很多,本文介绍使用SpringBoot结合JdbcTempla ...
- 解决在Ue4中当摄像机拉得超级远时图像出现的撕裂情况
当摄像机拉的超级远时场景就会出现这个情况.这个时候输入 r.SetNearClipPlane ,设置一下裁界面就好了
- Codeforces.1051G.Distinctification(线段树合并 并查集)
题目链接 \(Description\) 给定\(n\)个数对\(A_i,B_i\).你可以进行任意次以下两种操作: 选择一个位置\(i\),令\(A_i=A_i+1\),花费\(B_i\).必须存在 ...
- svn window下过滤文件(如配置文件等)
第一种: 在资源管理器中,右键一个未加入版本控制文件或目录,并从弹出菜单选择TortoiseSVN →Add to Ignore List,会出现一个子菜单,允许你仅选择该文件或者所有具有相同后缀的文 ...
- Conscription [POJ3723] [最小生成树]
Description: Windy有一个国家,他想建立一个军队来保护他的国家. 他召集了N个女孩和M男孩,想把他们雇佣成为他的士兵. 要无偿雇佣士兵,必须支付10000元. 女孩和男孩之间有一些关系 ...
- Flask特殊装饰器
@app.errorhandler():重定义错误返回信息 @app.errorhandler(404) #监听多少写多少 def error404(message): return f"你 ...