egg--this

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_39115469/article/details/96423741

一般在声明的 async 方法中使用 this 时,会在前面加 await 关键字,表示同步,即等待其他执行完毕后,在执行。

渲染模板:

this.ctx.render('html/login',{   //ctx表示context对象
          msg:'账号或密码错误',
        });  

调用service:

//2.调用service,返回验证结果
var flag = await this.service.user.validate(username,password);

获取get方式请求的参数:

var username = this.ctx.query.username;//获取username值

响应客户:

this.ctx.body = 'hi, machi';

获取 config.default.js 配置文件中的某一配置:

//比如 config.default.js 中的某一配置:
config.API_SETTING = 'http://localhost:7001/mock_setting';



//在代码中获取
const url = this.config.API_SETTING;

调用外部接口,获取数据,注意前面加 await 关键字:

//请求外部接口
//this.ctx.crul  前面需要加await关键字
const code = this.ctx.query.code;			//获取 code 请求参数
const result = await this.ctx.curl(this.config.API_NOTICE, {		//调用接口
      dataType: 'json',
      data: {
        code,
        name:'jay',
      },
    });

在controller中 获取国际化资源:

var email = this.ctx.__('Email');

URL对象:

const url=new URL(this.ctx.href);     // "http://localhost:7001/test"

require:

const allData = require('../public/data/gover_type_list.json');//获取文件
this.ctx.body = allData;  //响应

hasOwnProperty 用法:

const da = {
      "dat1": "hasOwnProperty",
      "dat2": 123
    };
    if(da.hasOwnProperty('dat1')){    //如果含有 dat1 属性
      this.ctx.body = 'hasOwnProperty';
    }else{
      this.ctx.body = 'hihi';
    }

猜你喜欢

转载自blog.csdn.net/qq_39115469/article/details/96423741
egg