开发 Vue 组件

随着 Vue 越来越火热, 相关组件库也非常多啦, 只用轮子怎么够, 还是要造起来!!!

1 环境配置

首先使用 vue-cli 脚手架, 生成基本项目;

npm install vue-cli -g
# mvue 是本次项目名称
vue init webpack mvue

如果要开发一个 npm 安装包, 还修改 package.json 的 main 选项

"main": "dist/mvue.js"

意思是我们将提供编译压缩后 mvue.js 文件给其他开发者使用;

2. webpack

在前端领域内, 可谓是没 webpack 不成活; 手写太麻烦, 就直接给大家文件了, 以下是我们的 webpack.config.js 文件:

var path = require('path');
var webpack = require('webpack');

module.exports = {
    entry: {
        main: './src/mvue.js'
    },
    output: {
        path: path.resolve(__dirname, '../dist'),
        publicPath: '/dist/',
        filename: 'mvue.js',
        library: 'mvue',
        libraryTarget: 'umd',
        umdNamedDefine: true
    },
    externals: {
        vue: {
            root: 'Vue',
            commonjs: 'vue',
            commonjs2: 'vue',
            amd: 'vue'
        }
    },
    resolve: {
        extensions: ['', '.js', '.vue']
    },
    module: {
        loaders: [{
            test: /\.vue$/,
            loader: 'vue'
        }, {
            test: /\.js$/,
            loader: 'babel',
            exclude: /node_modules/
        }, {
            test: /\.css$/,
            loader: 'style!css!autoprefixer'
        }, {
            test: /\.less$/,
            loader: 'style!css!less'
        }, {
            test: /\.(gif|jpg|png|woff|svg|eot|ttf)\??.*$/,
            loader: 'url?limit=8192'
        }, {
            test: /\.(html|tpl)$/,
            loader: 'vue-html'
        }]
    },
    plugins: [
        new webpack.DefinePlugin({
            'process.env': {
                NODE_ENV: '"production"'
            }
        }),
        new webpack.optimize.UglifyJsPlugin({
            compress: {
                warnings: false
            }
        }),
        new webpack.optimize.OccurenceOrderPlugin()
    ]
}

重点是 entry 和 output, 指定入口文件和输出文件;
因为我们要写的是分页组件, 所以已经在 components 目录下创建了 page.vue;
接下来 我们编写 webpack 入口文件:

import Page from './components/page';

const mvue = {
  Page
};

// 导出 install 函数
// Vue.use() 会调用这个函数
const install = function(Vue, opts = {}) {
  // 如果安装过就忽略
  if (install.installed) return;

  // 指定组件 name
  Vue.component(Page.name, Page);
}

// 自动安装 方便打包成压缩文件, 用<script scr=''></script>的方式引用
if (typeof window !== 'undefined' && window.Vue) {
  install(window.Vue);
}

// 把模块导出
module.exports = {
  install,
  Page
}

在入口文件中, 我们提供了对外公开的组件, install 函数, install 在 vue 组件开发中至关重要, vue 组件的调用方式是 Vue.use(***), 这个函数就会调用组件的 install 函数;
接下来就是各种组件的编写了!
以分页组件为例子, 首先我们要指定 props, 并对其类型进行验证,

  props: {
    current: {
      type: Number,
      default: 1
    },
    total: {
      type: Number,
      default: 1
    }, 
    currentChange: {
      type: Function
    }
  }

一共接受三个参数 分别是:

当前索引, 总条目, 回调函数

export default {
  name: 'mvue-page',
  props: {
    current: {
      type: Number,
      default: 1
    },
    total: {
      type: Number,
      default: 1
    }, 
    currentChange: {
      type: Function
    }
  },
  mounted () {
    this.insertPage()
  },

  data () {
    return {
      currentIndex: this.current,
      pageShowArray: [],
      displayCount: 7
    }
  },

  methods: {
// 翻页
    insertPage () {
      let self = this
      self.pageShowArray = []

      for (var i = 1; i <= self.total; i++) {
        this.pageShowArray.push(i)
      }
      // 小型分页
      if (this.total <= this.displayCount) { return; }
      let begin = this.currentIndex - 3 
      let end = this.currentIndex + 3

      begin = begin <= 1 ? 1 : begin
      end = end <= this.displayCount ? this.displayCount : end
      begin = begin >= this.total - this.displayCount ? this.total - this.displayCount : begin
      end = end >= this.total ? this.total : end

      let arr = this.pageShowArray.slice(begin - 1, end)
      this.$set(this, 'pageShowArray', arr)
    },

// 上一页
   pre () {
      if (this.currentIndex <= this.displayCount) {return;}
      this.setIndex(this.currentIndex - this.displayCount)
      this.insertPage()
    },
// 下一页
    next () {
      if (this.currentIndex >= this.total) {return;}
      this.setIndex(this.currentIndex + this.displayCount)
      this.insertPage()
    },
// item 点击
    itemClick (current) {
      this.setIndex(current)
      this.insertPage()
// 触发回调
      this.currentChange(current)
    },
    setIndex (current) {
      let temp = current
      if (temp <= 1) { temp = 1}
      if (temp >= this.total) { temp = this.total}
      this.$set(this, 'currentIndex', temp)
    }
  }
}

调用方式

...  html code
<mvue-page :current="2" :total="40" :currentChange='currentChange'></mvue-page>

... js code
// 两种方式选一个即可
// 按需加载
import {Page} from 'mvue'
Vue.use(Page)
// 全部加载
import mvue from '../dist/mvue.js'
Vue.use(mvue);

如上, 一个简单的分页组件已经有些模样, 接下来使用 webpack 打包;

// 对应 config 文件
webpack webpack.config.js

发布

npm publish
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
为了让后端开发人员能够更轻松地开发 Vue 组件,可以采取以下几个方法: 1. 学习基本的Vue知识:后端开发人员需要先学习一些基本的 Vue 知识,包括Vue的生命周期、组件通信、模板语法等。可以通过官方文档、在线教程或者视频教程来学习。 2. 使用可视化编辑器:可视化编辑器可以帮助后端开发人员更直观地创建和编辑 Vue 组件。这种工具通常提供拖拽和可视化设置等功能,使组件开发过程更简单和快速。 3. 利用UI库和组件库:使用现有的 UI库和组件库可以减少组件开发时间和工作量。这些库提供了丰富的组件和样式,后端开发人员可以直接使用它们来构建界面,而无需从零开始编写。 4. 制定标准的组件模板和规范:定义一套标准的组件模板和规范,使后端开发人员能够按照统一的方式来创建和管理组件。这有助于保持代码的一致性和可维护性。 5. 提供详细的文档和示例:编写清晰、详细的文档和示例代码,包括组件的使用方法、参数说明、示例演示等。这可以帮助后端开发人员更好地理解和使用组件。 6. 进行培训和知识分享:组织一些培训或知识分享会,让后端开发人员有机会学习和分享关于 Vue 组件开发的经验和技巧。这有助于提高整个团队的开发能力和效率。 通过以上措施,可以帮助后端开发人员更轻松地进行 Vue 组件开发,使他们能够快速上手并参与到前端开发中。同时,也可以促进前后端协作,提高整个团队的开发效率和质量。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值