英文:
Why isn't `RouterView` rendering any components?
问题
All components/screens render correctly if I insert them in the HTML manually, and the URL changes with no errors, but no content is shown in <RouterView>
. Thank you.
main.ts
import { createApp, VueElement } from 'vue';
import { router } from './data/router';
import AppWidget from './widgets/app.vue';
import * as types from './data/types';
globalThis.__VUE_OPTIONS_API__ = (process.env.NODE_ENV === "development") as unknown as string;
globalThis.__VUE_PROD_DEVTOOLS__ = (process.env.NODE_ENV === "development") as unknown as string;
const app = createApp(AppWidget);
app.use(router);
const root = app.mount(`.app`);
router.ts
import { createRouter, createWebHistory } from 'vue-router';
import * as constants from './settings';
import Home from '../widgets/home.vue';
import JournalScreen from '../widgets/journal/journalScreen.vue';
export const router = createRouter(
{
history: createWebHistory(),
routes:
[
{ path: '/', name: 'home', component: Home },
{ path: '/journal', name: 'journal', component: JournalScreen }
]
});
app.vue
<!-- JS ----------------------------------------------------->
<script setup lang="ts">
import { RouterLink, RouterView, useRouter } from 'vue-router';
import { router } from '../data/router';
import JournalScreen from './journal/journalScreen.vue';
router.push('/journal');
</script>
<!-- HTML ----------------------------------------------------->
<template>
<RouterView>
</RouterView>
</template>
journal.vue
<!-- JS ----------------------------------------------------->
<script setup lang="ts">
import { RouterLink, RouterView } from 'vue-router';
import * as types from '../../data/types';
import * as helper from '../../logic/helper';
import { state } from '../../data/state';
import JournalEntry from './journalEntry.vue';
import SignIn from '../io/dropboxSignin.vue';
import SaveToCloud from '../io/save.vue';
function mounted() { console.log('here'); } // does not fire
</script>
<!-- HTML ----------------------------------------------------->
<template>
<sign-in></sign-in>
<div>
Entries
</div>
<div v-for="item in state.entries">
<journal-entry :entry="item" :key="item.id"></journal-entry>
<br />
</div>
</template>
英文:
All components/screens render correctly if I insert them in the HTML manually, and the URL changes with no errors, but no content is shown in <RouterView>
. Thank you.
main.ts
import { createApp, VueElement } from 'vue';
import {router} from './data/router';
import AppWidget from './widgets/app.vue';
import * as types from './data/types';
globalThis.__VUE_OPTIONS_API__ = (process.env.NODE_ENV === "development") as unknown as string;
globalThis.__VUE_PROD_DEVTOOLS__ = (process.env.NODE_ENV === "development") as unknown as string;
const app = createApp(AppWidget);
app.use(router);
const root = app.mount(`.app`);
router.ts
import { createRouter, createWebHistory } from 'vue-router';
import * as constants from './settings';
import Home from '../widgets/home.vue';
import JournalScreen from '../widgets/journal/journalScreen.vue';
export const router = createRouter(
{
history: createWebHistory(),
routes:
[
{ path: '/', name: 'home', component: Home },
{ path: '/journal', name: 'journal', component: JournalScreen }
]
});
app.vue
<!-- JS ----------------------------------------------------->
<script setup lang="ts">
import { RouterLink, RouterView, useRouter } from 'vue-router';
import {router} from '../data/router';
import JournalScreen from './journal/journalScreen.vue';
router.push('/journal');
</script>
<!-- HTML ----------------------------------------------------->
<template>
<RouterView >
</RouterView>
</template>
journal.vue
<!-- JS ----------------------------------------------------->
<script setup lang="ts">
import { RouterLink, RouterView } from 'vue-router';
import * as types from '../../data/types';
import * as helper from '../../logic/helper';
import {state} from '../../data/state';
import JournalEntry from './journalEntry.vue';
import SignIn from '../io/dropboxSignin.vue';
import SaveToCloud from '../io/save.vue';
function mounted() {console.log('here');} //does not fire
</script>
<!-- HTML ----------------------------------------------------->
<template>
<sign-in></sign-in>
<div>
Entries
</div>
<div v-for="item in state.entries">
<journal-entry :entry="item" :key="item.id"></journal-entry>
<br />
</div>
</template>
答案1
得分: 0
I found the problem. I didn't post all my commented code in the question because I thought it was irrelevant, but it turns out that if you have a commented line inside RouterView
it doesn't render anything.
Once I changed
<RouterView>
<!-- <RouterLink to="/hi">Go to Hi page</RouterLink> -->
</RouterView>
to
<RouterView>
</RouterView>
everything worked. I've logged a bug on the Vue site for this.
英文:
I found the problem. I didn't post all my commented code in the question because I thought it was irrelevant, but it turns out that if you have a commented line inside RouterView
it doesn't render anything.
Once I changed
<RouterView>
<!-- <RouterLink to="/hi">Go to Hi page</RouterLink> -->
</RouterView>
to
<RouterView>
</RouterView>
everything worked. I've logged a bug on the Vue site for this.
答案2
得分: -1
app.use(router);
使整个应用程序了解了Vue Router,所以你不需要在任何组件中像 import {router} from './data/router';
那样做任何事情。
使用Composition API的Vue Router与以前使用Vue 2/Options API可能有不同的语法。
最好彻底查看Vue Router的Composition API文档,其中介绍了它的用法。文档使用setup()
函数来使用Composition API,但对于<script setup>
来说语法是相同的。
在你的情况下,app.vue 中的这段代码:
import {router} from './data/router';
router.push('/journal');
应该替换为:
import { useRouter } from 'vue-router';
const router = useRouter();
router.push('/journal');
还有另一个使用mounted
而不是onMounted的问题(也需要导入)。
journal.vue
<script setup>
import { onMounted } from 'vue';
onMounted(() => {
console.log('here');
})
</script>
英文:
app.use(router);
makes your entire app aware of Vue Router, so you don't need to do anything like import {router} from './data/router';
within any of your components.
Vue Router with Composition API has a different syntax than maybe what you're used to if you used it previously with Vue 2/Options API
It'd be best to thoroughly review the Vue Router Composition API documentation which goes over its usage. The docs use Composition API with setup()
function but the syntax is the same for <script setup>
In your case, this code in app.vue:
import {router} from './data/router';
router.push('/journal');
Should be replaced with:
import { useRouter } from 'vue-router'
const router = useRouter()
router.push('/journal');
There is also one other issue with using mounted instead of onMounted (which also needs to be imported)
journal.vue
<script setup>
import { onMounted } from 'vue'
onMounted(() => {
console.log('here');
})
</script>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论