[英]Can't set Ref data property in Vue3, item undefined
Essentially I'm using Vue3 and Nuxt3.基本上我使用的是 Vue3 和 Nuxt3。 I'm trying to grab the:id param from the URL and if it matches an ID in a JSON it updates a ref data point called 'exists'.
我正在尝试从 URL 中获取:id 参数,如果它与 JSON 中的 ID 匹配,它会更新一个名为“exists”的参考数据点。 My console log shows all that is working.
我的控制台日志显示了所有工作。 The issue is this.exists is showing as undefined.
问题是 this.exists 显示为未定义。 I attempted the const self = this;
我尝试了 const self = this; trick, to no avail.
诡计,无济于事。
Full warning:完整警告:
500: Cannot set properties of undefined (setting 'exists')
<template>
<div class="single-hunt">
<div class="container">
Single Hunt
</div>
</div>
</template>
<script>
import hunt from '../../assets/api/hunts.json'
import { onMounted } from 'vue';
import { useRoute } from 'vue-router'
export default {
setup() {
const route = useRoute();
const id = route.params.id;
const exists = ref(false);
onMounted(() => {
for (let i = 0; i < hunt.length; i++) {
if (hunt[i].address == id) {
console.log('exists')
this.exists = true;
break;
}
}
})
return {
hunt,
exists
}
}
}
</script>
<styles src="../../assets/css/singlehunt.css"/>
You should import ref from vue: import { ref } from 'vue';
你应该从 vue 导入 ref:
import { ref } from 'vue';
https://vuejs.org/guide/essentials/reactivity-fundamentals.html#reactive-variables-with-ref https://vuejs.org/guide/essentials/reactivity-fundamentals.html#reactive-variables-with-ref
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.