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

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

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 更改为
  1. import * as Vue from 'vue'
  2. import * as VueRouter from 'vue-router'
  3. // 1. 定义路由组件。
  4. // 这些可以从其他文件导入
  5. const Home = { template: '<div>Home</div>' }
  6. const About = { template: '<div>About</div>' }
  7. // 2. 定义一些路由
  8. // 每个路由应映射到一个组件。
  9. // 我们将在后面讨论嵌套路由。
  10. const routes = [
  11. { path: '/', component: Home },
  12. { path: '/about', component: About },
  13. ]
  14. // 3. 创建路由器实例并传递`routes`选项
  15. // 您可以在这里传递其他选项,但现在让我们保持简单。
  16. const router = VueRouter.createRouter({
  17. // 4. 提供要使用的历史实现。我们在这里出于简单起见使用哈希历史。
  18. history: VueRouter.createWebHashHistory(),
  19. routes, // 简写为 `routes: routes`
  20. })
  21. // 5. 创建并挂载根实例。
  22. const app = Vue.createApp({})
  23. // 确保 _使用_ 路由器实例使整个应用程序具有路由器功能。
  24. app.use(router)
  25. app.mount('#app')
  • index.html 更改为
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <link rel="icon" href="/favicon.ico">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Vite App</title>
  8. </head>
  9. <body>
  10. <div id="app">
  11. <h1>Hello App!</h1>
  12. <p>
  13. <!-- 使用router-link组件进行导航。 -->
  14. <!-- 通过传递`to`属性来指定链接。 -->
  15. <!-- `<router-link>`将使用正确的`href`属性呈现`<a>`标签 -->
  16. <router-link to="/">转到主页</router-link>
  17. <router-link to="/about">转到关于</router-link>
  18. </p>
  19. <!-- 路由出口 -->
  20. <!-- 与路由匹配的组件将在此处呈现 -->
  21. <router-view></router-view>
  22. </div>
  23. <script type="module" src="/src/main.ts"></script>
  24. </body>
  25. </html>
  26. - 运行 `yarn start`

Vue和vue-router版本:

  1. "dependencies": {
  2. "vue": "^3.3.2",
  3. "vue-router": "^4.2.0"
  4. },
英文:

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
  1. import * as Vue from &#39;vue&#39;
  2. import * as VueRouter from &#39;vue-router&#39;
  3. // 1. Define route components.
  4. // These can be imported from other files
  5. const Home = { template: &#39;&lt;div&gt;Home&lt;/div&gt;&#39; }
  6. const About = { template: &#39;&lt;div&gt;About&lt;/div&gt;&#39; }
  7. // 2. Define some routes
  8. // Each route should map to a component.
  9. // We&#39;ll talk about nested routes later.
  10. const routes = [
  11. { path: &#39;/&#39;, component: Home },
  12. { path: &#39;/about&#39;, component: About },
  13. // { path: &#39;/smm&#39;, component: About },
  14. ]
  15. // 3. Create the router instance and pass the `routes` option
  16. // You can pass in additional options here, but let&#39;s
  17. // keep it simple for now.
  18. const router = VueRouter.createRouter({
  19. // 4. Provide the history implementation to use. We are using the hash history for simplicity here.
  20. history: VueRouter.createWebHashHistory(),
  21. routes, // short for `routes: routes`
  22. })
  23. // 5. Create and mount the root instance.
  24. const app = Vue.createApp({})
  25. // Make sure to _use_ the router instance to make the
  26. // whole app router-aware.
  27. app.use(router)
  28. app.mount(&#39;#app&#39;)
  • Change index.html to
  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;en&quot;&gt;
  3. &lt;head&gt;
  4. &lt;meta charset=&quot;UTF-8&quot;&gt;
  5. &lt;link rel=&quot;icon&quot; href=&quot;/favicon.ico&quot;&gt;
  6. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  7. &lt;title&gt;Vite App&lt;/title&gt;
  8. &lt;/head&gt;
  9. &lt;body&gt;
  10. &lt;div id=&quot;app&quot;&gt;
  11. &lt;h1&gt;Hello App!&lt;/h1&gt;
  12. &lt;p&gt;
  13. &lt;!-- use the router-link component for navigation. --&gt;
  14. &lt;!-- specify the link by passing the `to` prop. --&gt;
  15. &lt;!-- `&lt;router-link&gt;` will render an `&lt;a&gt;` tag with the correct `href` attribute --&gt;
  16. &lt;router-link to=&quot;/&quot;&gt;Go to Home&lt;/router-link&gt;
  17. &lt;router-link to=&quot;/about&quot;&gt;Go to About&lt;/router-link&gt;
  18. &lt;/p&gt;
  19. &lt;!-- route outlet --&gt;
  20. &lt;!-- component matched by the route will render here --&gt;
  21. &lt;router-view&gt;&lt;/router-view&gt;
  22. &lt;/div&gt;
  23. &lt;script type=&quot;module&quot; src=&quot;/src/main.ts&quot;&gt;&lt;/script&gt;
  24. &lt;/body&gt;
  25. &lt;/html&gt;
  26. - run `yarn start`

Vue and vue-router versions:

  1. &quot;dependencies&quot;: {
  2. &quot;vue&quot;: &quot;^3.3.2&quot;,
  3. &quot;vue-router&quot;: &quot;^4.2.0&quot;
  4. },

答案1

得分: 1

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

index.html

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

App.vue

  1. <template>
  2. <h1>Hello App!</h1>
  3. <p>
  4. <router-link to="/">Go to Home</router-link>
  5. <router-link to="/about">Go to About</router-link>
  6. </p>
  7. <router-view></router-view>
  8. </template>
  9. <script setup></script>

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

main.ts

  1. import App from './App.vue'
  2. import { createApp } from 'vue'
  3. .
  4. .
  5. .
  6. const app = createApp(App)
  7. app.use(router)
  8. 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

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

App.vue

  1. &lt;template&gt;
  2. &lt;h1&gt;Hello App!&lt;/h1&gt;
  3. &lt;p&gt;
  4. &lt;router-link to=&quot;/&quot;&gt;Go to Home&lt;/router-link&gt;
  5. &lt;router-link to=&quot;/about&quot;&gt;Go to About&lt;/router-link&gt;
  6. &lt;/p&gt;
  7. &lt;router-view&gt;&lt;/router-view&gt;
  8. &lt;/template&gt;
  9. &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

  1. import App from &#39;./App.vue&#39;
  2. import { createApp } from &#39;vue&#39;
  3. .
  4. .
  5. .
  6. const app = createApp(App)
  7. app.use(router)
  8. 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:

确定