基于Vue搭建本身的组件库(1)

本项目演示地址:https://husilang.github.io/zm-uijavascript

项目参考文章:从零开始搭建Vue组件库 VV-UIcss

项目的初衷是学习怎么封装一个基于Vue的UI组件库,顺便记录每一个步骤,以及在此过程当中遇到的难点及体会。html

下面是我我的的一个项目搭建流程,但愿能帮助你们。vue

①脚手架初始化项目

使用 vue cli 3.0.0版本以上,在node或cmd中输入如下命令建立项目java

vue create project-name

选择 Manually select features
选择Babel,Router,Vuex,CSS Pre-processors,Unit Testing
选择Sass/SCSS(with node-sass)
选择Mocha + Chai
选择In dedicated config files
以上来安装(按需选择)
运行以下命令:node

cd project-name
npm run serve

看项目是否成功启动,启动成功恭喜你完成第一小步~webpack

②目录结构修改

src目录修改成examples,用来组件示例
在examples目录下,新建docs文件夹,用来编写各组件的文档
在项目下新增packages文件夹,用来存放封装的组件git

③增长配置文件

因为修改了目录结构,须要修改相关配置,这里参考vue cli官网。
在项目根目录下,增长配置文件vue.config.js,代码以下:github

const path = require("path");
module.exports = {
    pages: {
        index: {
            entry: "examples/main.js", // js入口文件修改
            template: "public/index.html",
            filename: "index.html"
        }
    },
    chainWebpack: config => {
        // 从新设置目录别名
        config.resolve.alias
            .set("@", path.resolve("examples"))
            .set("~", path.resolve("packages"));
        // 使 examples及packages目录下的js文件都加入编译
        config.module
            .rule("js")
            .include.add("/packages")
            .end()
            .include.add("/examples")
            .end()
            .use("babel")
            .loader("babel-loader");
    },
}

运行npm run serve 查看是否能成功启动web

④编写第一个组件

接下来,就是要封装第一个组件,因为此文档的重点不是讲解组件怎么封装,那咱们就先写一个简单的组件用来测试好了。

编写组件前,我先去了解了一下css命名规范BEM

看了几篇关于BEM的文章后,仍是比较模糊,实践出真知,在之后的组件封装中,我会慢慢去使用这种规范。

在packages下封装一个test组件

在packages下新建一个文件夹test,test目录下新建index.js文件以及src/test.vue,以下:
在packages/test/src/test.vue中

<template>
    <div class="zm-test" :style="{backgroundColor: bgColor}"></div>
</template>

<script>
    export default {
        name: 'ZmTest',
        props: {
            bgColor: {
                type: String,
                default: "#ccc"
            }
        }
    }
</script>

<style lang="scss">
    .zm-test {
        display: inline-block;
        width: 10px;
        height: 10px;
        border-radius: 50%;
        animation: zm-opacity 2s infinite linear;
    }
    @keyframes zm-opacity{
        0% {opacity:1}
        10%,90%{opacity:.8}
        20%,80%{opacity:.7}
        30%,70%{opacity:.5}
        40%,60%{opacity:.3}
        50%{opacity:0}
        100%{opacity:.95}
    }
</style>

在packages/test/index.js中

import ZmTest from './src/test.vue'

// 支持按需引用
ZmTest.install = function (Vue) {
  Vue.component(ZmTest.name, ZmTest);
};

export default ZmTest;

在packages下新建index.js

在packages/index.js中

import ZmTest from './test/index'

const components = [
    ZmTest
];

const install = function (Vue) {
  if (install.installed) return;
  components.map(component => Vue.component(component.name, component));
};

if (typeof window.Vue !== "undefined" && window.Vue) {
    install(window.Vue);
}

export default {
    install,
    ZmTest
}

在examples下引用示例

在examples/main.js中

// do something...

import ZmUI from '../packages/index'

Vue.use(ZmUI);

// do something...

在examples/views/Home.vue中引用test组件

<template>
  <div class="home">
    <img alt="Vue logo" src="../assets/logo.png">
    <HelloWorld msg="Welcome to Your Vue.js App"/>
    <zm-test bgColor="blue"></zm-test>
  </div>
</template>

<script>
// @ is an alias to /examples
import HelloWorld from '@/components/HelloWorld.vue'

export default {
  name: 'home',
  components: {
    HelloWorld
  }
}
</script>

启动程序后,在"http://localhost:8080/#/home"能够看到咱们封装的test组件能正常使用了~

⑤编写第一个组件的文档

小白对md的写法一窍不通...因此先去了解了md文件的常见语法,参考文章:如何写md格式的文档
此过程分为两步,第一步使.md文件正常展现,能够看到运行结果以及代码展现,第二步封装一个代码展现的组件,使起能够展开收起,高亮显示。

编写文档

在examples/docs下新建一个test.md文件,随意写点介绍,内容以下:

# 测试组件

测试组件是用来测试md文档是否能在项目中像vue文件同样正常展现代码运行结果

运行结果以下
<zm-test bgColor="red"></zm-test>

代码以下
`<zm-test bgColor="red"></zm-test>`

安装工具

对md文件须要一个编译工具--vue-markdown-loader
在本项目下,用命令行工具运行如下命令

npm run vue-markdown-loader -D

修改配置文件

修改vue.config.js,使md文件能像vue文件同样在项目里展现

// do something...

module.exports = {
    // do something...

    chainWebpack: config => {
        // do something...

        // 使用vue-markdown-loader
        config.module.rule("md")
            .test(/\.md/)
            .use("vue-loader")
            .loader("vue-loader")
            .end()
            .use("vue-markdown-loader")
            .loader("vue-markdown-loader/lib/markdown-compiler")
    },

    // do something...
}

添加路由

在router.js里添加测试文档的路由

// do something...

export default new Router({
    routes: [
        {
            path: '/',
            name: 'home',
            component: Home
        },
        {
            path: '/about',
            name: 'about',
            component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
        },
        {
            path: '/test',
            name: 'test',
            component: () => import('../docs/test.md')
        }
    ]
})

查看结果

从新启动程序(注意,只要修改了vue.config.js文件,程序都须要从新启动),打开"http://localhost:8080/#/test"能够看到文字代码展现以及一个小红点在闪烁,表明第一步完美结束~

封装代码展现组件

开始第二步。
以上,咱们完成了md文件的正常展现,可是咱们能够将代码展现作得更好,封装一个代码展现的组件,使示例代码能够展开收起,代码高亮等。
感兴趣的能够了解一下markdown-it
我还在了解中,之后会更新上。

⑥让项目在github上展现

参考文章:http://www.javashuo.com/article/p-cmsxosab-ec.html

项目已经完成一小半了,我已经火烧眉毛将它传到github上记录下来。
在本地我能直观地看到个人项目成果了,如今但愿能在github上有个演示地址,让别人也能直观地看到个人项目展现。经过github文档及相关资料搜索,我找到了解决方案。以下:

安装工具

在项目下安装gh-pages工具

npm install gh-pages -D

增长部署命令

package.json文件修改scripts命令

"scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "test:unit": "vue-cli-service test:unit",
    "predeploy": "npm run build",
    "deploy": "gh-pages -d dist"
  },

修改配置

vue.config.js须要修改publicPath配置

module.exports = {
    // do something...
    publicPath: process.env.NODE_ENV === 'production' ? '/project-name/' : '/', //因为个人项目在github上名为zm-ui,因此个人project-name为zm-ui
    // do something...
}

打包部署

在node或cmd运行如下命令

npm run deploy

看到"Published"恭喜你部署成功,接下来能够到github上查看你的项目,是否多了一个分支gh-pages

在github上修改项目设置

在github你的项目下,找到"Settings"下的"GitHub Pages", "Source"选择"gh-pages branch"保存成功后,就拥有演示地址了~~


 

以上,未完待续...

相关文章
相关标签/搜索