egg3.0渲染模板egg-view-nunjucks,支持点击事件

安装依赖

$ npm i egg-view-nunjucks --save
  "dependencies": {
    "egg": "^3",
    "egg-mongoose": "^3.3.1",
    "egg-scripts": "^2",
    "egg-view-nunjucks": "^2.3.0"
  },

配置信息

app/config/plugin.js

'use strict';

/** @type Egg.EggPlugin */
module.exports = {
  mongoose: {
    enable: true,
    package: 'egg-mongoose',
  },
  nunjucks: {
    enable: true,
    package: 'egg-view-nunjucks',
  },
};

app/config/config.default.js

'use strict';

/**
 * @param {Egg.EggAppInfo} appInfo app info
 */
module.exports = appInfo => {
  /**
   * built-in config
   * @type {Egg.EggAppConfig}
   **/
  const config = exports = {
    security: {
      csrf: {
        enable: false,
      },
    },
    mongoose: {
      client: {
      // 链接到本地的MongoDB,note是我本地数据库的名字
        url: 'mongodb://127.0.0.1:27017/note',
        options: {
          // dbName: 'note',
          useNewUrlParser: true,
        },
      },
    },
    view: {
      defaultViewEngine: 'nunjucks',
      mapping: {
        '.tpl': 'nunjucks',
      },
    },
  };

  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1682063943419_2410';

  // add your middleware config here
  config.middleware = [];

  // add your user config here
  const userConfig = {
    // myAppName: 'egg',
  };

  return {
    ...config,
    ...userConfig,

  };
};

支持tpl文件高亮

在settings.json文件中添加配置

  "files.associations": {
    "*.tpl": "html"
  },

定义view

app\view\role.tpl

<html>

<head>
  <title>Role Test</title>
  <link rel="stylesheet" href="/public/css/role.css" />
</head>

<body>

  <ul class="role-view view" id="role-box" >
    {% for item in list %}
    <li class="item" data-item="{{item}}">
      {{ item.name }}
    </li>
    {% endfor %}
  </ul>
</body>
<script>

  const dom = document.getElementById('role-box')
  dom.addEventListener('click', (e) => {
    console.log('e', e);
  })
  console.log(dom);

</script>

</html>

css样式

app\public\css\role.css

.role-view  {
  background-color: palevioletred;
}

.role-view .item {
  height: 26px;
  line-height: 26px;
}

app/controller/role.js

以下代码为上述文件的其中一个函数

  // 查询数据
  async list() {
    const { ctx } = this;
    const res = await ctx.service.role.findRoleList();
    console.log('render', res);

    // ctx.body = res;
    await ctx.render('role.tpl', { list: res });
  }

app/router.js

'use strict';

/**
 * @param {Egg.Application} app - egg application
 */
module.exports = app => {
  const { router, controller } = app;
  router.get('/', controller.home.index);
  router.get('/role', controller.role.list);
  router.post('/role/add', controller.role.add);
  router.post('/role/bitch_add', controller.role.bitch_add);
  router.delete('/role/del', controller.role.del);
  router.delete('/role/bitch_del', controller.role.bitch_del);
  router.post('/roles', controller.rolePost.index);
};

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值