npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

koa-vue-view

v2.1.6

Published

A Koa view engine which renders Vue components on server.

Readme

koa-vue-view

Build Status image image image

A Koa view engine which renders Vue components on server.

1.x的分支/npm包支持koa1;master分支和2.x版本的npm包支持koa2。

需求

我熟悉书写vue的代码,感觉她的语法很简洁明了,并且支持组件化;我最近在学习使用koa编写node web应用,在koa框架中渲染视图有很多选择;但是我想在koa中使用vue来渲染视图;

我在调研了vue的ssr解决方案后,感觉她很好,但是不满足我的需求,我只是想用她的语法和组件化来实现视图渲染,渲染的数据想从koa的ctx.state中读取,也不想前后端同用同一套路由这种方式;

所以我觉得用vue的ssr的基础部分——服务端渲染vue实例,来完成我的需求,即此中间件诞生;

本中间件包含功能:

  • 服务端渲染vue语法的视图文件
  • 视图文件的语法采用vue组件的编写语法
  • 支持vue的组件化
  • 支持全局数据、组件等共享

注意:本中间件虽然支持vue组件的编写语法,但是仅会处理其中的template部分,其他的如stylescript等部分都会原样输出

待添加功能:

  • 不应编译视图文件中template标签中的前端用的vue代码

安装

npm i -S koa-vue-view

使用

<!--模板: ./views/Master.vue -->
<template>
    <html lang="zh-CN">

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>{{hight(app.name)}} - {{app.version}}</title>
        <slot name="meta"></slot>
    </head>

    <body>
        <h1>{{layoutName}} - {{layoutVersion}}</h1>
        <slot name="top"></slot>
        <slot></slot>
        <slot name="bottom"></slot>
    </body>

    </html>
</template>

<!--组件: ./components/Age.vue -->
<template>
    <strong style="color:red;">
        <slot></slot>
    </strong>
</template>


<!--页面: ./views/User.vue -->
<template>
    <Master>
        <ul>
            <li v-for="(item,index) in users" :key="index">{{item.name}} <Age>{{ add(item.age, 1) }}</Age></li>
        </ul>
    </Master>
</template>
var path = require('path');
var Koa = require('koa');
var VueView = require('koa-vue-view');

var app = new Koa();
app.use(VueView({
    methodName: 'render',//在koa ctx注册渲染视图的方法名,默认render
    data: {
        _: require('lodash'),
        app: {
            name: 'Github',
            version: '1.0.0'
        }
    },
    methods: {
        add(a, b) {
            return a + b;
        }
    },
    components: {
        Master: {
            path: path.resolve(__dirname, './views/Master.vue'),
            data() {
                this.layoutVersion = '1.0.0';
                return {
                    layoutName: 'master'
                }
            },
            methods: {
                hight(str) {
                    return `***${str}***`;
                }
            }
        },
        Age: path.resolve(__dirname, './components/Age.vue')
    }
}));

app.use(ctx => {
    ctx.state.users = [{
        name: 'Tom',
        age: 20
    }, {
        name: 'Alice',
        age: 18
    }];
    ctx.render(path.resolve(__dirname, './views/User.vue'));
    /*
    或者
    ctx.render({
        path:path.resolve(__dirname, './views/User.vue'),
        data(){
            return {name:'Github'}
        },
        methods:{
            show(){}
        }
    });
    */
})


app.listen(8200);

规约

  • 在读取视图文件内容时,会将其内容分割为三部分:headertemplatefooter
    • template截取自文件中第一对顶级template标签中的内容;
    • header截取自文件中第一对顶级template标签的前面内容;
    • footer截取自文件中第一对顶级template标签的后面内容;
    • 视图文件中仅允许包含一对顶级template标签
  • 渲染视图时仅渲染template部分

Options

app.use(require('koa-vue-view')(options));

可接受的options选项:

Render

app.use(ctx => {
    ctx.render(文件路径|组件配置对象).then(html=>{})
})

更新日志

1.x对应的是koa1适用的版本,2.x对应的是koa2对应的版本;

2.1.6 | 1.1.6

  • 解决全局组件中引用全局组件时渲染出错的问题;
  • 加入filterHtml配置项,用于过滤渲染后的html字符串

2.1.5

  • fix issues#1

1.1.2

  • fix issues#1

2.1.3

  • 核心功能实现

1.1.1

  • 核心功能实现