vue转换时间戳过滤器_在Vue 2中使用的方便的单位转换过滤器的集合

vue转换时间戳过滤器

单位 (vue-units)

A plugin for adding handy conversion filters to your Vue.js project. Based on the convert-units package made by @ben-ng.

一个插件,用于向您的Vue.js项目添加方便的转换过滤器。 基于@ ben-ng制作的convert-units软件包。

安装 (Installation)

Install vue-units with NPM from the vue-units NPM repository by running the following command:

与NPM从通过运行下面的命令VUE单元NPM库安装VUE单元

npm install vue-units --save

If you prefer to use Yarn, you can install it with the following command instead:

如果您更喜欢使用Yarn,则可以使用以下命令来安装它:

yarn add vue-units

Add it to your vue instance:

将其添加到您的vue实例:

import Vue from 'vue';
import VueUnits from 'vue-units';

Vue.use(VueUnits)

If you don't use ES6, you can also include it as a script locally:

如果您不使用ES6,也可以将其作为脚本包含在本地:

<script src="https://unpkg.com/[email protected]^1.0/vue-units.js"></script>

用法 (Usage)

筛选器 (Filters)

单元() (unit())

The filter unit(from, to, includeUnit) is added to your Vue instance, which makes it easy to convert between a set of units in your templates:

过滤器unit(from, to, includeUnit)被添加到您的Vue实例中,这使得在模板中的一组单元之间进行转换变得容易:

<template>
  <div id="app">
    <p>{{1500 | unit('m', 'km', true)}}</p>
  </div>
</template>

The above code will result in the following output:

上面的代码将导致以下输出:

1.5 km

If includeUnit is false, only the converted value will be returned:

如果includeUnit为false,则仅返回转换后的值:

1.5

To see a list of available conversion units, please refer to the official repository for convert-units.

要查看可用转换单位的列表,请参阅convert-units的官方存储库。

原型 (Prototype)

You can access the instance of convert-units anywhere in your Vue templates, which gives you access to the additional functions that the convert-units package provides:

您可以在Vue模板中的任何位置访问convert-units实例,从而可以访问convert-units包提供的其他功能:

this.$units(12000).from('mm').to('m');
// 12 Metres

this.$units(12000).from('mm').toBest();
// 12 Meters (the smallest unit with a value above 1)

this.$units(12000).from('mm').toBest({ exclude: ['m'] })
// 1200 Centimeters (the smallest unit excluding meters)

this.$units(12000).from('mm').toBest({ cutOffNumber: 10 });
// 900 Centimeters (the smallest unit with a value equal to or above 10)

this.$units(12000)from('mm').toBest({ cutOffNumber: 10 })
// 10 Meters (the smallest unit with a value equal to or above 10)

this.$units(12000).from('m').possibilities();
// ["mm", "cm", "m", "km", "in", "yd", "ft-us", "ft", "mi"]

this.$units().measures();
// [ 'length', 'mass', 'volume' ]

For additional methods, please refer to the official repository for convert-units.

有关其他方法,请参阅convert-units的官方存储库。

例: (Example:)
<script>
export default {
  props: {
      distance: {
          type: Number,
          default: 123456789
      }
  },
  computed: {
      shortDistance() {
          const obj = this.$units(this.distance).from('cm2').toBest({
              exclude: ['m2']
          });
          return `${obj.val} ${obj.unit}`;
      }
  }
}
</script>

翻译自: https://vuejsexamples.com/a-collection-of-handy-unit-conversion-filters-for-use-in-vue-2/

vue转换时间戳过滤器

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值