我正在构建一个应用程序,在其中创建了用于下拉菜单的自定义元素。我使用Bootstrap select来呈现下拉列表。当我更新选择的值时,一切都会改变,例如选项中的文本值。 但是,当我选择带有旧文本的选项时,就会显示新文本。参见下面的图片:
这是下拉组件的代码:
<template>
<select :data-row-id="rowId" :title="placeholder" :data-live-search="search" v-on:change="$emit('change', $event)" class="FT-picker">
<option v-if="before" v-for="item in before" :value="item.value">{{ item.name }}</option>
<option data-divider="true"></option>
<option v-for="option in options" :value="option[valueName]">{{ option[name] }}</option>
</select>
</template>
<script>
export default {
props: {
options: {
type: Array,
required: true
},
name: {
type: String,
required: true
},
valueName: {
type: String,
required: true
},
search: {
type: Boolean,
required: false,
default: false
},
placeholder: {
type: String,
required: false
},
before: {
type: Array,
required: false,
},
rowId: {
type: Number,
required: false
}
}
}
</script>
这是组件及其更新方式
<keep-alive>
<select-picker
:options="exisitngActs"
name="name"
valueName="id"
search
></select-picker>
</keep-alive>
已安装:
getExistingActs() {
var vm = this
axios.post('/get-all-acts', {
})
.then(function(response) {
for(var x in response.data) {
vm.$set(vm.exisitngActs, x, response.data[x])
}
})
.catch(function (error) {
console.log(error)
})
console.log(vm.exisitngActs)
},
知道我可以尝试什么吗?我的Google搜索出现在观察者或计算属性上,但无法正常工作。
答案 0 :(得分:0)
使用v-model指令将元素绑定到Object值。在v-for中,还应该声明一个v-bind:key值:
<template>
<select :data-row-id="rowId" :title="placeholder" :data-live-search="search" v-on:change="$emit('change', $event)" class="FT-picker">
<option v-if="before" v-for="item in before" v-model="before" v-bind:key="item.value" :value="item.value">{{ item.name }}</option>
<option data-divider="true"></option>
<option v-for="option in options" v-model="options" v-bind:key="option.valueName" :value="option[valueName]">{{ option[name] }}</option>
</select>
</template>
<keep-alive>
<select-picker
v-model="existingActs"
:options="exisitngActs"
name="name"
valueName="id"
search
></select-picker>
</keep-alive>
您需要在this.data导出的初始位置将现存Acts声明为空对象。
data() {
return {
existingActs: {},
};
},
通过在组件中使用v-model =“ existingActs”并将现存的Acts对象添加到初始返回中,它将元素绑定到Object并在其更改时对其进行更新。
更新
尝试使用服务器总线:
@ / stores / ServerBus.js只是一个空的Vue实例:
import Vue from 'vue';
export const ServerBus= new Vue();
@ / components / voteComponent.vue文件
// Import the ServerBus Component
import { ServerBus } from "@/stores/ServerBus.js";
// In the Created call, start listening to the
created() {
// Begins listening for events from the contentVoteBus
ServerBus.$on('eventName', (data) => {
this.parameter = data;
});
},
mounted() {
this.getNodeVotes(this.nid);
},
methods: {
async getNodeVotes(nid) {
// Fetches votes for the node
const response = await TestService.fetchNodeVotes(nid,this.userid);
// Emits the node vote data to the ServerBus
ServerBus.$emit('eventName', response.data);
},
async submitVote(nid, uid, vote, e) {
// Submits a users new vote to be inserted or updated in the database
// If the user has not voted on this item before, it will be an insert.
// If the user is changing their vote it will be an update.
// It also returns the updated vote data
const response = await TestService.submitContentVote(nid,uid,vote);
// Emits the node vote data to the ServerBus
ServerBus.$emit('eventName', response.data);
},
async deleteVote(nid, uid, e) {
// Deletes a users previous vote on a node
// It also returns the updated vote data
const response = await TestService.deleteContentVote(nid,uid,0);
// Emits the node vote data to the ServerBus
ServerBus.$emit('eventName', response.data);
},
},
这是生成UI的Vue:
<b-col class="thumb-button py-3 thumbs-up-button" cols="6">
<img v-if="voteData.userVoted === true && voteData.userVote === 1" src="@/assets/icons/grey-thumbs-up.png" alt="Remove Thumbs Up Vote" title="Remove Thumbs Up Vote" class="p-2 disabled" v-on:click="deleteVote(nid, userid, $event)" />
<img v-else src="@/assets/icons/green-thumbs-up.png" alt="Thumbs Up" title="Thumbs Up" class="p-2" v-on:click="submitVote(nid, userid, 1, $event)" />
</b-col>
<b-col class="thumb-button py-3 thumbs-down-button" cols="6">
<img v-if="voteData.userVoted === true && voteData.userVote === 0" src="@/assets/icons/grey-thumbs-down.png" alt="Remove Thumbs Down Vote" title="Remove Thumbs Down Vote" class="p-2 disabled" v-on:click="deleteVote(nid, userid, $event)" />
<img v-else src="@/assets/icons/red-thumbs-down.png" alt="Thumbs Down" title="Thumbs Down" class="p-2" v-on:click="submitVote(nid, userid, 0, $event)" />
</b-col>
答案 1 :(得分:0)
尝试在vm。$ set(...)调用之后将以下内容添加到“ mount”函数中:
vm.$nextTick(function(){ $('.FT-picker').selectpicker('refresh'); });
如果只获得该组件的
我有一个article with a more detailed explanation,它的示例损坏了并且可以正常工作。