Vue.js project error: Variable cannot be accessed before initialization
P粉897881626
2023-08-25 22:40:48
<p>I'm currently developing a Twitter clone using Vue 3. The source code for the same can be found here. </p>
<p><code>HomeView.vue</code>The code is as follows:</p>
<pre class="lang-html prettyprint-override"><code><template>
<div class="home">
<Tweet
v-for="tweet in tweets"
:key="tweet._id"
:tweet="tweet" />
</div>
</template>
<script>
import { ref } from 'vue';
import Tweet from '../components/Tweet';
import tweets from '../tweets';
export default {
setup () {
const tweets = ref(tweets);
return {
tweets,
Tweet
}
}
}
</script>
</code></pre>
<p>But after performing the same operation, I am getting the following error in the developer's console. </p>
<pre class="brush:php;toolbar:false;">Uncaught (in promise) ReferenceError: Cannot access 'tweets' before initialization</pre></p>
Not a composition API expert, but I wanted to take a look at the lifecycle a>, you can't use tweets directly like this because they are not usable directly. They will then be filled.
Although your template is synchronized, it will error because it cannot access something when it is (originally) undefined.
Making may be a solution, otherwise it may be with
watch
and suspense, not sure.PS: There are 2 more
tweets
variables here, which may cause some errors, please be careful.