vue.js+nodejs的MVVM笔记


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值