Why does defineModel return undefinded? #8848
Answered
by
liulinboyi
wsy998
asked this question in
Help/Questions
-
<script lang="ts" setup>
import {NButton, NInput, NModal, NTable} from "naive-ui";
import {defineModel, ref} from "vue";
import {FindCountry, SaveCountry} from "../../wailsjs/go/main/App";
import {model} from "../../wailsjs/go/models.ts";
import CountryItem = model.CountryItem;
const kw = ref("")
let showAddDialog = defineModel<boolean>("show", {required: true, local: true});
const countries = ref<Array<model.CountryItem>>([])
const search = () => {
FindCountry(kw.value).then((res: model.CountryItem[]) => {
countries.value = res
})
}
console.log(showAddDialog)
const select = (c: model.CountryItem) => {
SaveCountry(c).then(() => {
console.log(showAddDialog)
// showAddDialog.value = false
emit("country", c)
})
}
const emit = defineEmits<{
(e: 'country', c: CountryItem): void
}>()
</script>
<template>
<n-modal :on-close="()=>showAddDialog=false" :show="showAddDialog" :show-icon="false"
block-scroll closeOnEsc preset="dialog" title="添加城市"
>
<template #header>
<div>添加城市</div>
</template>
<div>
<n-input v-model:value="kw" class="mb-2"></n-input>
<n-button @click="search">搜索</n-button>
<n-table>
<thead>
<tr>
<th>Id</th>
<th>城市名</th>
<th>经纬度</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="country in countries" :key="country.id">
<td>{{ country.id }}</td>
<td>{{ country.adm1 }}-{{ country.adm2 }}-{{ country.name }}</td>
<td>{{ country.lon }},{{ country.lat }}</td>
<td>
<n-button @click="select(country)">选择</n-button>
</td>
</tr>
</tbody>
</n-table>
</div>
</n-modal>
</template>
<style lang="sass" scoped>
</style> I use Vite.Am I using it wrong? Can you give an example. |
Beta Was this translation helpful? Give feedback.
Answered by
liulinboyi
Jul 28, 2023
Replies: 2 comments 8 replies
-
Beta Was this translation helpful? Give feedback.
2 replies
-
ReasonBecause the <CustomComponent
v-model:show="someValue"
/> is equal to <CustomComponent
:show="someValue"
@update:show="newValue => someValue = newValue"
/> If you set the and the code let showAddDialog = defineModel<boolean>("show", {required: true, local: true}); will compile to import { useModel as _useModel } from 'vue'
let showAddDialog = _useModel(__props, "show", { local: true });
Docs |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@wsy998 #8276