我的Vue路由器应用程序不会使用vue- v3 @4呈现组件,DOM上也不会显示任何内容,http://localhost:8080/view也不会显示对应于/view路径的组件。请给我引路。
我用Vue CLI创建了这个项目,然后安装了npm install vue-router,基本上我只是在./components/users中添加了一个新组件,还修改了main.js文件,仅此而已。
import AppView from "../AppView";
import AppRegister from "../AppRegister";
import { createRouter, createWebHistory,createMemoryHistory } from 'vue-router'
const routes = [
{
path: '/view',
name: 'view',
component: AppView
},
{
path: '/register',
name: 'register',
component: AppRegister
}
]
// let history = isServer ? createMemoryHistory() : createWebHistory()
const router = createRouter({
history:createWebHistory(),
routes: routes,
linkActiveClass: 'active'
})
export default router;import {createApp} from 'vue'
import App from './App.vue'
import router from './router/router.js'
import VueRouter from 'vue-router'
createApp(App).use(VueRouter).use(router).mount('#app')<script>
import AppView from "./AppView.vue";
import AppRegister from "./AppRegister.vue";
export default {
name: "App",
components: {
AppView,
AppRegister
},
data() {
return {};
},
beforeCreate() {
console.log('beforeCreating...');
console.log(this.$route.query.mode);
this.$router.push('/view');
// if(typeof this.$route.query.mode !== 'undefined') {
// const redirectPath = this.$route.query.mode || "/";
// console.log("redirectPath : " + redirectPath);
// }
},
created() {
console.log('creating...');
},
mounted() {
console.log('mounting...');
}
};
</script>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: "B Titr", sans-serif;
background-image: url("./assets/Suggestion.png");
background-repeat: no-repeat;
direction: rtl;
}
.container {
max-width: 1000px;
margin: 30px auto;
overflow: auto;
min-height: 300px;
border: 1px solid steelblue;
padding: 30px;
border-radius: 5px;
}
.btn {
display: inline-block;
background: #000;
color: #fff;
border: none;
padding: 10px 20px;
margin: 5px;
border-radius: 5px;
cursor: pointer;
text-decoration: none;
font-size: 15px;
font-family: inherit;
}
.btn:focus {
outline: none;
}
.btn:active {
transform: scale(0.98);
}
.btn-block {
display: block;
width: 100%;
}
</style>发布于 2021-05-07 07:23:22
在main.js中,您不需要.use(VueRouter),因为插件初始化已经被导入的router实例覆盖:
import router from './router/router.js'
import VueRouter from 'vue-router'
createApp(App).use(VueRouter).use(router).mount('#app') ❌
^^^^^^^^^^^^^^^
createApp(App).use(router).mount('#app') ✅并确保您的根组件(例如,App.vue)中有一个router-view:
<template>
<router-view />
</template>https://stackoverflow.com/questions/67415157
复制相似问题