vueコンポーネント---リングプログレスバーコンポーネント

5913 ワード

プロジェクトをするときは、コンポーネントライブラリを1セットしか使用しないほうがいいですが、多くの場合、私たちのコンポーネントライブラリには必要なコンポーネントがありません.このとき、私たちは自分でコンポーネントを書く必要があります.vuxにはリング進捗バーコンポーネントがないので、自分で書く必要があります.资料を探した后に1つのとても良い実现の方式を発见して、svgを通じて実现して、以前の时少しsvgを学んだことがありますが、あまり深く理解したことがありません..今から見れば本当に罪だ,参考リンクを与えるhttps://segmentfault.com/a/1190000008149403著者らは2つの方法を用いて,2つ目を選択し,簡単で拡張性が高いことが分かった.svgが見えるとcanvasのように描画したいと思います.原文はすでに详しく述べられているので、ここに自分で书いたコンポーネントを添付しましょう.手を伸ばして党たちも少し楽しくなる.
<template>
  <svg :height="option.size" :width="option.size" x-mlns="http://www.w3.org/200/svg">
    <circle  :r="option.radius" :cx="option.cx" :cy="option.cy" :stroke="option.outerColor" :stroke-width="option.strokeWidth" fill="none" stroke-linecap="round"/>
    <circle  id="progressRound" :stroke-dasharray="completenessHandle" :r="option.radius" :cx="option.cx" :cy="option.cy" :stroke-width="option.strokeWidth" :stroke="option.innerColor" fill="none" class="progressRound" />
  svg>
template>
<script> export default { name: 'CommonLoopProgress', props: { completeness: { type: Number, required: true, }, progressOption: { type: Object, default: () => {}, }, }, data () { return { } }, computed: { completenessHandle () { let circleLength = Math.floor(2 * Math.PI * this.option.radius) let completenessLength = this.completeness * circleLength return `${completenessLength},100000000` }, option () { //            let baseOption = { radius: 20, strokeWidth: 5, outerColor: '#E6E6E6', innerColor: '#FFDE00', } Object.assign(baseOption, this.progressOption) //          baseOption.cy = baseOption.cx = baseOption.radius + baseOption.strokeWidth baseOption.size = (baseOption.radius + baseOption.strokeWidth) * 2 return baseOption }, }, } script>
<style scoped lang='stylus'> @import '~stylus/_variables.styl'; @import '~stylus/_mixins.styl'; .progressRound { transform-origin: center; transform: rotate(-90deg); transition: stroke-dasharray 0.3s ease-in; } style>

原文のいくつかの悪い命名方法を修正し、私たちのコンポーネントを簡単に構成し、自由にすることができます.