vue實現(xiàn)聯(lián)動選擇
本文實例為大家分享了vue實現(xiàn)聯(lián)動選擇的具體代碼,供大家參考,具體內(nèi)容如下
因為項目需求,作者和作者頭像都是由后臺接口傳給前端的,所以我就選擇用select來實現(xiàn)
主要想法就是當選擇作者后,自動顯示頭像,(效果如下)

下面就分享下代碼(element):
頁面用的就是form表單
<el-form ref="goodsCircle" :model="goodsCircle" class="form-container"> ? ? ? ? ? ? ? <el-form-item ? ? ? ? ? ? label-width="80px" ? ? ? ? ? ? label="作者:" ? ? ? ? ? ? class="postInfo-container-item" ? ? ? ? ? ? prop="authorInfo" ? ? ? ? ? > ? ? ? ? ? ? <el-select ? ? ? ? ? ? ? filterable ? ? ? ? ? ? ? v-model="goodsCircle.authorInfo" ? ? ? ? ? ? ? remote ? ? ? ? ? ? ? placeholder="搜索用戶" ? ? ? ? ? ? ? @change="getAuthorImg" ? ? ? ? ? ? ? ? ? value-key="key" ? ? ? ? ? ? > ? ? ? ? ? ? ? <el-option ? ? ? ? ? ? ? ? v-for="item in authorArr" ? ? ? ? ? ? ? ? :key="item.key" ? ? ? ? ? ? ? ? :label="item.label" ? ? ? ? ? ? ? ? :value="item" ? ? ? ? ? ? ? /> ? ? ? ? ? ? </el-select> ? ? ? ? ? ? ? ? ? ? </el-form-item> ? ? ? ? ? ? <el-form-item prop="authorImg" label-width="80px" label="頭像" style="margin-bottom: 30px;"> ? ? ? ? <el-image ? ? ? ? ? v-model="goodsCircle.authorImg" ? ? ? ? ? :src="goodsCircle.authorImg" ? ? ? ? ? fit="cover" ? ? ? ? ? lazy ? ? ? ? ? style="width: 200px;height: 180px;" ? ? ? ? > ? ? ? ? ? <div slot="placeholder" class="image-slot"> ? ? ? ? ? ? 加載中 ? ? ? ? ? ? <span class="dot">...</span> ? ? ? ? ? </div> ? ? ? ? </el-image> ? ? ? </el-form-item> ?</el-form>
<script>
export default {
? data() {
? ? return {
? ? ? authorArr: [],
? ? ? goodsCircle: {
? ? ? ? authorInfo: {},
? ? ? ? author: "",
? ? ? ? authorImg: "",
? ?},
? ? };
? },
? methods: {
? ? //查詢發(fā)布者
? ? getAuthorList() {
? ? ? this.$api.operation
? ? ? ? .getAuthorList({
? ? ? ? ? status: this.listQuery.authorStatus
? ? ? ? })//這是接口
? ? ? ? .then(res => {
? ? ? ? ? if (res.data.code == 200) {
? ? ? ? ? ? let arr = [];
? ? ? ? ? ? res.data.result.forEach((res, index) => {
? ? ? ? ? ? ? arr[index] = {
? ? ? ? ? ? ? ? key: res.id,
? ? ? ? ? ? ? ? label: res.author,
? ? ? ? ? ? ? ? authorImg: res.authorImg
? ? ? ? ? ? ? };
? ? ? ? ? ? });
? ? ? ? ? ? this.authorArr = arr;
? ? ? ? ? }
? ? ? ? });
? ? },
? ? //change事件
? ? getAuthorImg(param) {
? ? ? this.goodsCircle.authorImg = param.authorImg;
? ? ? this.goodsCircle.author = param.label;
? ? }
? },
??
?
? created() {
? ? this.getAuthorList();
? }
};
</script>這樣就能實現(xiàn)效果了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue3報錯‘defineProps‘?is?not?defined的解決方法
最近工作中遇到vue3中使用defineProps中報錯,飄紅,所以這篇文章主要給大家介紹了關(guān)于Vue3報錯‘defineProps‘?is?not?defined的解決方法,需要的朋友可以參考下2023-01-01
Vue.config.js配置報錯ValidationError:?Invalid?options?object解
這篇文章主要給大家介紹了關(guān)于Vue.config.js配置報錯ValidationError:?Invalid?options?object的解決辦法,主要由于vue.config.js配置文件錯誤導(dǎo)致的,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-02-02
vue關(guān)于重置表單數(shù)據(jù)出現(xiàn)undefined的解決
這篇文章主要介紹了vue關(guān)于重置表單數(shù)據(jù)出現(xiàn)undefined的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09

