vue.js+nodejs的MVVM笔记
来源:互联网 发布:什么叫it行业 编辑:程序博客网 时间:2024/05/16 17:11
1、页面引入vue.js
<script src="/static/js/vue.js"></script> <script src="/static/js/vue-resource.js"></script>
2、在<head>中编写js代码
$(function () { var vm = new Vue({ el: '#vm', data: { email: '', password: '', birth: '', gender: 's', language: ['zh'], city: 'bj', introduction: '', subscribe: true }, methods: { register: function () { //$('#modal-data').text(JSON.stringify(this.$data, null, ' ')); //$('#modal').modal(); // TODO: post JSON data to server... //alert(JSON.stringify(this.$data, null, ' ')); //$('#code').val(''); var vm = this; alert(JSON.stringify(this.$data, null, ' ')); //Vue.http.options.emulateJSON = true; //var p ={name:"zhangsan",id:"aaaa"} vm.$http.post('/register',this.$data,{emulateJSON:true}).then((response) => { //vm.$set('gridData', response.data) \ alert(JSON.stringify(response.body, null, ' ')); $('#code').text(JSON.stringify(response.body, null, ' ')); }); //$('#code').text(JSON.stringify(this.$data, null, ' ')); } } }); window.vm = vm;});
3、服务端nodejs代码
const Koa = require('koa');const bodyParser = require('koa-bodyparser');const router = require('koa-router')();const app = new Koa();const isProduction = process.env.NODE_ENV === 'production';// log request URL:app.use(async (ctx, next) => { console.log(`Process ${ctx.request.method} ${ctx.request.url}...`); console.log('----------1--------->'); var start = new Date().getTime(), execTime; await next(); execTime = new Date().getTime() - start; ctx.response.set('X-Response-Time', `${execTime}ms`);});// parse request body:app.use(bodyParser());router.post('/register', async (ctx, next) => { console.log('-----------2-------->'); var body = ctx.request.body; console.log('----------3--------->' + body.introduction); //body.introduction='from node server.'; ctx.response.body = body;});router.get('/', async (ctx, next) => { console.log('-----------4-------->' + ctx.request.path); ctx.response.redirect('/static/index.html');});// static file support:let staticFiles = require('./static-files');app.use(staticFiles('/static/', __dirname + '/static'));// redirect to /static/index.html://app.use(async (ctx, next) => {// console.log('-----------4-------->');// ctx.response.redirect('/static/index.html');//});// add router middleware:app.use(router.routes());app.listen(3000);console.log('app started at port 3000...');
4、参考连接
http://www.liaoxuefeng.com/wiki/001434446689867b27157e896e74d51a89c25cc8b43bdb3000/00147574857851718682c42639f466a934ad9d4f485d1f2000
阅读全文
0 0
- vue.js+nodejs的MVVM笔记
- vue js的MVVM模式
- Vue.js学习笔记-2-MVVM
- Vue.js和MVVM的小细节
- Vue.js 和 MVVM
- Vue的MVVM原理图
- 浅谈对Vue.js的MVVM的理解
- VUE的MVVM框架解析
- 安装vue.js的方法,安装nodejs,安装cnpm
- Ubuntu下Vue.js+webpack的安装(nodejs版本纠正)
- Vue.js 和 MVVM 小细节
- Vue.js 和 MVVM 小细节
- Vue.js 和 MVVM 小细节
- Vue.js 和 MVVM 小细节
- Vue.js 和 MVVM 小细节
- Vue.js 和 MVVM 小细节
- Vue.js 和 MVVM 小细节
- vue.js和mvvm小细节
- Android网络API(android.net.wifi.aware):概览
- h5安卓页面键盘遮挡表单的办法处理
- HEVC学习笔记(一)——基本知识
- Android px与dip(dp)、px与sp单位的转换
- 服务器配置的各种英文简称
- vue.js+nodejs的MVVM笔记
- caffe中Eltwise层
- yii, gridview隐藏查看view按钮以及自定义查看按钮链接
- Mybatis的sql封装
- 表达式与流程控制(2)
- sql经典语句
- 设计模式(1)-工厂方法模式
- 使用java代码在本地创建文件夹(多层目录),在本地磁盘创建文件目录
- JAVA设计模式之单例模式