diff --git a/src/progress/README.md b/src/progress/README.md index b7bd30132..76e84f983 100644 --- a/src/progress/README.md +++ b/src/progress/README.md @@ -61,3 +61,11 @@ Use `pivot-text` to custom text,use `color` to custom bar color. | text-color | Pivot text color | _string_ | `white` | | inactive | Whether to be gray | _boolean_ | `false` | | show-pivot | Whether to show text | _boolean_ | `true` | + +### Methods + +Use [ref](https://vuejs.org/v2/api/#ref) to get Progress instance and call instance methods. + +| Name | Description | Attribute | Return value | +| --- | --- | --- | --- | +| resize | Resize Progress when container element resized or visibility changed | - | - | diff --git a/src/progress/README.zh-CN.md b/src/progress/README.zh-CN.md index d8db3325c..3517867ae 100644 --- a/src/progress/README.zh-CN.md +++ b/src/progress/README.zh-CN.md @@ -69,3 +69,38 @@ Vue.use(Progress); | text-color | 进度文字颜色 | _string_ | `white` | | inactive | 是否置灰 | _boolean_ | `false` | | show-pivot | 是否显示进度文字 | _boolean_ | `true` | + +### 方法 + +通过 ref 可以获取到 Progress 实例并调用实例方法,详见[组件实例方法](#/zh-CN/advanced-usage#zu-jian-shi-li-fang-fa)。 + +| 方法名 | 说明 | 参数 | 返回值 | +| ------ | -------------------------------------------- | ---- | ------ | +| resize | 外层元素大小变化后,可以调用此方法来触发重绘 | - | - | + +## 常见问题 + +### 组件从隐藏状态切换到显示状态时,渲染不正确? + +Progress 组件在挂载时,会获取自身的宽度,并计算出进度条的样式。如果组件一开始处于隐藏状态,则获取到的宽度永远为 0,因此无法展示正确的进度。 + +#### 解决方法 + +方法一,如果是使用 `v-show` 来控制组件展示的,则替换为 `v-if` 即可解决此问题: + +```html + + + + +``` + +方法二,调用组件的 resize 方法来主动触发重绘: + +```html + +``` + +```js +this.$refs.progress.resize(); +``` diff --git a/src/progress/index.js b/src/progress/index.js index 9d90b94ba..1aef1dc4b 100644 --- a/src/progress/index.js +++ b/src/progress/index.js @@ -30,16 +30,17 @@ export default createComponent({ }, mounted() { - this.setWidth(); + this.resize(); }, watch: { - showPivot: 'setWidth', - pivotText: 'setWidth', + showPivot: 'resize', + pivotText: 'resize', }, methods: { - setWidth() { + // @exposed-api + resize() { this.$nextTick(() => { this.progressWidth = this.$el.offsetWidth; this.pivotWidth = this.$refs.pivot ? this.$refs.pivot.offsetWidth : 0; diff --git a/types/index.d.ts b/types/index.d.ts index 38a46936a..3e6a93bd3 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -19,6 +19,7 @@ import { List } from './list'; import { Locale } from './locale'; import { Notify } from './notify'; import { Picker } from './picker'; +import { Progress } from './progress'; import { Sku } from './sku'; import { Swipe } from './swipe'; import { SwipeCell } from './swipe-cell'; @@ -65,7 +66,6 @@ export class Pagination extends VanComponent {} export class Panel extends VanComponent {} export class PasswordInput extends VanComponent {} export class Popup extends VanComponent {} -export class Progress extends VanComponent {} export class PullRefresh extends VanComponent {} export class Radio extends VanComponent {} export class RadioGroup extends VanComponent {} @@ -110,6 +110,7 @@ export { Locale, Notify, Picker, + Progress, Sku, Swipe, SwipeCell, diff --git a/types/progress.d.ts b/types/progress.d.ts new file mode 100644 index 000000000..e54f734f9 --- /dev/null +++ b/types/progress.d.ts @@ -0,0 +1,5 @@ +import { VanComponent } from './component'; + +export class Progress extends VanComponent { + resize(): void; +}