为什么 `RouterView` 没有渲染任何组件?

huangapple go评论63阅读模式
英文:

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 &lt;RouterView&gt;. Thank you.

main.ts

import { createApp, VueElement } from &#39;vue&#39;;
import {router} from &#39;./data/router&#39;;
import AppWidget from &#39;./widgets/app.vue&#39;;
import * as types from &#39;./data/types&#39;;

globalThis.__VUE_OPTIONS_API__ = (process.env.NODE_ENV === &quot;development&quot;) as unknown as string;
globalThis.__VUE_PROD_DEVTOOLS__ = (process.env.NODE_ENV === &quot;development&quot;) as unknown as string;

const app = createApp(AppWidget);
app.use(router);
const root = app.mount(`.app`);

router.ts

import { createRouter, createWebHistory } from &#39;vue-router&#39;;
import * as constants from &#39;./settings&#39;;
import Home from &#39;../widgets/home.vue&#39;;
import JournalScreen from &#39;../widgets/journal/journalScreen.vue&#39;;

export const router = createRouter(
{
	history: createWebHistory(),
	routes:
	[
		{ path: &#39;/&#39;,                                    name: &#39;home&#39;,                               component: Home },
		{ path: &#39;/journal&#39;,                             name: &#39;journal&#39;,                            component: JournalScreen }
	]
});

app.vue

&lt;!-- JS -----------------------------------------------------&gt;
&lt;script setup lang=&quot;ts&quot;&gt;
import { RouterLink, RouterView, useRouter } from &#39;vue-router&#39;;
import {router} from &#39;../data/router&#39;;
import JournalScreen from &#39;./journal/journalScreen.vue&#39;;

router.push(&#39;/journal&#39;);

&lt;/script&gt;

&lt;!-- HTML -----------------------------------------------------&gt;
&lt;template&gt;  
    &lt;RouterView &gt;       
    &lt;/RouterView&gt;
&lt;/template&gt;

journal.vue

&lt;!-- JS -----------------------------------------------------&gt;
&lt;script setup lang=&quot;ts&quot;&gt;
import { RouterLink, RouterView } from &#39;vue-router&#39;;
import * as types from &#39;../../data/types&#39;;
import * as helper from &#39;../../logic/helper&#39;;
import {state} from &#39;../../data/state&#39;;
import JournalEntry from &#39;./journalEntry.vue&#39;;
import SignIn from &#39;../io/dropboxSignin.vue&#39;;
import SaveToCloud from &#39;../io/save.vue&#39;;

function mounted() {console.log(&#39;here&#39;);} //does not fire
&lt;/script&gt;

&lt;!-- HTML -----------------------------------------------------&gt;
&lt;template&gt;
	&lt;sign-in&gt;&lt;/sign-in&gt;	
	&lt;div&gt;
		Entries
	&lt;/div&gt;
	&lt;div v-for=&quot;item in state.entries&quot;&gt;
		&lt;journal-entry :entry=&quot;item&quot; :key=&quot;item.id&quot;&gt;&lt;/journal-entry&gt;
		&lt;br /&gt;
	&lt;/div&gt;
&lt;/template&gt;

答案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

&lt;RouterView&gt;
    &lt;!-- &lt;RouterLink to=&quot;/hi&quot;&gt;Go to Hi page&lt;/RouterLink&gt; --&gt;
&lt;/RouterView&gt;

to

&lt;RouterView&gt;
&lt;/RouterView&gt;

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

&lt;RouterView&gt;
    &lt;!-- &lt;RouterLink to=&quot;/hi&quot;&gt;Go to Hi page&lt;/RouterLink&gt; --&gt;
&lt;/RouterView&gt;

to

&lt;RouterView&gt;
&lt;/RouterView&gt;

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 &#39;./data/router&#39;; 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 &lt;script setup&gt;

In your case, this code in app.vue:

import {router} from &#39;./data/router&#39;;
router.push(&#39;/journal&#39;);

Should be replaced with:

import { useRouter } from &#39;vue-router&#39;

const router = useRouter()
router.push(&#39;/journal&#39;);

There is also one other issue with using mounted instead of onMounted (which also needs to be imported)

journal.vue

&lt;script setup&gt;
import { onMounted } from &#39;vue&#39;

onMounted(() =&gt; {
  console.log(&#39;here&#39;);
})
&lt;/script&gt;

huangapple
  • 本文由 发表于 2023年4月1日 01:07:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75901062.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定