无法在JavaScript中创建Vue路由器实例

编程入门 行业动态 更新时间:2024-10-25 08:16:15
本文介绍了无法在JavaScript中创建Vue路由器实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

按照指南,我到达了需要的地方创建一个Vue实例(似乎可行)。但是,我还需要在Vue的构造函数中提供Vuew Router的实例,如下所示。

Following the guide I arrived at the point where I need to create an instance of Vue (which seems to work). However, I also need to provide an instance of Vuew Router into the constructor of Vue, as shown below.

const router = new VueRouter({ routes }); const app = new Vue({ router }).$mount('#app');

遗憾的是,它会产生以下错误。我不确定如何处理它,并且 googlearching 。

Regrettably, it produces the following error. I'm very uncertain on how to deal with it and troubleshooting seems to be rather sparesely documented when googlearching.

Uncaught ReferenceError: VueRouter is not defined(…)

我确保已经安装了两个软件包以及一些其他功能。

I made sure that I have both packages installed, plus some extras.

+-- vue@2.0.8 +-- vue-cli@2.5.1 +-- vue-router@2.0.3

我还没有实现下面两个代码的导入(实际上并不确定在哪里放置代码,以及当我尝试使用 index.js

I haven't implemented the importing for those two below (not sure where to put in the code, really, and when I tried with my index.js it barked not recognizing the token). However, I believe that those are not required because Vue is still recognized and only its router fails to be recognized. If importing would be crutial, I have a feeling that both would fail if omitted.

import Vue from 'vue' import VueRouter from 'vue-router'

整个过程特别棘手,因为要运行一个决定它在Net.Core下,而不在NodeJs下,所以我可能会受到限制。我们不会使用Webpack或Browserify来保持进程运行(相反,我们会继续运行 dotnet )。不确定在此阶段是否有相关信息,但我已经读过它应该是 哦,这么简单又容易,但是坦率地说,这似乎简直就是简单。因此,我怀疑如果将其部署在特定的环境中很容易,但就我而言,需要进行一些手动按摩。

The whole thing is extra tricky because there's a decision to run it under Net.Core and not NodeJs, so I might be limited by that. We're won't be using Webpack or Browserify to keep the process running (instead we go the plain dotnet run). Not sure if it's relevant info at this stage but I've read that it's supposed to be "oh, so simple and easy" but, frankly speaking, it seems to be anything but simple and easy. So I suspect that it's easy if deployed in a certain environment but in my case requiring some hands-on massaging.

我可以通过哪些方法来进一步调查该问题?

What can I look into to investigate the issue further?

推荐答案

以下是vue-router的基本设置,但是的,您肯定需要导入vue& vue-router可以照原样引用它们:

Below is a basic set up for vue-router, but yes you definitely need to import vue & vue-router to be able to reference them as you are:

import Vue from 'vue'; import VueRouter from 'vue-router'; Vue.use( VueRouter ); const Foo = { template: '<div>foo</div>' } const Bar = { template: '<div>bar</div>' } const routes = [ { path: '/foo', component: Foo }, { path: '/bar', component: Bar } ] const router = new VueRouter({ routes }) const app = new Vue({ router }).$mount('#app')

<div id="app"> <h1>Hello App!</h1> <p> <router-link to="/foo">Go to Foo</router-link> <router-link to="/bar">Go to Bar</router-link> </p> <router-view></router-view> </div>

您必须使导入功能正常运行,尽管我从另一个问题中知道这很棘手。

you'll have to get your import set up functioning for this to work though which I know from another question has been proving tricky.

如果要退出导入,您只需链接到html中的库,并省略顶部的2条导入行,即

If you have to move away from imports you can just link to the libaries in your html and omit the 2 import lines at the top, i.e.

Vue.use( VueRouter ); const Foo = { template: '<div>foo</div>' } const Bar = { template: '<div>bar</div>' } const routes = [ { path: '/foo', component: Foo }, { path: '/bar', component: Bar } ] const router = new VueRouter({ routes }) const app = new Vue({ router }).$mount('#app')

<script src="unpkg/vue/dist/vue.js"></script> <script src="unpkg/vue-router@2.0.3"></script> <div id="app"> <h1>Hello App!</h1> <p> <router-link to="/foo">Go to Foo</router-link> <router-link to="/bar">Go to Bar</router-link> </p> <router-view></router-view> </div>

更多推荐

无法在JavaScript中创建Vue路由器实例

本文发布于:2023-11-25 22:32:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631615.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由器   实例   JavaScript   Vue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!