vue: 代码小记
2023-09-27 14:23:58 时间
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路径中的#号
- vscode代码保存时自动格式化成ESLint风格(支持VUE)
- vue项目中使用代码编辑器,视频播放器插件
- vue-codemirror 代码编辑器
- vscode+eslint自动格式化vue代码的方法
- vue-element-admin关闭代码校验eslint
- 精品微信小程序ssm的物流快递管理平台+后台前后分离VUE
- vue.js:使用v-html绑定HTML代码案例
- vue项目js实现图片放大镜功能
- 【vue环境】全局安装和卸载vue-cli
- vue v-for语法遍历
- 利用Vue的插件机制,实现项目公共代码管理
- 分享几个简单的技巧让你的 vue.js 代码更优雅
- vue+axios+promise实际开发用法
- 【Vue路由】路由守卫、生命周期钩子、路由器工作模式
- vue系列:vue/cli3配置watch实时代码打包
- 前端工程化:vue代码检查工具vetur
- vue非常实用的几行代码【日期处理、字符串处理、数组处理、颜色操作】
- 【Vue 开发实战】实战篇 # 45:如何构建可交互的组件文档让代码高亮的显示在页面
- vscode里怎么添加vue代码片段
- 在vue里用codemirror实现代码编辑器功能?
- Vue Material
- 使用Webpack的代码分离实现Vue懒加载(译文)
- Vue.js 系列教程 4:Vuex