当前位置:   article > 正文

Vue开发组件如何将数据和视图分离_vue死数据分离处理

vue死数据分离处理

一、函数组建

  1. <script>
  2. export default {
  3. props: {
  4. value: {
  5. type: Array,
  6. },
  7. },
  8. data() {
  9. return {
  10. newTag: '',
  11. }
  12. },
  13. methods: {
  14. addTag() {
  15. if (this.newTag.trim().length === 0 || this.value.includes(this.newTag.trim())) {
  16. return
  17. }
  18. this.$emit('input', [...this.value, this.newTag.trim()])
  19. this.newTag = ''
  20. },
  21. removeTag(tag) {
  22. this.$emit('input', this.value.filter(t => t !== tag))
  23. },
  24. },
  25. render() {
  26. return this.$scopedSlots.default({
  27. tags: this.value,
  28. addTag: this.addTag,
  29. removeTag: this.removeTag,
  30. inputEvents: {
  31. input: e => {
  32. this.newTag = e.target.value
  33. },
  34. keydown: e => {
  35. if (e.keyCode === 13) {
  36. e.preventDefault()
  37. this.addTag()
  38. }
  39. },
  40. },
  41. inputAttrs: {
  42. value: this.newTag,
  43. },
  44. })
  45. },
  46. }
  47. </script>

二、消费组建

  1. <template>
  2. <div id="app layout">
  3. <tagsInput v-model="tags">
  4. <div class="input-tags-wrap" slot-scope="{inputAttrs,inputEvents,tags,removeTag}">
  5. <span class="tag" v-for="tag in tags" :key="tag">
  6. <span>{{tag}}</span>
  7. <button class="tag-input-remove" @click="removeTag(tag)">x</button>
  8. </span>
  9. <input type="text" class="tag-input" v-bind="inputAttrs" v-on="inputEvents" placeholder="Add tag..." >
  10. </div>
  11. </tagsInput>
  12. <tags-input v-model="tags" style="margin-top:50px;margin-bottom:50px;">
  13. <div class="input-tags-wrap" slot-scope="{inputAttrs,inputEvents,tags,removeTag,addTag}">
  14. <div class="tags-wrap-header">
  15. <input type="text" v-bind="inputAttrs" v-on="inputEvents" placeholder="Add tag..." ><button class="tag-input-add" @click="addTag">添加</button>
  16. </div>
  17. <div>
  18. <dl>
  19. <dd v-for="tag in tags " :key="tag">{{tag}}
  20. <button class="tag-input-remove" @click="removeTag(tag)">x</button>
  21. </dd>
  22. </dl>
  23. </div>
  24. </div>
  25. </tags-input>
  26. <button @click="submit">提交</button>
  27. </div>
  28. </template>
  29. <script>
  30. import tagsInput from './components/tagsInput.vue'
  31. export default {
  32. name: 'app',
  33. data(){
  34. return {
  35. tags:['Helllo World','Lange']
  36. }
  37. },
  38. components: {
  39. tagsInput
  40. },
  41. methods:{
  42. submit(){
  43. console.log(this.tags)
  44. }
  45. }
  46. }
  47. </script>
  48. <style>
  49. .layout{
  50. width: 100%;
  51. display: flex;
  52. justify-content: center;
  53. height: 100vh;
  54. }
  55. .input-tags-wrap{
  56. width: 400px;
  57. display: flex;
  58. flex-wrap: wrap;
  59. border:1px solid #eee;
  60. background-color: #fff;
  61. padding: 5px;
  62. }
  63. .input-tags-wrap .tag-input{
  64. border:none;
  65. outline: transparent;
  66. margin-left: 5px;
  67. }
  68. .input-tags-wrap .tag{
  69. background: rgb(219, 148, 15);
  70. color: #fff;
  71. padding: 5px;
  72. border-radius: 5px;
  73. margin: 5px;
  74. }
  75. .tag-input-remove{
  76. outline: transparent;
  77. font-size: 16px;
  78. color: #fff;
  79. border: none;
  80. border-radius: 50%;
  81. margin-left: 5px;
  82. cursor:pointer;
  83. background-color: rgb(7, 230, 118);
  84. }
  85. .tags-wrap-header{
  86. display: flex;
  87. height: 40px;
  88. line-height: 40px;
  89. }
  90. .tags-wrap-header input{
  91. width: 200px;
  92. display: inline-flex;
  93. height: 32px;
  94. line-height: 32px;
  95. box-sizing: border-box;
  96. }
  97. .tags-wrap-header button{
  98. display: inline-flex;
  99. height: 32px;
  100. outline: none;
  101. padding: 0 12px;
  102. background: green;
  103. color: #fff;
  104. border-color: transparent;
  105. line-height: 32px;
  106. cursor: pointer;
  107. }
  108. </style>

三、实现的效果

 同一套逻辑,不同的展示方式。高效的复用组件

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号