项目作者: dimensi

项目描述 :
Render format string like sprintf with vue components
高级语言: JavaScript
项目地址: git://github.com/dimensi/vue-sprintf-components.git
创建时间: 2018-03-23T20:55:40Z
项目社区:https://github.com/dimensi/vue-sprintf-components

开源协议:MIT License

下载


vue-sprintf-components

Sprintf with vue components

Demo

Edit Vue Template

Install

Via Yarn:

  1. yarn add vue-sprintf-components

Via NPM:

  1. npm i vue-sprintf-components

Quick start

  1. <template>
  2. <div>
  3. <VueSprintf text="component say: {0}">
  4. <ButtonUi>
  5. Hello!
  6. </ButtonUi>
  7. </VueSprintf>
  8. <!-- render: component say: <button>hello!</button> -->
  9. </div>
  10. </template>
  11. <script>
  12. import VueSprintf from "vue-sprintf-components";
  13. export default {
  14. components: {
  15. VueSprintf,
  16. ButtonUi: {
  17. template: '<button><slot></slot></button>'
  18. }
  19. }
  20. };
  21. </script>

Usage

The component takes 2 props: text and silence

  • text
    • type: String
    • required: true
      Any string with placeholders {0} and named placeholders {name}
  • silence
    • type: Boolean
      If silence false, if there are not enough slots for placeholders, there will be an error
  • placeholders
    • type: Array|Object
      Fallback placeholders if slots not enoght

      Example

      With args placeholders

      1. <VueSprintf text="component say: {0}">
      2. <ButtonUi>
      3. Hello!
      4. </ButtonUi>
      5. </VueSprintf>
      To render
      1. component say: <button>Hello!</button>

With named placeholders

  1. <VueSprintf text="component 'a' say: {a} and component 'b' say: {b}">
  2. <ButtonUi slot="a">
  3. Hello!
  4. </ButtonUi>
  5. <ButtonUi slot="b">
  6. Bye-Bye!
  7. </ButtonUi>
  8. </VueSprintf>

To render

  1. component 'a' say: <button>Hello!</button> and component 'b' say: <button>Bye-Bye!</button>

With named placeholders + fallback placeholders

  1. <VueSprintf text="component 'a' say: {a} and component 'b' say: {b}"
  2. :placeholders="{ b: 'Bye-bye' }">
  3. <ButtonUi slot="a">
  4. Hello!
  5. </ButtonUi>
  6. </VueSprintf>

To render

  1. component 'a' say: <button>Hello!</button> and component 'b' say: Bye-Bye!

With list placeholders + fallback placeholders

  1. <VueSprintf text="component '0' say: {0} and component '1' say: {1}"
  2. :placeholders="['Bye-bye']">
  3. <ButtonUi>
  4. Hello!
  5. </ButtonUi>
  6. </VueSprintf>

To render

  1. component '0' say: <button>Hello!</button> and component '1' say: Bye-Bye!