【问题标题】:Cannot use 'in' operator to search for 'xxxxx' in undefined in vue.js无法使用“in”运算符在 vue.js 中的 undefined 中搜索“xxxxx”
【发布时间】:2020-01-03 11:42:07
【问题描述】:

我正在尝试创建一个将添加记录的模式表单。我能够显示数据中的默认值,但是一旦我尝试修改该字段,每当我尝试在输入框中键入更改时都会收到以下错误。

*vue.min.js:6 TypeError: Cannot use 'in' operator to search for 'fullname' in undefined
    at a.ke [as $set] (vue.min.js:6)
    at input (eval at Ya (vue.min.js:1), <anonymous>:3:2182)
    at He (vue.min.js:6)
    at HTMLInputElement.n (vue.min.js:6)
    at HTMLInputElement.Yr.o._wrapper (vue.min.js:6)*

在给定的下面我添加了我正在尝试创建的组件的代码: 请帮忙。

var bus = new Vue();

Vue.component('leagues_add', {

	props:	{
			show: Boolean,
			is_admin: Boolean,
			
		},

	data: function () {
			return {
				newLeague: {"fullname":"a", "notes":"b", "group_image_path": "c"} // remember to always enclose the fieldnames in doublequotes
			}
		},

	methods: {
			    closeModal() {
				   this.show = false;
				},
				showModal() {
							this.show = true;
				},
				addLeague() {
				event.preventDefault();

				var formData = this.toFormData(this.newLeague);

				axios.get("http://"+ window.location.hostname +"/db/leagues/index.php?action=create").then(function(response){

				if (response.data.error) {
					app.errorMessage = response.data.message;
				} else {
					app.leagues = response.data.leagues;
				}

			    });
				},
				toFormData(obj) {
					var fd = new FormData();
					for (var i in obj) {
						fd.append(i, obj[i]);
					}
					return fd;
				},

			}
	,
	template:  
	`
	<!-- Add new Leage -->

	<div>	
        <div class="text-center pb-3" >
		<button type="button" class="btn btn-outline-primary bg-success text-white" @click="showModal"><b class="" style="">Add League</b></button>
	</div>

	<transition name="modal">	
		<div id="overlay" v-show="this.show">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<h5 class="modal-title">Add League</h5>
						<button type="button" class="close" @click="closeModal"><span aria-hidden="true">×</span></button>
					</div>
					<div class="modal-body">
						<form action="#" method="POST">
							<div class="form-group">
								<input type="text" v-model="this.newLeague.fullname" class="form-control form-control-md" placeholder="Name of League">
							</div>
							<div class="form-group">
								<textarea v-model="this.newLeague.notes" rows="3" cols="100%" name="notes" class="form-control form-control-md" placeholder="Describe this league">
								</textarea>
							</div>
							<div class="form-group form-inline ">
								<div class="col-12 p-0">
									<input type="url" v-model="this.newLeague.group_image_path" class="col-5 pull-left form-control form-control-md" placeholder="Image URL">
												&nbsp;
								    <button class="col-4 btn btn-primary btn-md">Image</button>
								</div>
							</div>
							<div class="form-group">
								<button class="btn btn-info btn-block btn-md" @click="closeModal();addLeague();">Add this league</button>
							</div>
						</form>
					</div>
				</div>
			</div>			
		</div>
	</transition>
<div>
	
	`

});
<div class="row">
     <div class="col-md-12">LEAGUES SECTION</div>
</div>
<div class="row mt-2">      
    <div class="col-md-12">
       <leagues_add :show="true" />
    </div>
</div>

     

【问题讨论】:

  • 你能在 jsfiddle 或 codepen 上创建一个完整的例子吗?

标签: vue.js vue-component cdn


【解决方案1】:

问题出在这里:

v-model="this.newLeague.fullname"

您不能将this.v-model 一起使用。相反,它应该是:

v-model="newLeague.fullname"

您还应该删除模板中对this 的所有其他引用。在许多情况下,它们是无害的,但有时,例如 v-model,它们会引起问题。

下面的完整示例。请注意在编辑文本时第一个输入如何无法正常工作。

new Vue({
  el: '#app1',
  
  data () {
    return { newLeague: { fullname: 'League 1' } }
  }
})

new Vue({
  el: '#app2',
  
  data () {
    return { newLeague: { fullname: 'League 1' } }
  }
})
<script src="https://unpkg.com/vue@2.6.11/dist/vue.js"></script>

<div id="app1">
  <input type="text" v-model="this.newLeague.fullname">
  {{ newLeague.fullname }}
</div>

<div id="app2">
  <input type="text" v-model="newLeague.fullname">
  {{ newLeague.fullname }}
</div>

【讨论】:

  • 使用 this 完全没有必要,但如果你使用 this 它仍然有效。
  • @Anatoly 不正确。在某些情况下,它可能会导致失败。示例包括v-modelv-on 内部以及作用域插槽内部。
  • 我谈论的是这种情况,而不是所有可能的情况。
  • 如果我做错了,请告诉我。我在组件中声明了 data: function () {... 部分。是否应该像 @skirtle 的示例那样在组件之外声明它?有没有办法从组件中正确声明它?
【解决方案2】:

我按照@skirtle 编写数据部分的方式进行了操作。

我原来的语法是:

data: function () {
            return {
                newLeague: {"fullname":"a", "notes":"b", "group_image_path": "c"}           }
        }

data () {
    return { newLeague: { fullname: 'League 1' } }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-08
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    • 2013-01-15
    • 2014-10-31
    • 1970-01-01
    相关资源
    最近更新 更多