前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Vue 中使用Pug

Vue 中使用Pug

作者头像
码农笔录
发布2020-08-24 12:47:35
1.1K0
发布2020-08-24 12:47:35
举报
文章被收录于专栏:码农笔录码农笔录

原文地址 https://www.aiprose.com/blog/126

1 介绍 pug 是一种前端模板引擎,原名 jade

可用来生成 HTML,它的写法类似于 CSS

这里先简单举几个例子

代码语言:javascript
复制
#hello
<div id="hello"></div>
代码语言:javascript
复制
a.link-button Link
<a class="link-button">Link</a>

易理解,同时极大的简约了我们的代码。

2 安装 2.1 下载

代码语言:javascript
复制
npm i -D pug pug-html-loader pug-plain-loader
或者
yarn add pug pug-html-loader pug-plain-loader

2.2 配置 // vue.config.js

代码语言:javascript
复制
module.exports = {
    chainWebpack: config => {
      config.module.rule('pug')
        .test(/\.pug$/)
        .use('pug-html-loader')
        .loader('pug-html-loader')
        .end()
  }
}

2.3 使用,注意要加 lang=“pug”

代码语言:javascript
复制
<template lang="pug">
    div.hello
        h1 Hello World
</template>

3 实践 下面将拿出实际项目中的一些代码进行改造

原代码

代码语言:javascript
复制
<template>
    <div class="gallery">
        <div class="btn-group">
            <span>选择相册:</span>
            <Select v-model="cataId" style="width:200px">
                <Option v-for="item in catalogs" :value="item.id" :key="item.id" clearable>{{ item.name }}</Option>
            </Select>
            <Button type="info">上传照片</Button>
        </div>
        <vue-waterfall-easy :imgsArr="imgsArr" @scrollReachBottom="loadMore" ref="waterfall"></vue-waterfall-easy>
    </div>
</template>

改造后 12行代码变成9行,标签完全简化

代码语言:javascript
复制
<template lang="pug">
    .gallery
        .btn-group
            span 选择相册:
            Select(v-model="cataId" style="width:200px")
                Option(v-for="item in catalogs" :value="item.id" :key="item.id" clearable) {{ item.name }}
            Button(type="info") 上传照片
        vue-waterfall-easy(:imgsArr="imgsArr" @scrollReachBottom="loadMore" ref="waterfall")
</template>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-08-21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档