我看到了一些关于 v-select 和 slot 的文档,但我真的不明白我是否可以将它应用于我的示例codepen。
我只需要获取选定的文本(而不是值),并在代码中的某处使用它:
new Vue({
el: "#app",
vuetify: new Vuetify(),
data: {
state: {},
selectedText: "",
states: [
{ value: "a", text: "alpha" },
{ value: "b", text: "beta" },
{ value: "g", text: "gamma" }
]
},
methods: {
change: (newValue) => {
// do something with the text
// "alpha", "beta", or "gama"
console.log(newValue);
}
}
});
<link href="https://cdn.jsdelivr.net/npm/@mdi/font@4.x/css/materialdesignicons.min.css" rel="stylesheet"/>
<link href="https://cdn.jsdelivr.net/npm/vuetify@2.2.20/dist/vuetify.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/babel-polyfill/dist/polyfill.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.2.20/dist/vuetify.min.js"></script>
<div id="app">
<v-app id="inspire">
<v-container fluid>
<label>my selected text is: {{state}}</label>
<v-row align="center">
<v-col cols="3">
<v-select v-model="state" :items="states" @change="change" :text="selectedText"></v-select>
</v-col>
</v-row>
</v-container>
</v-app>
</div>