相关疑难解决方法(0)

“v-model”指令无法更新迭代变量本身

我读了一篇关于Renderless Components的文章,它通过 $scopedSlots 属性将一个组件拆分为一个展示组件(视图部分)和一个无渲染组件(逻辑部分)。这是一个简单的标签组件。当您按 enter 时,您将添加一个新标签

<!DOCTYPE html>
<html>
<head>
<script src="http://vuejs.org/js/vue.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <div id="app">
    <custom-component v-model="tags">
      <div slot-scope="{tags,addTag,newTag}">
        <span v-for="tag in tags">
          {{tag}}
        </span>
        <input type="text" @keydown.enter.prevent="addTag" v-model="newTag">
      </div>
    </custom-component>
  </div>

   <script>
     Vue.component('custom-component',{
        props:['value'],
        data(){
          return {
            newTag:''
          }
        },
        methods:{
          addTag(){
            this.$emit('input',[...this.value,this.newTag])      
            this.newTag = ''
          }
        },
        render(h){
          return this.$scopedSlots.default({
            tags:this.value,
            addTag:this.addTag,
            newTag:this.newTag
          })
        }
      })


      new Vue({
        el:'#app',
        data:{
        tags:[
         'Test',
         'Design'
         ]
        }
      })



   </script> …
Run Code Online (Sandbox Code Playgroud)

javascript vue.js

7
推荐指数
2
解决办法
1万
查看次数

标签 统计

javascript ×1

vue.js ×1