将选定的值传递给 vuejs 函数
Pass selected value to vuejs function
如何将选定的值传递给 vuejs 函数?
v-model
我猜不会有帮助。
我需要在之后为过滤器设置值
item: items | orderBy sortKey reverse
其中 reverse
和 sortKey
是动态值。
html
<select class="sort-filter" v-on="change: sortBy(???)">
<option value="title asc">Title (A-Z)</option>
<option value="title desc">Title (Z-A)</option>
<option value="price asc">Price (Min. - Max.)</option>
<option value="price desc">Price (Max. - Min.)</option>
</select>
js
methods: {
sortBy: function (sortKey) {
console.log(sortKey)
}
}
你有几种方法可以做到。
编辑:改进 2)
可以像 2) 中那样使用 v-model,但不是直接在 orderBy 过滤器中使用值,而是可以使用 computed properties
computed: {
sortKey: {
get: function() {
return this.sorting.split(' ')[0]; // return the key part
}
},
sortOrder: {
get: function() {
return this.sorting.split(' ')[1]; // return the order part
}
}
}
这样,sortKey 和 sortOrder 将像普通 属性 一样在您的过滤器中可用:
v-repeat="items | orderBy sortKey sortOrder"
1) 使用javascript事件:
如果不指定任何参数,将自动传递原生事件对象
<select class="sort-filter" v-on:change="sortBy">
然后你可以像这样使用它:
methods: {
sortBy: function(e) {
console.log(e.target.value);
},
}
2) 使用 v-model
您可以添加 v-model 指令
<select name="test" v-model="sorting" v-on:change="sortBy">
这样 sorting
值将在每次更改时更新。
您可以在您的 ViewModel 的数据对象中添加此值以更加清晰:
data: {
sorting: null // Will be updated when the select value change
}
然后您可以像在您的方法中一样访问该值:
methods: {
sortBy: function() {
console.log(this.sorting);
},
}
如果你只需要更新sortKey
值,这个方法就没有必要了。
3) 其他奇怪的方式
您显然可以使用您的模型值作为参数。
<select name="test" v-model="sortKey" v-on:change="sortBy(sortKey)">
这是有效的,但我真的不明白这一点。
methods: {
sortBy: function(sortKey) {
console.log(sortKey);
},
}
如果您想将 selected 值传递给 vuejs 函数,请执行以下操作:
- 您需要在 select 标记中使用 v-model 指令作为 v-model = variableName
- 将该变量作为参数传递,如 @on-change=sortBy(variableName);
因此您的代码将如下所示:
<select class="sort-filter" v-model="sortingVariable" @on-change="sortBy(sortingVariable)">
<option value="title asc">Title (A-Z)</option>
<option value="title desc">Title (Z-A)</option>
<option value="price asc">Price (Min. - Max.)</option>
<option value="price desc">Price (Max. - Min.)</option>
</select>
如果您想使用动态值循环并且不能使用 v-model,这应该可以工作
<select name="option" v-on:change="selectedOption($event.target.value)">
<option value="0">SELECT</option>
<option v-for="i in 10" :value="i">{{ i }}</option>
</select>
尝试
<select name="option" @change="myFunction($event.target.value)">
<option v-for="item in list" :value="item.code" :key="item.code">{{ item.name }}</option>
</select>
// Function
myFunction(value) {
console.log(value);
}
如何将选定的值传递给 vuejs 函数?
v-model
我猜不会有帮助。
我需要在之后为过滤器设置值
item: items | orderBy sortKey reverse
其中 reverse
和 sortKey
是动态值。
html
<select class="sort-filter" v-on="change: sortBy(???)">
<option value="title asc">Title (A-Z)</option>
<option value="title desc">Title (Z-A)</option>
<option value="price asc">Price (Min. - Max.)</option>
<option value="price desc">Price (Max. - Min.)</option>
</select>
js
methods: {
sortBy: function (sortKey) {
console.log(sortKey)
}
}
你有几种方法可以做到。
编辑:改进 2)
可以像 2) 中那样使用 v-model,但不是直接在 orderBy 过滤器中使用值,而是可以使用 computed properties
computed: {
sortKey: {
get: function() {
return this.sorting.split(' ')[0]; // return the key part
}
},
sortOrder: {
get: function() {
return this.sorting.split(' ')[1]; // return the order part
}
}
}
这样,sortKey 和 sortOrder 将像普通 属性 一样在您的过滤器中可用:
v-repeat="items | orderBy sortKey sortOrder"
1) 使用javascript事件:
如果不指定任何参数,将自动传递原生事件对象
<select class="sort-filter" v-on:change="sortBy">
然后你可以像这样使用它:
methods: {
sortBy: function(e) {
console.log(e.target.value);
},
}
2) 使用 v-model
您可以添加 v-model 指令
<select name="test" v-model="sorting" v-on:change="sortBy">
这样 sorting
值将在每次更改时更新。
您可以在您的 ViewModel 的数据对象中添加此值以更加清晰:
data: {
sorting: null // Will be updated when the select value change
}
然后您可以像在您的方法中一样访问该值:
methods: {
sortBy: function() {
console.log(this.sorting);
},
}
如果你只需要更新sortKey
值,这个方法就没有必要了。
3) 其他奇怪的方式
您显然可以使用您的模型值作为参数。
<select name="test" v-model="sortKey" v-on:change="sortBy(sortKey)">
这是有效的,但我真的不明白这一点。
methods: {
sortBy: function(sortKey) {
console.log(sortKey);
},
}
如果您想将 selected 值传递给 vuejs 函数,请执行以下操作:
- 您需要在 select 标记中使用 v-model 指令作为 v-model = variableName
- 将该变量作为参数传递,如 @on-change=sortBy(variableName);
因此您的代码将如下所示:
<select class="sort-filter" v-model="sortingVariable" @on-change="sortBy(sortingVariable)">
<option value="title asc">Title (A-Z)</option>
<option value="title desc">Title (Z-A)</option>
<option value="price asc">Price (Min. - Max.)</option>
<option value="price desc">Price (Max. - Min.)</option>
</select>
如果您想使用动态值循环并且不能使用 v-model,这应该可以工作
<select name="option" v-on:change="selectedOption($event.target.value)">
<option value="0">SELECT</option>
<option v-for="i in 10" :value="i">{{ i }}</option>
</select>
尝试
<select name="option" @change="myFunction($event.target.value)">
<option v-for="item in list" :value="item.code" :key="item.code">{{ item.name }}</option>
</select>
// Function
myFunction(value) {
console.log(value);
}