简体   繁体   English

如何将ExpressJS,VueJS,Jade一起使用?

[英]How use a ExpressJS, VueJS, Jade together?

What is the best practice to use ExpressJS, VueJS and Jade together? 将ExpressJS,VueJS和Jade一起使用的最佳做法是什么?

It's a little bit stupid question, but do I need convert Jade to HTML (like I know, because VueJS can't serve Jade files)? 这是一个有点愚蠢的问题,但我是否需要将Jade转换为HTML(就像我知道的那样,因为VueJS无法提供Jade文件)?
Do i need serve a Jade or converted HTML index file in ExpressJS? 我需要在ExpressJS中提供Jade或转换后的HTML索引文件吗?

Without using VueJS, code in my index.js file is something like this: 不使用VueJS,我的index.js文件中的代码是这样的:

app.set("view engine", "pug");
app.set("views", __dirname + "/templates");


app.get('/', function(req, res){
  res.render("index");
});

And when I want to use Gulp, then.. how? 当我想使用Gulp时,那么......怎么样?

ExpressJS lives at the back-end and uses your .jade (actually renamed as .pug) templates to generate and serve html. ExpressJS位于后端,使用.jade(实际上重命名为.pug)模板生成并提供html。 And that's all, next, you can use Vue (client side framework) to develop anything you want. 接下来,您可以使用Vue(客户端框架)开发您想要的任何内容。

So, what you can do is, create a .jade template (.pug) like this: 所以,你可以做的是,创建一个.jade模板(.pug),如下所示:

...
    body
        p {{message}}

    script(src='path/to/vue.js')
    script.
        const app = new Vue({
            el: 'body',
            data(){
                return {
                    message: 'Hello World!'
                }
            }
        });

It is a simple example. 这是一个简单的例子。 The important thing is that you just are serving a .jade like always, and then, you include the vue library to use it. 重要的是你只是像往常一样提供.jade,然后,你包含vue库来使用它。

Later, you can approach a better front-end development using Gulp and Webpack-Stream tools to compile .vue files. 稍后,您可以使用Gulp和Webpack-Stream工具进行更好的前端开发,以编译.vue文件。

A gulpfile.js like this, to compile all scripts of your app: 像这样的gulpfile.js ,用于编译应用程序的所有脚本:

gulp.task('scripts', () => {
    return gulp.src("resources/assets/js/main.js")
        .pipe(named())
        .pipe(webpack(require('./webpack.config')))
        .pipe(sourcemaps.init({loadMaps: true}))
        .pipe(through.obj(function (file, enc, cb) {
            // Dont pipe through any source map files as it will be handled
            // by gulp-sourcemaps
            var isSourceMap = /\.map$/.test(file.path);
            if (!isSourceMap) this.push(file);
            cb();
        }))
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest("./public/js"))
        .pipe(notify("Scripts compiled"));
});

The content of your webpack.config.js could be: 您的webpack.config.js的内容可能是:

module.exports = {
    devtool: 'source-map',
    module: {
        loaders: [
            { test: /\.vue/, loader: 'vue' },
        ],
    },
};

*Note that you will need the vue-loader package to let Webpack compile .vue files *请注意,您需要使用vue-loader包让Webpack编译.vue文件

By this way you will be able to develop a complete Express & Jade + VueJS application. 通过这种方式,您将能够开发出完整的Express&Jade + VueJS应用程序。

I hope it helps. 我希望它有所帮助。

You need to compile jade file into HTML. 您需要将jade文件编译为HTML。 I used prepros for compiling. 我用prepros进行编译。 But I already transfer to haml instead of jade. 但我已经转移到haml而不是玉。 But this will work perfectly. 但这将完美地运作。

for inline javascript. 对于内联javascript。 Use this 用这个

script.
    if(usingJade)
        console.log('you are awesome')
    else
        console.log('you are not awesome');

In general case - using of webpack is already enough for goal. 一般情况下 - 使用webpack已经足够了。 I have similar case before with entry jade template and .vue files. 我之前有类似的案例,用于输入jade模板和.vue文件。 Based on this example 基于这个例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM