【发布时间】:2018-11-28 01:31:18
【问题描述】:
我正在使用el-select 构建一个选择组件。像这样的:
<template>
//omitted code
<el-select v-model="filterForm.client"
filterable
remote
placeholder="Please enter a keyword"
:remote-method="filterClients"
:loading="loading">
<el-option
v-for="item in clientCandidates"
:key="item._id"
:label="item.name"
:value="item._id">
</el-option>
</el-select>
</template>
<scripts>
export default {
data() {
filterForm: {
client: ''
},
clientCandidates: [],
loading: false
},
methods: {
filterClients(query) {
if (query !== '') {
this.loading = true;
setTimeout(() => {
this.loading = false;
this.clientCandidates = [{_id: '1', name: 'foo'}, {_id: '2', name: 'bar'}];
}, 200);
} else {
this.clientCandidates = [];
}
}
}
}
</scripts>
到目前为止一切都很好,但是由于组件会出现在不同的页面中,所以我想提取一个自定义组件以避免重复。
根据guideline,
v-model="fullName"
等价于
v-bind:value="fullName"
v-on:input="$emit('input', $event)"
所以我像这样提取了选择组件:
<template>
<el-select
v-bind:value="clientId"
v-on:input="$emit('input', $event)"
placeholder="Filter by short name"
filterable="true"
remote="true"
:remote-method="filter"
:loading="loading">
<el-option
v-for="item in clients"
:key="item._id"
:label="item.name"
:value="item._id">
</el-option>
</el-select>
</template>
<scripts>
export default {
props: {
clientId: {
type: String,
required: true
}
},
data() {
return {
clients: [],
loading: false,
}
},
methods: {
filter(query) {
if (query !== '') {
this.loading = true;
setTimeout(() => {
this.loading = false;
this.clients = [{_id: '1', name: 'foo'}, {_id: '2', name: 'bar'}];
}, 200);
} else {
this.clients = [];
}
}
}
}
</scripts>
而父组件是这样的:
<select-client v-model="filterForm.clientId"></select-client>
选择下拉菜单工作正常,但不幸的是,选择没有显示我选择的选项,在我选择一个选项后它仍然是空的。我怀疑也许我应该将v-on:input 切换为'v-on:change',但它也不起作用。
更新
我创建了一个简单的例子,你可以克隆它here,请查看el-select-as-component 分支。运行
npm install
npm run dev
您将看到一个包含 3 种选择的简单页面:
左边是一个用 raw select 编写的自定义组件,它工作正常。
中间的一个是用el-select 编写的自定义组件,下拉列表仍然是空的,但是您可以在单击Filter 按钮后在控制台中看到filterForm.elClientId。这就是我提出这个问题的原因。
右边是一个普通的el-select,它工作正常。
【问题讨论】:
-
MCVE请?
-
@JacobGoh 谢谢,我已经用 github 示例更新了问题。
-
我 fork 你的 repo 并在我完成后让你成为 PR
标签: vuejs2 vue-component element-ui