gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 19:50:08 25 4
gpt4 key购买 nike

我读了一篇关于 Renderless Components 的文章,它通过 $scopedSlots 属性将组件拆分为展示组件( View 部分)和无渲染组件(逻辑部分)。这是一个简单的 Tag 组件。当您按 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>
</body>
</html>

但是,它不起作用,似乎newTag总是''(空字符串),当我使用SPA方式时,模拟器说“‘v-model’指令无法更新迭代变量‘newTag’本身”,这里是 demo on jsbin

解决方案是,正如文章中提到的,使用 :value 属性绑定(bind)和 @input 事件绑定(bind),而不是 v-model。 jsbin 上的演示

<!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,inputAttrs,inputEvents}">
<span v-for="tag in tags">
{{tag}}
</span>
<input type="text" v-bind="inputAttrs" v-on="inputEvents">
</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,
inputAttrs:{
value:this.newTag
},
inputEvents:{
input:(e) => {
this.newTag = e.target.value
},
keydown:(e) => {
if(e.keyCode === 13){
e.preventDefault()
this.addTag()
}
}
}
})
}
})


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



</script>
</body>
</html>

我不知道为什么 v-model 不起作用。

编辑

上面的问题已经得到了明确的回答,而我在阅读引用链接后又遇到了另一个问题,并且仍然是 v-model 不起作用的问题


<!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">
<base-test v-slot="sp">
<input type="text" v-model="sp.foo">
<div>{{ sp}}</div>
</base-test>
</div>
<script>
Vue.component('base-test', {
template: `
<div>
<slot :foo="foo"></slot>
</div>
`,
data(){
return{
foo: 'Bar',
}
}
});


// Mount
new Vue({
el: '#app',
});
</script>
</body>
</html>

正如我们所见,sp 是一个对象。为什么 v-model 这次似乎不起作用?

最佳答案

我的解决方案非常简单(请参阅v-model="tags[index]"):

不要这样做:

<template v-for="tag in tags">
<TagView :key="tag.key" v-model="tag" />
</template>

你应该这样做:

<template v-for="(tag, index) in tags">
<TagView :key="tag.key" v-model="tags[index]" />
</template>

原因是您无法将迭代对象tag传递到v-model进行修改。请查找有关此的更多信息:Iterating a list of objects with foreach

关于javascript - “v-model”指令无法更新迭代变量本身,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57974480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com