Vue app.use() 不接受我的路由器作为参数

问题描述 投票:0回答:1

我正在使用 Django 和 Vue 3(带有 pinia)制作一个新项目。我正在尝试设置 vue router(最新版本),我在 vue router 网站中找到了这些说明 https://router.vuejs.org/guide/

// 1. Define route components.
// These can be imported from other files
const Home = { template: '<div>Home</div>' }
const About = { template: '<div>About</div>' }

// 2. Define some routes
// Each route should map to a component.
// We'll talk about nested routes later.
const routes = [
  { path: '/', component: Home },
  { path: '/about', component: About },
]

// 3. Create the router instance and pass the `routes` option
// You can pass in additional options here, but let's
// keep it simple for now.
const router = VueRouter.createRouter({
  // 4. Provide the history implementation to use. We are using the hash history for simplicity here.
  history: VueRouter.createWebHashHistory(),
  routes, // short for `routes: routes`
})

// 5. Create and mount the root instance.
const app = Vue.createApp({})
// Make sure to _use_ the router instance to make the
// whole app router-aware.
app.use(router)

app.mount('#app')

// Now the app has started!

我做了类似的事情:

import './assets/main.css'

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import router from "./router";
import App from './App.vue'

import { createRouter, createWebHistory } from "vue-router";

const routes = [
    { path: "/", component: () => import("/src/pages/Index.vue") }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

const app = createApp(App)
app.use(createPinia())
app.use(router)

app.mount('#app')

我的 IDE(使用 PyCharm)表示 router 不是 app.use() 可接受的参数。确切的消息是:

Argument type Router is not assignable to parameter type Plugin<unknown[]> ...   Type Router is not assignable to type (((app: App, ...options: unknown[]) => any) & {install?: PluginInstallFunction<unknown[]>}) | {install: PluginInstallFunction<unknown[]>}     Type (app: App) => void is not assignable to type PluginInstallFunction<unknown[]> 

运行服务器时,我没有收到任何错误,但它不显示我的索引页面,并且始终停留在 App.vue 上。

我做错了什么?

App.vue 文件:

<template>
  <header>
    <img alt="Vue logo" class="logo" src="./assets/logo.svg" width="125" height="125" />
    <div class="wrapper">
      <HelloWorld msg="You did it!" />
    </div>
  </header>

  <main>
    <div id="app">
      <div>
      <form action="" @submit.prevent="onSubmit">
        <div class="form-group d-flex flex-row">
          <input class="form-control col-3 mx-2" placeholder="Name" v-model="student.name" />
          <input class="form-control col-3 mx-2" placeholder="Course" v-model="student.course" />
          <input class="form-control col-3 mx-2" placeholder="Rating" v-model="student.rating" />
          <button class="btn btn-success">Submit</button>
        </div>
      </form>
      <table class="table">
        <thead>
          <th>Name</th>
          <th>Course</th>
          <th>Rating</th>
          <th>Remove</th>
        </thead>
        <tbody v-for="student in students" :key="student.id" @dblclick="setStudent(student)">
          <td>{{ student.name }}</td>
          <td>{{ student.course }}</td>
          <td>{{ student.rating }}</td>
          <td><btn class="btn btn-outline-danger btn-sm m-1" @click="deleteStudent(student.id)">x</btn></td>
        </tbody>
      </table>
        </div>
    </div>
<!--    <TheWelcome />-->
  </main>
</template>

<script setup>
import { ref, onMounted } from "vue"
import HelloWorld from './components/HelloWorld.vue'
import TheWelcome from './components/TheWelcome.vue'
const blankStudent = { name: null, course: null, rating: null };
const students = ref(null);
const student = ref(blankStudent);

async function getStudents() {
  const response = await fetch("http://localhost:8000/api/students/");
  const data = await response.json();
  students.value = data;
}

async function addStudent() {
  const response = await fetch("http://localhost:8000/api/students/", {
    method: "post",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(student.value)
  });
  await getStudents();
  console.log("adding", response);
}

async function editStudent() {
  const response = await fetch(`http://localhost:8000/api/students/${student.value.id}/`, {
    method: "put",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(student.value)
  });
  await getStudents();
  student.value = blankStudent;
  console.log("adding", response);
}
function setStudent(event) {
  student.value = event;
}
async function deleteStudent(id) {
  const response = await fetch(`http://localhost:8000/api/students/${id}/`, {
    method: "delete",
    headers: { "Content-Type": "application/json" },
    // body: JSON.stringify(student.value)
  });
  await getStudents();
}
async function onSubmit() {
  if (!student.value.id) addStudent();
  else editStudent();
}
onMounted(async() => {
  await getStudents()
})
</script>

<style scoped>
header {
  line-height: 1.5;
}

.logo {
  display: block;
  margin: 0 auto 2rem;
}

@media (min-width: 1024px) {
  header {
    display: flex;
    place-items: center;
    padding-right: calc(var(--section-gap) / 2);
  }

  .logo {
    margin: 0 2rem 0 0;
  }

  header .wrapper {
    display: flex;
    place-items: flex-start;
    flex-wrap: wrap;
  }
}
</style>

和 router.js 文件:

// import { createRouter, createWebHashHistory } from "vue-router";
//
// const routes = [
//     { path: "/a", component: () => import("/src/pages/Index.vue") }
// ]
//
// const router = createRouter({
//     history: createWebHashHistory(),
//     routes
// })
//
// export default router

 import { createWebHistory, createRouter } from "vue-router";
const HomeComponent = () =>import(/* webpackChunkName: "3420.js" */ "./pages/Index.vue");
const AboutComponent = "<div style='color: red'>About</div>";

const routes = [
    {
        path: "/",
        name: "Home Component",
        component: HomeComponent,

    },
    {

        path: "/about-us",
        name: "About Component",
        component: AboutComponent,

    },
]

const router = createRouter({
    mode: "history",
    history: createWebHistory(),
    routes,
    linkActiveClass: "nested-active", // active class for non-exact links.
    linkExactActiveClass: "nested-active",
});
export default router;
javascript django vue.js routes vue-router4
1个回答
1
投票
Create new router file  as "router.js" inside this file write this 

------------------------------------------------------------


 import { createWebHistory, createRouter } from "vue-router";
const HomeComponent = () =>import(/* webpackChunkName: "3420.js" */ "./components/home");
const AboutComponent = () =>import(/* webpackChunkName: "1420.js" */ "./components/about");

const routes = [
    {
        path: "/",
        name: "Home Component",
        component: HomeComponent,

    },
    {
        
        path: "/about-us",
        name: "About Component",
        component: AboutComponent,

    },
]

const router = createRouter({
    mode: "history",
    history: createWebHistory(),
    routes,
    linkActiveClass: "nested-active", // active class for non-exact links.
    linkExactActiveClass: "nested-active",
});
export default router;
----------------------------------------------------------
Now in your Js file write this 


 import { createApp } from "vue";
    import router from "./router";
    import { createPinia } from "pinia";
    import App from "./components/App.vue";

const app = createApp(App);
app.use(router);
app.use(createPinia());
app.mount("#app");
    
© www.soinside.com 2019 - 2024. All rights reserved.