Vue路由器在使用create-vue@3时无法加载。

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

Vue router doesn't load with create-vue@3

问题

Here's the translated content:

我正在尝试使用Vue Router,但没有得到预期的结果。

我正在按照https://router.vuejs.org/guide/教程中的步骤进行操作,如果我使用CDN并将其放在唯一的HTML文件中,它可以工作,但如果我尝试以下方式则不行:

复现步骤:

  • 运行 npm create vue@3
  • 配置:
    • 项目名称:... vue-project
    • 添加TypeScript?是
    • 添加JSX支持?是
    • 为单页应用程序开发添加Vue Router?是
    • 添加Pinia进行状态管理?... 否
    • 添加Vitest进行单元测试?... 否
    • 添加端到端测试解决方案?› 否
    • 添加ESLint进行代码质量?... 否
  • 安装所有依赖项
  • src/main.ts 更改为
import * as Vue from 'vue'
import * as VueRouter from 'vue-router'

// 1. 定义路由组件。
// 这些可以从其他文件导入
const Home = { template: '<div>Home</div>' }
const About = { template: '<div>About</div>' }

// 2. 定义一些路由
// 每个路由应映射到一个组件。
// 我们将在后面讨论嵌套路由。
const routes = [
  { path: '/', component: Home },
  { path: '/about', component: About },
]

// 3. 创建路由器实例并传递`routes`选项
// 您可以在这里传递其他选项,但现在让我们保持简单。
const router = VueRouter.createRouter({
  // 4. 提供要使用的历史实现。我们在这里出于简单起见使用哈希历史。
  history: VueRouter.createWebHashHistory(),
  routes, // 简写为 `routes: routes`
})

// 5. 创建并挂载根实例。
const app = Vue.createApp({})
// 确保 _使用_ 路由器实例使整个应用程序具有路由器功能。
app.use(router)

app.mount('#app')
  • index.html 更改为
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <link rel="icon" href="/favicon.ico">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vite App</title>
  </head>
  <body>

<div id="app">

    <h1>Hello App!</h1>
    <p>
      <!-- 使用router-link组件进行导航。 -->
      <!-- 通过传递`to`属性来指定链接。 -->
      <!-- `<router-link>`将使用正确的`href`属性呈现`<a>`标签 -->
      <router-link to="/">转到主页</router-link>
      <router-link to="/about">转到关于</router-link>
    </p>
    <!-- 路由出口 -->
    <!-- 与路由匹配的组件将在此处呈现 -->
    <router-view></router-view>
</div>

    <script type="module" src="/src/main.ts"></script>
  </body>
</html>

- 运行 `yarn start`

Vue和vue-router版本:

  "dependencies": {
    "vue": "^3.3.2",
    "vue-router": "^4.2.0"
  },
英文:

i was trying out the vue router but i'm not getting the expected result.

I'm following the https://router.vuejs.org/guide/ tutorial, if i use the CDN and put it in a unique HTML file it works, but if i try in this way it doesn´t:

Steps to reproduce:

  • run npm create vue@3
  • Configuration:
    • Project name: … vue-project
    • Add TypeScript? Yes
    • Add JSX Support? Yes
    • Add Vue Router for Single Page Application development? Yes
    • Add Pinia for state management? … No
    • Add Vitest for Unit Testing? … No
    • Add an End-to-End Testing Solution? › No
    • Add ESLint for code quality? … No
  • install all dependencies
  • Change src/main.ts to
import * as Vue from &#39;vue&#39;
import * as VueRouter from &#39;vue-router&#39;

// 1. Define route components.
// These can be imported from other files
const Home = { template: &#39;&lt;div&gt;Home&lt;/div&gt;&#39; }
const About = { template: &#39;&lt;div&gt;About&lt;/div&gt;&#39; }

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

// 3. Create the router instance and pass the `routes` option
// You can pass in additional options here, but let&#39;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(&#39;#app&#39;)
  • Change index.html to
&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;link rel=&quot;icon&quot; href=&quot;/favicon.ico&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Vite App&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;

&lt;div id=&quot;app&quot;&gt;

    &lt;h1&gt;Hello App!&lt;/h1&gt;
    &lt;p&gt;
      &lt;!-- use the router-link component for navigation. --&gt;
      &lt;!-- specify the link by passing the `to` prop. --&gt;
      &lt;!-- `&lt;router-link&gt;` will render an `&lt;a&gt;` tag with the correct `href` attribute --&gt;
      &lt;router-link to=&quot;/&quot;&gt;Go to Home&lt;/router-link&gt;
      &lt;router-link to=&quot;/about&quot;&gt;Go to About&lt;/router-link&gt;
    &lt;/p&gt;
    &lt;!-- route outlet --&gt;
    &lt;!-- component matched by the route will render here --&gt;
    &lt;router-view&gt;&lt;/router-view&gt;
&lt;/div&gt;

    &lt;script type=&quot;module&quot; src=&quot;/src/main.ts&quot;&gt;&lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;

- run `yarn start`

Vue and vue-router versions:

  &quot;dependencies&quot;: {
    &quot;vue&quot;: &quot;^3.3.2&quot;,
    &quot;vue-router&quot;: &quot;^4.2.0&quot;
  },

答案1

得分: 1

Vue应该被注入到index.html中。将所有内容移到<div id="app">内,并放入一个.vue SFC文件。

index.html

<body>
    <div id="app"></div>
    <script type="module" src="/src/main.ts"></script>
</body>

App.vue

<template>
  <h1>Hello App!</h1>
  <p>
    <router-link to="/">Go to Home</router-link>
    <router-link to="/about">Go to About</router-link>
  </p>
  <router-view></router-view>
</template>

<script setup></script>

然后,在main.ts中导入这个组件,并将其设置为根组件使用createApp()。

main.ts

import App from './App.vue'
import { createApp } from 'vue'
.
.
.
const app = createApp(App)
app.use(router)

app.mount('#app')
英文:

Vue should be injected into index.html. Move everything inside the &lt;div id=&quot;app&quot;&gt; into a .vue SFC file

index.html

&lt;body&gt;
    &lt;div id=&quot;app&quot;&gt;&lt;/div&gt;
    &lt;script type=&quot;module&quot; src=&quot;/src/main.ts&quot;&gt;&lt;/script&gt;
&lt;/body&gt;

App.vue

&lt;template&gt;
  &lt;h1&gt;Hello App!&lt;/h1&gt;
  &lt;p&gt;
    &lt;router-link to=&quot;/&quot;&gt;Go to Home&lt;/router-link&gt;
    &lt;router-link to=&quot;/about&quot;&gt;Go to About&lt;/router-link&gt;
  &lt;/p&gt;
  &lt;router-view&gt;&lt;/router-view&gt;
&lt;/template&gt;

&lt;script setup&gt;&lt;/script&gt;

then import this component into main.ts and set it as the root component with createApp()

main.ts

import App from &#39;./App.vue&#39;
import { createApp } from &#39;vue&#39;
.
.
.
const app = createApp(App)
app.use(router)

app.mount(&#39;#app&#39;)

huangapple
  • 本文由 发表于 2023年5月13日 17:17:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241963.html
匿名

发表评论

匿名网友

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

确定