vue.js - 预选 VUE2 下拉菜单
问题描述
我有一个vue2
应用程序,我需要预先选择一个dropdown
,但对于我的生活,我无法弄清楚。
我已经查看并尝试了以下所有内容:
如何在 Vue.js 中使用 v-repeat 创建的选择中预选当前值
普通 HTML
<select ref="courierList" id="courierList" class="form-control" v-model="shippingDetails.selectedCourier" @change="selectedCourierDd">
<option :value="courier.name" v-for="(courier, i) in couriers" :key="i">
{{ courier.name }}
</option>
</select>
data() {
return {
couriers: [],
.... OTHER DATA ITEMS
}
},
beforeMount() {
this.fetchCouriers();
},
fetchCouriers() {
axios.get('/couriers')
.then((response) => {
this.couriers = response.data.couriers;
.... OTHER CODE
尝试代码> 1
将以下内容添加到option
selected="{{ courier.name === preselectedCourierName ? 'true' : 'false' }}">
也没有true
/false
尝试代码> 2
将以下内容添加到option
v-bind:selected="courier === preselectedCourierName"
和下面
data() {
return {
.... OTHER DATA ITEMS
preselectedCourier: [],
preselectedCourierName: ''
}
fetchCouriers() {
axios.get('/couriers')
.then((response) => {
this.couriers = response.data.couriers;
.... OTHER CODE
console.log('couriers', this.couriers[0])
this.preselectedCourier = this.couriers[0];
this.preselectedCourierName = this.preselectedCourier.name;
console.log('preselectedCourier', this.preselectedCourier)
console.log('preselectedCourierName', this.preselectedCourierName)
尝试代码> 3
<option :value="courier.name" v-for="(courier, i) in couriers" :key="i" :selected="courier.name === 'APC'">
没有错误,但下拉菜单仍未预先选择
解决方案
推荐阅读
- hive - 雪花 DATEDIFF 与 Hive
- c++ - 是否可以使用 AWS C++ SDK 下载预置的 url 文件
- java - 当我用我的 Android webview 拍照时,它会用 0B 保存它并且不会上传
- c# - 如何在没有唯一过滤数据的情况下获取导航属性?
- plugins - 如何结合 Laravel Mix 自动加载 vue3 组件(使用插件“unplugin-vue-components”)?
- svn - 如何将外部删除的文件标记为 Subversion 删除?
- c# - 如何反序列化具有不同类型属性的 XML 数组
- javascript - 加载新类别后 Javacsript 无法正常工作
- python - 我们如何使用 fastAPI 构建一个 API 来触发另一个 Python 文件执行查询,而不等待它的响应?
- sql-server - ETL 的变更捕获替代方案